Markus
Markus

Reputation: 3782

How to create a bar plot for my data?

I want to create a simple two-sided bar plot (with two bars per each X axis like here) using pandas and matplotlib.

df = 
ID      Rank1   Rank2
243390  120.5   9.0
243810  37.5    10.0
253380  77.0    5.0
255330  29.0    8.0
256520  177.5   25.0

I was able to create a bar plot with two bars, but the plot does not appear.

fig = plt.figure(figsize=(12,8))
ax = fig.add_subplot(111)
bar_width = 0.35
opacity = 0.8

rects1 = plt.bar(df["ID"], df["Rank1"], bar_width, 
                 alpha=opacity,
                 color='b',
                 label='Rank1')

rects2 = plt.bar(df["ID"] + bar_width, df["Rank2"], bar_width, 
                 alpha=opacity,
                 color='r',
                 label='Rank2')
plt.legend()
#plt.tight_layout()
plt.show() 

Upvotes: 1

Views: 84

Answers (1)

Scott Boston
Scott Boston

Reputation: 153460

Your bar_widths are to small for your x-axis scale. Try this:

fig = plt.figure(figsize=(12,8))
ax = fig.add_subplot(111)
bar_width = 200
opacity = 0.8

rects1 = plt.bar(df["ID"]- bar_width/2, df["Rank1"], bar_width, 
                 alpha=opacity,
                 color='b',
                 label='Rank1')

rects2 = plt.bar(df["ID"] + bar_width/2, df["Rank2"], bar_width, 
                 alpha=opacity,
                 color='r',
                 label='Rank2')
plt.legend()
#plt.tight_layout()
plt.show() 

enter image description here

Upvotes: 1

Related Questions