Python matplotlib 绘制双Y轴曲线图的示例代码
作者:松鼠爱出饼干 时间:2023-04-14 11:07:27
Matplotlib简介
Matplotlib是非常强大的python画图工具
Matplotlib可以画图线图、散点图、等高线图、条形图、柱形图、3D图形、图形动画等。
Matplotlib安装
pip3 install matplotlib#python3
双X轴的
可以理解为共享y轴
ax1=ax.twiny()
ax1=plt.twiny()
双Y轴的
可以理解为共享x轴
ax1=ax.twinx()
ax1=plt.twinx()
自动生成一个例子
x = np.arange(0., np.e, 0.01)
y1 = np.exp(-x)
y2 = np.log(x)
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(x, y1)
ax1.set_ylabel('Y values for exp(-x)')
ax1.set_title("Double Y axis")
ax2 = ax1.twinx() # this is the important function
ax2.plot(x, y2, 'r')
ax2.set_xlim([0, np.e])
ax2.set_ylabel('Y values for ln(x)')
ax2.set_xlabel('Same X for both exp(-x) and ln(x)')
plt.show()
例子:画了一个双y轴坐标的图表
# -*- coding: utf-8 -*-
#调用包
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#读取文件
io=r'E:\工作\专项\白骑士数据验证\白骑士数据汇总表.xlsx'
yinka=pd.read_excel(io,sheet_name='YINKA_sample')
bqs=pd.read_excel(io,sheet_name='BQS_result')
yinka_bqs=pd.merge(yinka,bqs,left_on='no',right_on='no',how='inner')
#绘图
fig,ax=plt.subplots(1,1,figsize=(20, 300))
ax.grid() #画网格
x=total.index-1
#为什么+1,因为对不齐,所以使用时根据情况编写
y=total['var1']
ax.plot(x,y,'k--o',alpha=0.5) #画折线图
ax.set_xlim([0,16])
#设置x轴的取值范围 这个可以让x轴与y轴的起点一致
ax.set_xticks(np.arange(0,16)) #设置x轴的刻度范围
ax.set_xticklabels(np.arange(0,16),rotation=30)
#设置x轴上的刻度
ax.set_ylim([0,1800]) #同理y轴数值范围
ax.set_yticks(range(0,1800,300))#设置y轴的刻度范围
ax.set_yticklabels(range(0,1800,300))#设置y轴上的刻度
ax.legend(loc='upper left') #设置ax子图的图例(legend)
#新知识点
for a,b in zip(x,y): #设置注释 zip函数是对应关系
ax.text(a,b,b,ha='center',va='bottom',fontsize=15)
#重点
ax1=ax.twinx()
#这个是能够实现双y轴的重点,共享x轴;还有一种是双x轴的图表换成ax.twiny()
y1=total[['adopt','reject']]
y1.plot.bar(ax=ax1,alpha=0.5)
#这个是matplotlib中条形图的绘制方法,如果使用seaborn绘制方法使用sns.barplot()函数,需要调整很多细节
#这里只设置了y轴的刻度,x轴的刻度设置了一下偶尔会出现失败,值得注意的是要将数据对齐
ax1.set_ylim([0,1800])
ax1.set_yticks(range(0,1800,300))
ax1.set_yticklabels(range(0,1800,300))
for e,f,w in zip(data_.index,data_[0],data_[1]):
ax1.text(e-1,f,f,ha='center',va='bottom',fontsize=10,color='b')
ax1.text(e-1,w,w,ha='center',va='bottom',fontsize=10,color='g')
ax1.legend(loc='best')
plt.show() #养成习惯这个最好写一下#
#保存图片
plt.savefig('path') #图表输出到本地
结果显示:
来源:https://www.cnblogs.com/hhh188764/p/13099113.html
标签:Python,matplotlib,曲线图
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python sklearn 中的 make_blobs() 函数示例详解
2022-07-24 21:45:48
从web到内网渗透的一次过程详解
2023-05-20 21:23:08
![](https://img.aspxhome.com/file/2023/7/83267_0s.jpg)
django开发post接口简单案例,获取参数值的方法
2022-10-20 06:45:00
![](https://img.aspxhome.com/file/2023/6/109386_0s.jpg)
Dreamweaver MX新功能试用:连续空格
2008-01-06 21:03:00
![](https://img.aspxhome.com/file/uploadpic/20072/200723113527171.jpg)
Django配置MySQL数据库的完整步骤
2023-07-19 14:43:47
![](https://img.aspxhome.com/file/2023/9/64249_0s.png)
MySQL常见错误提示及解决方法
2008-02-23 10:08:00
python如何变换环境
2021-06-02 19:19:44
![](https://img.aspxhome.com/file/2023/7/118647_0s.png)
Go语言resty http包调用jenkins api实例
2024-05-21 10:27:27
![](https://img.aspxhome.com/file/2023/4/123974_0s.jpg)
python使用xmlrpclib模块实现对百度google的ping功能
2023-05-06 19:02:21
Python实现Tracert追踪TTL值的方法详解
2023-06-24 13:08:14
![](https://img.aspxhome.com/file/2023/5/66405_0s.png)
golang coroutine 的等待与死锁用法
2024-04-30 10:03:34
vue实现二维码扫码功能(带样式)
2024-04-10 10:31:39
![](https://img.aspxhome.com/file/2023/2/139772_0s.png)
ie和firefox中css自动换行实现方法
2008-04-08 12:49:00
python链接sqlite数据库的详细代码实例
2024-01-20 12:17:00
![](https://img.aspxhome.com/file/2023/7/77767_0s.png)
MySQL 中查找含有目标字段的表的方法
2024-01-12 16:45:20
几个SQL SERVER应用问题解答
2008-01-01 19:12:00
Vue路由模式中的hash和history模式详细介绍
2024-06-07 15:20:16
![](https://img.aspxhome.com/file/2023/2/123022_0s.gif)
MySQL中把varchar类型转为date类型方法详解
2024-01-27 03:15:01
![](https://img.aspxhome.com/file/2023/7/111397_0s.png)
基于Python OpenCV实现图像的覆盖
2022-09-12 22:05:02
![](https://img.aspxhome.com/file/2023/2/99132_0s.jpg)
selenium3.0+python之环境搭建的方法步骤
2023-03-20 06:00:22
![](https://img.aspxhome.com/file/2023/7/96977_0s.png)