Search code examples
pythonmatplotlibplotfigure

What is the equivalent of using attribute "figsize" in Line2D in Python


I am trying to take a Line2D plot and change its size to make it bigger in Python. I have tried to change the function to:

# Single 1D Discretized Brownian Motion
np.random.seed(5)
fig = plt.figure()

# Initialize the parameters        
T = 1
N = 501 # Number of points, number of subintervals = N-1
dt = T/(N-1) # Time step ()

# time units
t = np.linspace(0,T,N)

# Vectorized option (more efficient)  
dX = np.sqrt(dt) * np.random.randn(1,N)
X = np.cumsum(dX, axis=1)

plt.plot(t, X[0,:],figsize=(15,12))
plt.xlabel('$t$', fontsize=15)
plt.ylabel(' $X(t)$', fontsize=15)
plt.title('1D Discretized Brownian Path', fontsize=14)

plt.show()

the line of interest is plt.plot(t, X[0,:],figsize=(15,12)) that led to the error:

AttributeError: 'Line2D' object has no property 'figsize'

What is an alternative way to change the size of the figure? How does one increase its size in this case? I apologize in advance if this has a obvious answer, I am new to Python.


Solution

  • figsize is a property of matplotlib.figure.Figures. There are a number of ways to set it (see this question) but the easiest in this case is probably to add

    plt.figure(figsize=(15,12))
    

    before the call to plt.plot, i.e.

    # ...
    plt.figure(figsize=(15,12))
    plt.plot(t, X[0,:])
    # ...
    

    This will create a Figure instance with the specified size and set that instance to be the 'current' figure - which is what plt.plot will use.