python - 使用 matplotlib python 和 pandas 绘图

标签 python matplotlib pandas

我有一个数据帧(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/

相关文章:

python - 如何将word2vec转为glove格式

python - 让子进程保持事件状态并继续向其发出命令? Python

python - 如何在 AWS EC2 Ubuntu 实例上为 IPython Notebook 配置启动脚本

python - 无法直接应用网格网格来创建 Surfaceplot

python - Numpy 条件最大范围

python - Pandas 计数字典中值的出现

python - 如何在 Ubuntu 上通过 pip 安装 python3 版本的软件包?

python - 使用 BigInts 创建位掩码

python - Matplotlib:如何删除一组子图之间的间距

Python经验分布函数(ecdf)实现