Giving a Y-Scale to matplotlib

Viewed 77

I have two axes. I want to plot 4e-6 in ax1 and plot 4e-7 in ax2. How can I make y-axis of ax1 like y-axis of ax2? i.e, it should be scaled to 1e-6 and the scale should appear at the top of the y-axis.

Here is the code:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(figsize=(10,5))
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
x = np.linspace(0, 1000, 3)
y1 = np.full_like(x, 4e-6)
y2 = np.full_like(x, 4e-7)

ax1.plot(x, y1)
ax2.plot(x, y2)
plt.show()

Here is the figure:

enter image description here

1 Answers

This can be achieved as shown here: default_tick_formatter

Mentioning the y_axis value as 4*1e-7 instead of 4e-7, will result in the solution to your requirement.

The following was recreated and the solution is produced below:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(figsize=(10,5))
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
x = np.linspace(0, 1000, 3)
y1 = np.full_like(x, 4*1e-6)
y2 = np.full_like(x, 4*1e-7)

ax1.plot(x, y1)
ax2.plot(x, y2)
plt.show()

The results would lookresult

Related