迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Python >

Matplotlib 中的 twinx 和 twiny

作者:迹忆客 最近更新:2023/03/17 浏览次数:

本篇文章介绍了我们如何在 Python Matplotlib 中使用 matplotlib.axes.Axes.twinx()matplotlib.axes.Axes.twiny() 创建具有共同 X 轴或 Y 轴的双轴。

在 Python Matplotlib 中使用 matplotlib.axes.Axes.twinx()

函数 matplotlib.axes.Axes.twinx() 在 Matplotlib 图中创建与初始轴共享 X 轴的其他轴。

import matplotlib.pyplot as plt 

students=["Anil","Sohit","Hrishav","Ayush","Sunil"]
heights_in_cms=[165,160,140,150,130]

fig,axes=plt.subplots()
fig.set_size_inches(8, 6)
axes.bar(students,heights_in_cms)
y1, y2 = axes.get_ylim() 
axes.set_xlabel("Students",fontsize=12)
axes.set_ylabel("Height in cms",fontsize=12)

twin_axes=axes.twinx() 
twin_axes.set_ylim(y1*0.394,y2*0.394)
twin_axes.set_ylabel("Height in Inches",fontsize=12)

fig.suptitle("Plot using matplotlib.axes.Axes.twinx()",fontsize=15)
plt.show()

输出:

使用 matplotlib.axes.Axes.twinx()进行绘图

它创建了一个学生身高的条形图。左边的 Y 轴标签代表学生的身高,单位是 cm,右边的 Y 轴标签代表学生的身高,单位是 inches

在这种情况下,我们创建一个新的轴 twin_axes,与 axes 共享 X 轴。axes 的 Y 轴的标签设置为 Height in cms,而 twin_axesY 轴设置为 Height in Inches

Matplotlib Python 中的 matplotlib.axes.Axes.twiny()

函数 matplotlib.axes.Axes.twiny() 在 Matplotlib 图中创建与初始轴共享 Y 轴的其他轴。

import matplotlib.pyplot as plt 

distance_in_kms=[10,20,30,40,50]
fare_in_dollars=[2,3.5,5,7,10]

fig,axes=plt.subplots()
fig.set_size_inches(10, 8)
axes.plot(distance_in_kms,fare_in_dollars)
x1, x2 = axes.get_xlim() 
axes.set_xlabel("Distance in kms",fontsize=12)
axes.set_ylabel("Fare ($)",fontsize=12)

twin_axes=axes.twiny() 
twin_axes.set_xlim(x1*0.62,x2*0.62)
twin_axes.set_xlabel("Distance in miles",fontsize=12)

fig.suptitle("Plot using matplotlib.axes.Axes.twiny()",fontsize=15)
plt.show()

输出:

使用 matplotlib.axes.Axes.twiny()进行绘图

我们创建一个新的轴 twin_axes,与 axes 共享 Y 轴。axes 的 X 轴的标签设置为 Distance in kms,而 twin_axes 的 X 轴设置为 Distance in miles

Matplotlib 中一起使用 twinx()twiny()

import matplotlib.pyplot as plt 

distance_in_kms=[10,20,30,40,50]
fare_in_dollars=[2,3.5,5,7,10]

fig,axes=plt.subplots()
fig.set_size_inches(10, 8)
axes.plot(distance_in_kms,fare_in_dollars)
x1, x2 = axes.get_xlim() 
y1, y2 = axes.get_ylim() 
axes.set_xlabel("Distance in kms",fontsize=12)
axes.set_ylabel("Fare ($)",fontsize=12)

twin_axes=axes.twinx().twiny()

twin_axes.set_ylim(y1*0.85,y2*0.85)
twin_axes.set_ylabel("Fare in Euros",fontsize=12)

twin_axes.set_xlim(x1*0.62,x2*0.62)
twin_axes.set_xlabel("Distance in miles",fontsize=12)

fig.suptitle("Matplotlib use twinx() and twiny() together",fontsize=15)
plt.show()

输出:

Matplotlib 同时使用 twinx()和 twiny()

它创建一个 Matplotlib 图,图的四边都有刻度线。axes'将控制左边的 X 轴和底部的 Y 轴,而 twin_axes’将控制右边的 X 轴和顶部的 Y 轴。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Pandas read_csv()函数

发布时间:2024/04/24 浏览次数:254 分类:Python

Pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 DataFrame 中。

Pandas 追加数据到 CSV 中

发布时间:2024/04/24 浏览次数:352 分类:Python

本教程演示了如何在追加模式下使用 to_csv()向现有的 CSV 文件添加数据。

Pandas 多列合并

发布时间:2024/04/24 浏览次数:628 分类:Python

本教程介绍了如何在 Pandas 中使用 DataFrame.merge()方法合并两个 DataFrames。

Pandas loc vs iloc

发布时间:2024/04/24 浏览次数:837 分类:Python

本教程介绍了如何使用 Python 中的 loc 和 iloc 从 Pandas DataFrame 中过滤数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便