get number base of graph axis

Viewed 45

I need to obtain the numerical base of the "y" axis of the graph to save it in a variable and be able to operate with it later, in this case it is "1e-6", but the reality is that it always varies since I am working with files. csv with more than one hundred columns and thousands of data per column. I made this code separately just so we can focus on the main problem.

import numpy as np
import matplotlib.pyplot as plt


x = [1,2,3,4,5]
y = [0.000001,0.000002,0.000003,0.000004,0.000005]

fig, axs = plt.subplots(nrows=1, ncols=1, figsize=(8, 4))
axs.plot(x,y)

"""

base_number = axs.get_base_number() -> Is there any such method to get the foundation? 

"""

fig.tight_layout()
plt.show()

matplotlib figure

2 Answers

You can extract the offset_text:

import numpy as np
import matplotlib.pyplot as plt

x = [1,2,3,4,5]
y = [0.000001,0.000002,0.000003,0.000004,0.000005]
fig, axs = plt.subplots(nrows=1, ncols=1, figsize=(8, 4))
axs.plot(x,y)
plt.tight_layout()
exponent = axs.yaxis.get_offset_text().get_text()
print('exponent:', int(exponent.split('e−')[1]))

output: 6

NB. you need to ensure the plot is drawn first, this is why I applied tight_layout, you can also call fig.canvas.draw()

other approach:

Use the ticks/tickslabels:

t = axs.get_yticklabels()[-1]
float(t.get_text())/t.get_position()[1]

output: 1000000

The 1e-6 is actually just the min(y) of the data you have. if you mean the minimum value the plot can display then ymax, ymin = axs.get_ylim() might be what you need.

Related