plot dataframe with two y-axes

懵懂的女人 提交于 2021-02-20 06:16:18

问题


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?


回答1:


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()




回答2:


In general just add one extra argument to your plot call: secondary_y=['size'].

In this case a separate plot is easier though, because of line vs bars etc.



来源:https://stackoverflow.com/questions/51784495/plot-dataframe-with-two-y-axes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!