0

I have a Pandas dataframe with four columns; the first is a centretype (x axis) and I want the rest of the columns to be displayed as side by side columns for each centretype by year. I am not sure what I'm doing wrong, as I am getting only the values for 2019 but I want 2018 and 2017 displayed too.

enter image description here

enter image description here

df = c2

colors = ['steelblue','forestgreen','salmon']

ax = df.plot(kind='bar', color=colors, width=0.7, align='center', stacked=False, rot=60, figsize=(12,6), legend=False, zorder=3)


from matplotlib.ticker import FuncFormatter, MaxNLocator
ax.yaxis.set_major_locator(MaxNLocator(integer=True))

plt.grid(zorder=0)
#plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.2))
plt.xlabel("Type of centre")
plt.ylabel("Number")
plt.title("")

ax.spines['bottom'].set_color('gainsboro')
ax.spines['top'].set_color('white') 
ax.spines['right'].set_color('white')
ax.spines['left'].set_color('gainsboro')

ax.yaxis.label.set_color('black')
ax.xaxis.label.set_color('black')

ax.title.set_color('black')

ax.patch.set_facecolor('white')

plt.savefig('images/figure1.png', dpi=300, facecolor=ax.get_facecolor(), transparent=True,  bbox_inches='tight', pad_inches=0.1)
plt.show()
data.shape
1
  • If I define df like df = pd.DataFrame(np.random.rand(5,3), columns=[2018, 2019, 2020], index=list("ABCDE")) and run your code I get the desired output, namely this. This means that you're either not using the data you think you do, or that you're using a specific pandas version with a bug in it. Commented Jan 20, 2020 at 17:14

1 Answer 1

1

The reason that your picture contains just one column is df = c2. Apparently c2 contains only one of columns from your DataFrame. Delete this instruction.

As a test, I performed just:

df.plot.bar(width=0.7, rot=60, figsize=(12,6), legend=False);

getting the following proper picture, with 5 sets of bars, 3 bars each:

enter image description here

Start from just the above instruction and then decide which remaining instruction should actually be executed.

In my opinion, the omitted parameters are not needed, because they pass their respective default values.

Note also the semicolon at the end. Working under Jupyter Notebook it is required to render just the picture. Otherwise (without the terminating semicolon), <matplotlib.axes._subplots.AxesSubplot at ...> is printed over the picture.

Maybe you should also drop legend=False (stay with default True value and print the legend). Otherwise it is not clear what particular bars refer to.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.