Julian
Julian

Reputation: 603

Matplotlib: How to assign correct y-axis scale to data?

I can create this plot:

import matplotlib.pyplot as plt

x = range(1, 16)
zero = [0] * 15
pointfive = [0.5] * 15

fig = plt.figure()

ax1 = fig.add_subplot(111)
ax1.plot(x, days, 'moccasin')
ax1.set_ylabel('days', color='moccasin')

ax2 = ax1.twinx()
ax2.plot(x, probs, "turquoise")
ax2.set_ylabel('probabilities', color="turquoise")

ax3 = fig.add_subplot(111)
ax3.plot(x, zero , "--", color="moccasin")

ax4 = fig.add_subplot(111)
ax4.plot(x, pointfive , "--", color="turquoise")

fig.text(0.5, 0.04, 'days to lat, ha='center')

enter image description here

These to things I want changes:

This illustrates my desired changes:

enter image description here

How do I do that? Thanks!

Upvotes: 2

Views: 112

Answers (1)

Diziet Asahi
Diziet Asahi

Reputation: 40697

You've defined your axis ax1 (left) and ax2 (right). Simply use these to choose the axis you want to plot:

x = range(1, 16)
y1 = 10*np.random.random(size=len(x))
y2 = np.random.random(size=len(x))
zero = [0] * 15
pointfive = [0.5] * 15

fig = plt.figure()

ax1 = fig.add_subplot(111)
ax1.plot(x, y1, 'moccasin')
ax1.set_ylabel('days', color='moccasin')

ax2 = ax1.twinx()
ax2.plot(x, y2, "turquoise")
ax2.set_ylabel('probabilities', color="turquoise")


ax1.plot(x, zero , "--", color="moccasin")
ax2.plot(x, pointfive , "--", color="turquoise")

ax1.set_xlabel('days to lat')

ax1.set_title('TITLE')

Upvotes: 3

Related Questions