Home > Enterprise >  matplotlib plot ruined when changing size of x-axis
matplotlib plot ruined when changing size of x-axis

Time:09-16

Suppose I want to plot a function as follows:

def f(t):
    return 1/(1-t)

def analytic_plot():
    t1 = np.arange(0, 1, 0.01)
    plt.figure()
    plt.plot(t1, f(t1))
    plt.show()

This works fine. But suppose that for arguments sake, i want the x-axis not labled as 0-1 but 0-100. In my mind it should be like this:

t2 = [x * 100 for x in t1]

But if we replot it now as:

plt.plot(t2, f(t1))

it doesn't at all do what I expect. Aren't we still mapping one element at a time from t2 to the value of f(t1)?

CodePudding user response:

Try this:

def f(t):
    return 1/(1-t)

def analytic_plot(x, y):
    plt.figure()
    plt.plot(x, y)
    plt.show()

t1 = np.arange(0, 1, 0.01)
t2 = [x * 100 for x in t1]
y = f(t1)

analytic_plot(t1, y)
analytic_plot(t2, y)

enter image description here enter image description here

CodePudding user response:

You have called the same parameters the second time around plt.plot(t2, f(t1)), to fix just change to t2 in the first parameter:

t1 = np.arange(0, 1, 0.01)
t2 = [x * 100 for x in t1]
plt.plot(t2, f(t1)) 
plt.show()

This will give you the following image (see the scale of x, 100): Img of the plot When using plt.plot(t1, f(t1)) you are going to get the normal scale: Normal plot

  • Related