0

I am pretty new to using Matplotlib

I couldn't figure out how to apply the things I found to my own graph, so I decided to make my own post

I use this code to generate my bar chart:

p = (len(dfrapport.index))

p1 = p * 2.5
p2 = p * 1.5

height = dfrapport['aantal']
bars = dfrapport['soort']
y_pos = np.arange(len(bars))


plt.bar(y_pos, height, color = ['black', 'red','orange', 'yellow', 'green', 'blue', 'cyan'])

plt.title('Aantal noodstoppen per categorie')
plt.xlabel('categorieën')
plt.ylabel('aantal')
plt.tick_params(axis='x', which='major', labelsize=p2)

plt.xticks(y_pos, bars)
plt.show()

But I don't understand how to change the size of the plot? because when I use plt.figure(figsize=(p1,p2))

I get an empty plot with the correct labels ( But it does apply the size to the piechart i create later? ) And the barchart i originally wanted to create has basic 1-8 labels.

I want to change the size based on the amount of bars are created because sometimes the data I use doesn't contain one of the categories.

2 Answers2

1

With as few changes as humanly possible to your current code, the way to do this is to add the following line right after you define p1 and p2:

plt.gcf().set_size_inches(p1,p2)

The above will set the size of the current Figure object that pyplot is using to make plots. In the future, you may way to switch over to using the Axes-based interface to Matplotlib, as it's much more powerful and flexible in general:

p = (len(dfrapport.index))

p1 = p * 2.5
p2 = p * 1.5

height = dfrapport['aantal']
bars = dfrapport['soort']
y_pos = np.arange(len(bars))

fig = plt.figure(figsize=(p1,p2))
ax = fig.gca()
ax.bar(y_pos, height, color = ['black', 'red','orange', 'yellow', 'green', 'blue', 'cyan'])

ax.set_title('Aantal noodstoppen per categorie')
ax.set_xlabel('categorieën')
ax.set_ylabel('aantal')
ax.xaxis.set_tick_params(which='major', labelsize=p2)

ax.set_xticks(y_pos, bars)
fig.show()
tel
  • 13,005
  • 2
  • 44
  • 62
  • Thanks! I used a random guide to generate the chart the first time. I started reading this after your first answer: https://pandas.pydata.org/pandas-docs/version/0.23/generated/pandas.DataFrame.plot.bar.html But you already edited my code haha. – Martijn van Amsterdam Dec 18 '18 at 15:29
1

plt.figure(figsize=(p1,p2)) is the correct approach. The question is hence a bit unclear, because you just need to put it in your code, e.g.

p = (len(dfrapport.index))
p1 = p * 2.5
p2 = p * 1.5
plt.figure(figsize=(p1,p2))

# ...

plt.bar(...)

This is also shown in the question: How do you change the size of figures drawn with matplotlib?

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • My understanding was that `plt.show()` would display the `plt.figure` and like I said, i get an empty plot with the correct labels. If i use `plt.figure` in combination with `plt.bar()` instead of `plt.show()` it would work? – Martijn van Amsterdam Dec 18 '18 at 15:34
  • @MartijnvanAmsterdam You might have been putting the `plt.figure(figsize=(p1,p2))` line after your `plt.bar...` line. That would cause the current figure to get reset, which would explain why you were seeing an empty plot. ImportanceOfBeingErnest is right that if you put the `plt.figure` line above your plotting code, it should work fine. – tel Dec 18 '18 at 15:41
  • Ah I understand now, yes i did place it under my `plt.bar...` but above my `plt.show()` – Martijn van Amsterdam Dec 18 '18 at 15:50