问题
As I checked with the Matplotlib document and other resources. I got that when multiple axis were created they are not depend on each other and we can plot the multiple line as per different axis. But I need to plot the graph like two Y-axis contains with the temperature as (Celsius and Fahrenheit) and need to plot only one single line related to that so with 1st axis user able to check Celsius and with 2nd axis Fahrenheit. with x-axis as range (1 -24 hr).
Suggestions are most welcome.
回答1:
use twinx()
to show two axes, and convert the y_limits of ax1
in Celsius to y_limits of ax2
in Fahrenheit:
import matplotlib.pyplot as plt
from random import randint
x = range(1,24)
y = [randint(0,75) for x in x]
fig, ax1 = plt.subplots()
ax1.plot(x,y)
y_lim = ax1.get_ylim()
y2_lim = [x*9/5 + 32 for x in y_lim]
ax2 = ax1.twinx()
ax2.set_ylim(y2_lim)
ax1.set_ylabel('deg C')
ax2.set_ylabel('deg F')
plt.show()
来源:https://stackoverflow.com/questions/42506819/graph-with-multiple-x-and-y-axis-using-matplotlib