15

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?

Christin Abel
  • 301
  • 1
  • 3
  • 10
  • 1
    What have you tried recently? – RobJan Aug 10 '18 at 10:30
  • df.plot(x="land_cover", y=[1, 2, 3, 4, 5, 6, 7], stacked=True, kind="bar") .... but I don't really know how to get the second axis in.... and also if that is the most elegant way to do it... – Christin Abel Aug 10 '18 at 10:50
  • 1
    check plotting on a secondary Y axis: http://pandas.pydata.org/pandas-docs/version/0.12/visualization.html – xan Aug 10 '18 at 10:54

2 Answers2

27

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

RobJan
  • 1,351
  • 1
  • 14
  • 18
  • somehow this causes `TypeError: 'AxesSubplot' object is not subscriptable` error, but if I assign `ax` first: `ax = df.plot(kind="bar", ...)` and `df['size'].plot(secondary_y=True, ...` everything goes right – vladkras Feb 17 '19 at 15:55
  • if I plot two columns of a pandas DataFrame at once, `df[['size','lad_cover']].plot(secondary_y=True)` does not work in this case – develarist Jan 10 '21 at 07:46
  • @develarist Try something like `df.plot(x="A", y= ["B", "C", "D"], secondary_y=["C"])`, this also allows to have multiple y values per x value – smcs Oct 26 '21 at 17:30
  • What code is pretty fine? There doesn't seem to be any. Answer now seems incomplete. Didn't work for me. Found solution here: https://stackoverflow.com/a/46063451/2455413 – markling Jul 05 '22 at 10:42
12

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.

Michel de Ruiter
  • 7,131
  • 5
  • 49
  • 74