使用 Matplotlib 绘制具有多个 x 和 y 轴的图形 [英] Graph with multiple x and y axis using Matplotlib

查看:118
本文介绍了使用 Matplotlib 绘制具有多个 x 和 y 轴的图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我查看 Matplotlib 文档和其他

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.

解决方案

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()

这篇关于使用 Matplotlib 绘制具有多个 x 和 y 轴的图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆