【发布时间】:2016-11-02 07:26:22
【问题描述】:
我在 pandas 数据框中有一个表,其中包含 2 列
+----------+------------+
| id| orders |
+----------+------------+
| 1 | 1100 |
| 2 | 22753 |
| 3 | 34 |
| 4 | 11 |
| 5 | 430 |
| 6 | 1175 |
| ... | .. |
| 800 | 17 |
+----------+------------+
我想绘制一个条形图,我希望 x 轴条的范围为
1-100,100-200,200-300 以此类推直到 700-800,
以及y轴上各自的总订单数
请帮帮我,我正在使用
matplotlib.pyplot 包。
我尝试运行此代码
fig = plt.figure(figsize=(17, 6)) # Create matplotlib figure
ax = fig.add_subplot(111) # Create matplotlib axes
width = 0.2
df.orders.plot(kind='bar', color='red', ax=ax, width=width, position=1)
ax.legend()
plt.show()
【问题讨论】:
-
df.plot(kind='bar', color='red', ax=ax, width=width, position=1)。你可以试试这个吗?
-
@Backtrack 我试过了,但是在 x 轴上我有从 1 到 800 的单独条,这是不合适的,我希望它在 1-100、100-200 等组中
-
你应该创建一个新的
dataframe,它有8行和相应的订单总和。 -
@jbndlr 先生,看到我也添加了图像,您能告诉我该怎么做吗?我以前的 df 有 8 行的新数据框
-
plt.xticks(np.arange(min(df.id), max(df.id)+100, 100.0))。像这样的东西
标签: python python-2.7 python-3.x pandas matplotlib