python - Matplotlip - plotting many lines from dataframe in one graph
问题描述
I have this df:
United States Russia United Kingdom Italy Brazil Germany Turkey France Iran China
0 0 0 0 0 0 0 NaN 0 0 27
1 0 0 0 0 0 0 NaN 0 0 27
2 0 0 0 0 0 0 NaN 0 0 27
3 0 0 0 0 0 0 NaN 0 0 44
4 0 0 0 0 0 0 NaN 0 0 44
.. ... ... ... ... ... ... ... ... ... ...
131 1309541 198676 215260 218268 155939 169218 137115.0 138854 106220 83991
132 1329799 209688 219183 219070 162699 169575 138657.0 139063 107603 84010
133 1347916 221344 223060 219814 168331 170508 139771.0 139519 109286 84011
134 1369964 232243 226463 221216 177589 171306 141475.0 140227 110767 84018
135 1390746 242271 229705 222104 188974 172239 143114.0 140734 112725 84024
and I need to plot each progression on a separate line with matplotlib.
So far I have:
def world_plot_logarithmic(df):
plt.rcParams["font.family"] = "Times New Roman"
plt.rcParams["font.size"] = "8"
plt.rcParams['axes.grid'] = True
#a = [pow(10, i) for i in range(10)]
fig = plt.figure()
ax = fig.add_subplot(2, 1, 1)
line, = ax.plot(df, color='blue', lw=1)
ax.set_yscale('log')
pylab.show()
But this works for a single column.
How can I plot all countries in a single graph using different colors, randomly chosen, for each country?
解决方案
我认为您可以plot
直接在数据框上使用,例如:
def world_plot_logarithmic(df):
plt.rcParams["font.family"] = "Times New Roman"
plt.rcParams["font.size"] = "8"
plt.rcParams['axes.grid'] = True
df.plot(logy=True)
推荐阅读
- php - 如何为 WordPress 插件或主题添加自动更新功能
- pandas - Pandas 中的 EWM 用于时间序列转换
- node.js - 如何使用服务帐户密钥文件调用受限的 firebase 函数?
- c - 无法从 Turbo C 中读取文件
- python - 我如何将图像用作按钮功能(Tkinter)
- python - 打印字典时出现KeyError?
- r - 如何在 r 中跨时间绘制多个组?
- python - Tensorflow:将张量列设置为无穷大
- swift - 如何通过访问 API 端点来解码 JSON 数据?
- informatica - Informatica 过滤器转换条件为 TRUE,没有任何文件