Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib: How to change figsize for double bar plot

I have plotted a double bar plot in matplotlib using the following code:

x = pd.Series(range(12))
y = self.cust_data['Cluster_ID'].value_counts().sort_index()
z = self.cust_data['Cluster_ID_NEW'].value_counts().sort_index()
plt.bar(x + 0.0, y, color = 'b', width = 0.5)
plt.bar(x + 0.5, z, color = 'g', width = 0.5)
plt.legend(['Old Cluster', 'New Cluster'])
plt.savefig("C:\\Users\\utkarsh.a.ranjan\\Documents\\pyqt_data\\generate_results\\bar", bbox_inches='tight',pad_inches=0.1)
plt.clf()

I want to use the figsize parameter to make the resultant plot bigger in size. This is easy when plotting a single bar plot, but here I am confused as to where to put the figsize parameter.

like image 761
Utkarsh Ranjan Avatar asked Jun 28 '18 10:06

Utkarsh Ranjan


People also ask

How do you change Figsize?

Set the figsize Argument First off, the easiest way to change the size of a figure is to use the figsize argument. You can use this argument either in Pyplot's initialization or on an existing Figure object. Here, we've explicitly assigned the return value of the figure() function to a Figure object.

How do you change the size of a bar plot?

To set width for bars in a Bar Plot using Matplotlib PyPlot API, call matplotlib. pyplot. bar() function, and pass required width value to width parameter of bar() function. The default value for width parameter is 0.8.


3 Answers

The best way to do that follows a more OO approach:

fig, ax = plt.subplots(figsize=(12,12))
ax.bar(x + 0.0, y, color = 'b', width = 0.5)
ax.bar(x + 0.5, z, color = 'g', width = 0.5)
ax.legend(['Old Cluster', 'New Cluster'])
fig.savefig("C:\\Users\\utkarsh.a.ranjan\\Documents\\pyqt_data\\generate_results\\bar", bbox_inches='tight',pad_inches=0.1)
plt.clf()

You also might want to add format to your filename. If not, the format is taken from your rc parameter savefig.format, which is usually png.

BTW, if you want to stick to your code as much as possible, you can also add the line before your plt.bar(...):

plt.figure(figsize=(12,12))
like image 139
westr Avatar answered Oct 23 '22 11:10

westr


A very simple way to increase plot size is to simply add this line of code before your code:

plt.rcParams["figure.figsize"] = (10, 5)

You can change the figure size according to your wish.

like image 23
Navya Jain Avatar answered Oct 23 '22 12:10

Navya Jain


You could set the size with figsize

import matplotlib.pyplot as plt

f, ax = plt.subplots(figsize=(18,5)) # set the size that you'd like (width, height)
plt.bar([1,2,3,4], [0.1,0.2,0.3,0.4], label = 'first bar')
plt.bar([10,11,12,13], [0.4,0.3,0.2,0.1], label = 'second bar')
ax.legend(fontsize = 14)

enter image description here

like image 30
nahusznaj Avatar answered Oct 23 '22 11:10

nahusznaj