I would like to have two lines (or better scatter plots) in one plot.
The Secondary Y line should be in log scale. How to do it with python matplotlib?
You can create a second y axis by using ax2 = ax.twinx()
. You can then, as tacaswell pointed out in the comments, set this second axis to log scale.
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize=(5,3))
ax = fig.add_subplot(111)
ax2 = ax.twinx()
x = np.random.rand(10)
y = np.random.rand(10)
y2 = np.random.randint(1,10000, size=10)
l1 = ax.scatter(x,y, c="b", label="lin")
l2 = ax2.scatter(x,y2, c="r", label="log")
ax2.set_yscale("log")
ax2.legend(handles=[l1, l2])
ax.set_ylabel("Linear axis")
ax2.set_ylabel("Logarithmic axis")
plt.tight_layout()
plt.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With