我有一个数据帧(df)和从该数据帧中切片的两个系列。
X 例如是 2014-10 | 2014年11月 例如,Y 是 123、345、678 等数字,我想像这样绘制图表:
700
*
500
300
*
100 *
2014-10 2014-11 2014-12
我的代码:
xlist = df.month #series
x_string = str(xlist) #string
ylist = df.numbers #series
y_string = str(ylist) #string
plt.xticks(x,x_string) #set to use my series as the x axis values
plt.plot(x, ylist, 'bo') #plot x(x_string) and y(ylist) axis on the graph.
plt.show() #show my graph
我尝试绘制系列和字符串,但都不起作用。
目前有人建议我不要使用 xticks,我可以获得:
df.plot(x='month',
y='numbers',
title='Time dependency of ...')
最佳答案
你可以这样做directly in pandas :
df.plot(x='month', y='numbers', title='Time dependency of ...')
或者:
df[['month', 'numbers']].set_index('month').plot(title='Time dependency of ...')
假设df
是一个带有“月份”和“数字”列的数据框。
关于python - 使用 matplotlib python 和 pandas 绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27180423/