Search code examples
pythonmatplotlibgraphing

Plotting exponential graphs in matplotlib - ytick spacing


I'm fairly new to matplotlib and graphing in general, so I'm sorry if this problem sounds trivial.

I'm trying to plot a line graph with following x and y values.

x_values = [18, 19, 20, 21, 22, 23, 24]

y_values = [6, 14, 39, 124, 553, 1718, 5524]

The problem is, since the y_values are exponential, you can't really see the lower values on the graph. I've tried to play around with yticks, but the proportions stay the same. Is there any way to plot this, so you can see all the values? I would love to have something like fixed spacing between yticks or something similar.

My current code:

import numpy as np
import matplotlib.pyplot as plt

x_values = [18, 19, 20, 21, 22, 23, 24]
y_values = [6, 14, 39, 124, 553, 1718, 5524]

plt.plot(np.array(x_values), np.array(y_values))
plt.xticks(np.arange(18, 25, 1), ('18', '+19', '+20', '+21', '+22', '+23', '+24'))
plt.grid()
plt.show()

plot

plot

plt.yticks(np.concatenate((np.arange(0, 175, 25), [553], [1718], [5524]), axis=None))

This would be my goal, but with better y-axis proportions.


Solution

  • Perhaps you're just looking for a log scale?

    plt.plot(x_values, y_values)
    plt.yscale("log")
    plt.yticks(y_values, y_values)
    plt.show()
    

    enter image description here