Matplotlib.pyplot: How To Set Up A Second Y-axis For An Existing Plot
I have two sets of values that are linearly dependent. Therefore I just need a single graph with a second y-axis in the right scale. What is the most elegant way to do this? Maki
Solution 1:
If you don't want to call bar
twice and only want the second y axis to provide a conversion, then simply don't call bar at all the second time. You can still create and adjust the second y axis without actually plotting anything on it.
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(4)
y2 = np.array([23, 32, 24, 28])
y1 = 4.2 * y2
y2max = np.max(y2) * 1.1
fig = plt.figure(1, figsize=(6,6))
ax = fig.add_subplot(111)
ax.bar(x, y2)
ax.set_ylabel('Consumption in $m^3$')
ax2 = ax.twinx()
ax2.set_ylabel('Consumption in EUR')
ax.set_ylim(ymax=y2max)
ax2.set_ylim(ymax=4.2*y2max)
Post a Comment for "Matplotlib.pyplot: How To Set Up A Second Y-axis For An Existing Plot"