Graph with multiple x and y axis using Matplotlib

女生的网名这么多〃 提交于 2021-01-27 23:12:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!