Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to fix the screen size for a matplotlib plot

I am trying to plot a tangent function (see below) and am trying to stop the plot ‘jumping around’:

import numpy as np
import matplotlib.pyplot as plt

lowerBound = 0

plt.ion()

while True:
    x = np.arange(lowerBound, lowerBound + 50, 0.1)
    y = np.tan(x / 2)

    plt.cla()
    plt.plot(x, y)
    plt.pause(0.01)

    lowerBound += 1

I tried

plt.figure(figsize=(5, 4))

and

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

fig = plt.figure()
fig.set_figheight(4)

but they just made the plot smaller, instead of fixing the height.

I was thinking I could do:

set y to maximum of -200 and y and set y to minimum of 200 and y,
but I don’t know how to get the min/max for all items in an array.

>Solution :

To stop the plot from "jumping around" as you describe, you can use the ylim method to set the limits of the y-axis in your plot. This will prevent the y-axis from adjusting automatically as the data changes, which will help prevent the plot from "jumping around".

Here is an example of how you can use the ylim method to set the y-axis limits in your plot:

import numpy as np
import matplotlib.pyplot as plt

lowerBound = 0

plt.ion()

while True:
    x = np.arange(lowerBound, lowerBound + 50, 0.1)
    y = np.tan(x / 2)

    # Set the y-axis limits to [-200, 200]
    plt.ylim(-200, 200)

    plt.cla()
    plt.plot(x, y)
    plt.pause(0.01)

    lowerBound += 1

In this example, the ylim method is called before the plot method to set the y-axis limits to [-200, 200]. This will prevent the y-axis from adjusting automatically as the data changes, which will help prevent the plot from "jumping around".

Alternatively, if you want to use the min and max functions to set the y-axis limits based on the minimum and maximum values in the y array, you can use the min and max functions like this:

import numpy as np
import matplotlib.pyplot as plt

lowerBound = 0

plt.ion()

while True:
    x = np.arange(lowerBound, lowerBound + 50, 0.1)
    y = np.tan(x / 2)

    # Set the y-axis limits to the minimum and maximum values in the y array
    plt.ylim(min(y), max(y))

    plt.cla()
    plt.plot(x, y)
    plt.pause(0.01)

    lowerBound += 1

In this example, the ylim method is called before the plot method to set the y-axis limits to the minimum and maximum values in the y array. This will prevent the y-axis from adjusting automatically as the data changes, which will help prevent the plot from "jumping around".

You can adjust the y-axis limits to suit your specific requirements and preferences. By setting the y-axis limits, you can control the appearance of your plot and prevent it from "jumping around" as the data changes.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading