Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib chart area vs plot area

In matplotlib, how can I control the size of plot area vs the total area of chart?

I set set the size of the chart area using the following code:

fig = plt.gcf()
fig.set_size_inches(8.11, 5.24)

However, I do not know how to set the size of plot area and as a result when I output the chart, the legend on the x-axis comes out cut in half.

like image 442
cybujan Avatar asked Oct 19 '25 01:10

cybujan


1 Answers

I think an example can help you. The figure size figsize can set the size of the window that your plot will inhabit. The axes list parameters [left, bottom, width, height] determines where in the figure the plot will exist and how much area will be covered.

So if you run the code below you will see that the window is of size 8x6 inches. Inside that window will be a main plot big_ax that takes up 0.8x0.8 of the total area. There is a second plot small_ax of size 0.3x0.3 of the total area.

import matplotlib.pyplot as plt
import numpy as np

x1 = np.random.randint(-5, 5, 50)
x2 = np.random.randn(20)

fig = plt.figure(figsize=(8,6))  # sets the window to 8 x 6 inches

# left, bottom, width, height (range 0 to 1)
# so think of width and height as a percentage of your window size
big_ax = fig.add_axes([0.1, 0.1, 0.8, 0.8]) 
small_ax = fig.add_axes([0.52, 0.15, 0.3, 0.3]) # left, bottom, width, height (range 0 to 1)

big_ax.fill_between(np.arange(len(x1)), x1, color='green', alpha=0.3)
small_ax.stem(x2)

plt.setp(small_ax.get_yticklabels(), visible=False)
plt.setp(small_ax.get_xticklabels(), visible=False)
plt.show()

enter image description here

like image 199
Scott Avatar answered Oct 21 '25 15:10

Scott