Apply scaling to y axis

Viewed 126

I have following image:

import numpy as np
import matplotlib.pyplot as plt

data = [40, 50, 80, 86, 88, 89, 80, 90, 88, 87, 89, 90, 89, 90, 87, 88, 89, 90]
fig, ax = plt.subplots()
# ax.set_yscale("log")
ax.plot(data)
plt.show()

enter image description here

Now I want to apply a scale on the y axis, to better see the difference of the high values. I tried to set_yscale to log, but that didn´t change anything.

2 Answers

So I solved my issue using custom yscale functions. See https://matplotlib.org/stable/gallery/scales/scales.html

def forward(x):
    return 10**x

def inverse(x):
    return np.log(x)

data = [40, 50, 80, 86, 88, 89, 80, 90, 88, 87, 89, 90, 89, 90, 87, 88, 89, 90]
data = [d / 100 for d in data]
fig, ax = plt.subplots()
ax.set_yscale('function', functions=(forward, inverse))
ax.set_ylim([0.4,1])
ax.yaxis.set_major_locator(FixedLocator(np.arange(0.4, 1.01, 0.05)))
ax.grid()
ax.plot(data)
plt.show()

Procuding following plot: enter image description here

If you are looking for zoom functionality over x-axis and y-axis, you can try this, it will help you zoom in and out over both x-axis and y-axis,

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.axis import Axis

data = [40, 50, 80, 86, 88, 89, 80, 90, 88, 87, 89, 90, 89, 90, 87, 88, 89, 90]
fig, ax = plt.subplots()
# ax.set_yscale("log")
ax.plot(data)
ax.yaxis.zoom(-1)
ax.xaxis.zoom(2)
plt.show()

After zoom

Related