我想使用同一把斧头在同一行上绘制多个条形图,但是pandas
绘图功能似乎每次都删除了我以前的绘图。
例如 :
import pandas as pd
import pylab as plt
fig, ax = plt.subplots()
df = pd.DataFrame({'lab':['D', 'E', 'F'], 'val':[10, 30, 20]})
df.plot.bar(x='lab', y='val', rot=0, ax=ax)
df = pd.DataFrame({'lab':['A', 'B', 'C'], 'val':[10, 30, 20]})
df.plot.bar(x='lab', y='val', rot=0, ax=ax)
ax.set_xlim(-.5, 6)
plt.show()
给我吗
我尝试使用pandas函数的
xticks
参数,但是它不起作用。 最佳答案
您需要设置横条在X轴上的位置
https://matplotlib.org/gallery/lines_bars_and_markers/barchart.html#sphx-glr-gallery-lines-bars-and-markers-barchart-py
men_means, men_std = (20, 35, 30, 35, 27), (2, 3, 4, 1, 2)
women_means, women_std = (25, 32, 34, 20, 25), (3, 5, 2, 3, 3)
ind = np.arange(len(men_means)) # the x locations for the groups
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(ind - width/2, men_means, width, yerr=men_std,
color='SkyBlue', label='Men')
rects2 = ax.bar(ind + width/2, women_means, width, yerr=women_std,
color='IndianRed', label='Women')
注意“ ind”部分-建立条的位置。另请注意,条形的颜色已拼出。