Create subplots for two different pandas series

Viewed 40

I am trying to create subplots for two different pandas series. I want there to be two bar plots next to each other. The data has been normalized so the plots would share a y-axis but there are different values in the x-axis between plots. See below for an example of what I've tried.

df1 = pd.Series(data = [.49, .37, .25, .11, .05], index=[['stop a', 'stop b', 'stop c', 'stop d', 'stop e']])
df2 = pd.Series(data = [.65, .32, .19, .10, .01], index=[['stop a', 'stop b', 'stop d', 'stop f', 'stop h']])

fig, axes = plt.subplots(nrows=1, ncols=2, sharey=True)

df1.plot(kind='bar', ax=axes[0,0])
df2.plot(kind='bar', ax=axes[0,1])

The code returns the following error.

IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed

How can I create the two subplots to visualize the data?

1 Answers

try:

fig, axes = plt.subplots(nrows=1, ncols=2, sharey=True)
df1.plot(kind='bar', ax=axes[0])
df2.plot(kind='bar', ax=axes[1])
#since you have created 2 columns and 1 row so you have to pass only the column index
#But For instance you passed nrows=2 then you have pass row index as well as column index
#For example ax=axes[0,1] <----here 0 is row index and 1 is column index

output:

enter image description here

Related