python matplotlib multiple bars




python plot multiple figures (4)

The trouble with using dates as x-values, is that if you want a bar chart like in your second picture, they are going to be wrong. You should either use a stacked bar chart (colours on top of each other) or group by date (a "fake" date on the x-axis, basically just grouping the data points).

import numpy as np
import matplotlib.pyplot as plt

N = 3
ind = np.arange(N)  # the x locations for the groups
width = 0.27       # the width of the bars

fig = plt.figure()
ax = fig.add_subplot(111)

yvals = [4, 9, 2]
rects1 = ax.bar(ind, yvals, width, color='r')
zvals = [1,2,3]
rects2 = ax.bar(ind+width, zvals, width, color='g')
kvals = [11,12,13]
rects3 = ax.bar(ind+width*2, kvals, width, color='b')

ax.set_ylabel('Scores')
ax.set_xticks(ind+width)
ax.set_xticklabels( ('2011-Jan-4', '2011-Jan-5', '2011-Jan-6') )
ax.legend( (rects1[0], rects2[0], rects3[0]), ('y', 'z', 'k') )

def autolabel(rects):
    for rect in rects:
        h = rect.get_height()
        ax.text(rect.get_x()+rect.get_width()/2., 1.05*h, '%d'%int(h),
                ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)
autolabel(rects3)

plt.show()

How to plot multiple bars in matplotlib, when i tried to call the bar function multiple times, they overlap and as seen the below figure the highest value red can be seen only. How can I plot the multiple bars with dates on the x axes?

So far, i tried this:

import matplotlib.pyplot as plt
  import datetime

    x = [datetime.datetime(2011, 1, 4, 0, 0),
         datetime.datetime(2011, 1, 5, 0, 0),
         datetime.datetime(2011, 1, 6, 0, 0)]
    y = [4, 9, 2]
    z=[1,2,3]
    k=[11,12,13]

    ax = plt.subplot(111)![enter image description here][1]
    ax.bar(x, y,width=0.5,color='b',align='center')
    ax.bar(x, z,width=0.5,color='g',align='center')
    ax.bar(x, k,width=0.5,color='r',align='center')
    ax.xaxis_date()

    plt.show()

I got this:

The results should be something like,but with the dates are on the x axes and bars are next to each other:


import matplotlib.pyplot as plt
from matplotlib.dates import date2num
import datetime

x = [datetime.datetime(2011, 1, 4, 0, 0),
     datetime.datetime(2011, 1, 5, 0, 0),
     datetime.datetime(2011, 1, 6, 0, 0)]
x = date2num(x)

y = [4, 9, 2]
z=[1,2,3]
k=[11,12,13]

ax = plt.subplot(111)
ax.bar(x-0.2, y,width=0.2,color='b',align='center')
ax.bar(x, z,width=0.2,color='g',align='center')
ax.bar(x+0.2, k,width=0.2,color='r',align='center')
ax.xaxis_date()

plt.show()

I don't know what's the "y values are also overlapping" means, does the following code solve your problem?

ax = plt.subplot(111)
w = 0.3
ax.bar(x-w, y,width=w,color='b',align='center')
ax.bar(x, z,width=w,color='g',align='center')
ax.bar(x+w, k,width=w,color='r',align='center')
ax.xaxis_date()
ax.autoscale(tight=True)

plt.show()


I did this solution: if you want plot more than one plot in one figure, make sure before plotting next plots you have set right matplotlib.pyplot.hold(True) to able adding another plots.

About datetime values on X axe, solution using alignment of bars works for me. When you create another bar plot with matplotlib.pyplot.bar(), just use align='edge|center' and set width='+|-distance'.

When you set all bars (plots) right, you will see bars fine.


Use a 2D data structure and let matplotlib handle your column spacing:

import numpy as np
import matplotlib.pyplot as plt

x = np.random.randn(1000, 3)
bins = np.arange(-4.5, 4.5, 1)
colors = ['red', 'tan', 'lime']

plt.hist(x, bins=bins, color=colors, label=colors)
plt.legend()
plt.xticks(bins + 0.5, ['bin ' + str(int(x)) for x in (bins + 0.5)])
plt.show()

rwidth=1 argument in hist() will remove any space between the bars.

Source





matplotlib