首页 > 解决方案 > 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?

标签: pythonpandasmatplotlib

解决方案


我认为您可以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)

推荐阅读