pandas 用两个 y 轴绘制数据框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51784495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:55:17  来源:igfitidea点击:

plot dataframe with two y-axes

pythonpandasdataframematplotlibmultiple-axes

提问by Christin Abel

I have the following dataframe:

我有以下数据框:

   land_cover          1         2         3         4         5         6       size
0          20  19.558872  6.856950  3.882243  1.743048  1.361306  1.026382  16.520265
1          30   9.499454  3.513521  1.849498  0.836386  0.659660  0.442690  8.652517 
2          40  10.173790  3.123167  1.677257  0.860317  0.762718  0.560290  11.925280 
3          50  10.098777  1.564575  1.280729  0.894287  0.884028  0.887448  12.647710
4          60   6.166109  1.588687  0.667839  0.230659  0.143044  0.070628  2.160922 
5         110  17.846565  3.884678  2.202129  1.040551  0.843709  0.673298  30.406541 

I want to plot the data in the way that:
. land_cover is the x-axis
. cols 1 - 6 should be stacked bar plots per land_cover class (row)
. and the column 'size' should be a second y-axis and could be a simple point symbol for every row and additionally a smooth line connecting the points

Any ideas?

我想在方式绘制数据:
。land_cover 是 x 轴
。cols 1 - 6 应该是每个 land_cover 类(行)的堆叠条形图
。并且“大小”列应该是第二个 y 轴,并且可以是每一行的简单点符号,另外还有一条连接点的平滑线有

任何想法吗?

回答by RobJan

Your code is pretty fine. I only add two more lines

你的代码很好。我只添加了两行

 import matplotlib.pyplot as plt 

 df.plot(x="land_cover", y=[1, 2, 3, 4, 5, 6], stacked=True, kind="bar")
 ax = df['size'].plot(secondary_y=True, color='k', marker='o')
 ax.set_ylabel('size')

 plt.show()

plot

阴谋