Reputation: 31
I am able to plot the data set below when it only has the last two columns (the GDP per year and the population value) but I want to learn how to plot it to also include the year.
suicides_gdp = suicides_russia.groupby(["year", " gdp_for_year ($) "])["suicides_no"].sum()
suicides_gdp
year gdp_for_year ($)
1989 506,500,173,960 37921
1990 516,814,274,022 39028
1991 517,962,962,963 39281
1992 460,290,556,901 45923
1993 435,083,713,851 55846
1994 395,077,301,248 61420
1995 395,531,066,563 60548
1996 391,719,993,757 57511
1997 404,926,534,140 54746
1998 270,953,116,950 51518
1999 195,905,767,669 56974
2000 259,708,496,267 56619
2001 306,602,673,980 56958
2002 345,110,438,692 55024
2003 430,347,770,732 51445
2004 591,016,690,743 49096
2005 764,017,107,992 45802
2006 989,930,542,279 42614
2007 1,299,705,247,686 41149
2008 1,660,844,408,500 38211
2009 1,222,643,696,992 37408
2010 1,524,916,112,079 33356
2011 2,051,661,732,060 31038
2012 2,210,256,976,945 29643
2013 2,297,128,039,058 28690
2014 2,063,662,665,172 26541
2015 1,368,400,705,491 25432
I tried plt.plot(suicides_gdp.index, suicides_gdp.values)
and plt.barh(x="suicides_no", y=["year", " gdp_for_year ($) "], width=5)
but I get the following errors respectively:
ValueError: setting an array element with a sequence.
for the line plot and TypeError: bar() got multiple values for keyword argument 'x'
for the horizontal bar chart.
How can I plot the following data set using either a line plot or bar chart?
Upvotes: 0
Views: 43
Reputation: 150745
I would plot bar
, instead of barh
. Also, since the two columns have different scales, it's best to plot them in twin axes:
suicides_gdp = suicides_gdp.reset_index()
fig, ax = plt.subplots(figsize=(12,6))
ax2 = ax.twinx()
ax2.bar(suicides_gdp['year'], suicides_gdp['suicides_no'],
color='C1', alpha=0.5)
ax.plot(suicides_gdp['year'], suicides_gdp['gdp_for_year ($)'], zorder=100)
plt.show()
Output
Upvotes: 2
Reputation: 1572
As you saw already, the function barh
only two arguments which means two dimensions. That means it allows to represent only two columns. If you want to represent three columns, you have two options :
Upvotes: 1