Aas*_*hil 13 plot matplotlib series ipython pandas
请考虑我的系列如下:第一列是article_id,第二列是频率计数.
article_id
1 39
2 49
3 187
4 159
5 158
...
16947 14
16948 7
16976 2
16977 1
16978 1
16980 1
Name: article_id, dtype: int64
Run Code Online (Sandbox Code Playgroud)
我使用以下命令从数据帧中获取此系列:
logs.loc[logs['article_id'] <= 17029].groupby('article_id')['article_id'].count()
Run Code Online (Sandbox Code Playgroud)
logs是这里的数据框,article_id是其中的一列.
如何绘制条形图(使用Matlplotlib),使得article_id位于X轴上,频率计数位于Y轴上?
我的本能是使用.tolist()将其转换为列表但不保留article_id.
jez*_*ael 30
您需要的IIUC Series.plot.bar:
#pandas 0.17.0 and above
s.plot.bar()
#pandas below 0.17.0
s.plot('bar')
Run Code Online (Sandbox Code Playgroud)
样品:
import pandas as pd
import matplotlib.pyplot as plt
s = pd.Series({16976: 2, 1: 39, 2: 49, 3: 187, 4: 159,
5: 158, 16947: 14, 16977: 1, 16948: 7, 16978: 1, 16980: 1},
name='article_id')
print (s)
1 39
2 49
3 187
4 159
5 158
16947 14
16948 7
16976 2
16977 1
16978 1
16980 1
Name: article_id, dtype: int64
s.plot.bar()
plt.show()
Run Code Online (Sandbox Code Playgroud)
新的pandas API建议采用以下方式:
import pandas as pd
s = pd.Series({16976: 2, 1: 39, 2: 49, 3: 187, 4: 159,
5: 158, 16947: 14, 16977: 1, 16948: 7, 16978: 1, 16980: 1},
name='article_id')
s.plot(kind="bar", figsize=(20,10))
Run Code Online (Sandbox Code Playgroud)
如果您使用 Jupyter,则不需要该matplotlib库。
| 归档时间: |
|
| 查看次数: |
24209 次 |
| 最近记录: |