Python利用matplotlib做图中图及次坐标轴
2017-12-20 11:54
781 查看
图中图
准备数据import matplotlib.pyplot as plt fig = plt.figure() x = [1, 2, 3, 4, 5, 6, 7] y = [1, 3, 4, 2, 5, 8, 6]
- 大图
首先确定大图左下角的位置以及宽高:
注意,4个值都是占整个figure坐标系的百分比。在这里,假设figure的大小是10x10,那么大图就被包含在由(1, 1)开始,宽8,高8的坐标系内。
# below are all percentage left, bottom, width, height = 0.1, 0.1, 0.8, 0.8 ax1 = fig.add_axes([left, bottom, width, height]) # main axes ax1.plot(x, y, 'r') ax1.set_xlabel('x') ax1.set_ylabel('y') ax1.set_title('title')
将大图坐标系添加到figure中,颜色为r(red),取名为title:
- 小图
接着,我们来绘制左上角的小图,步骤和绘制大图一样,注意坐标系位置和大小的改变
ax2 = fig.add_axes([0.2, 0.6, 0.25, 0.25]) # inside axes ax2.plot(y, x, 'b') ax2.set_xlabel('x') ax2.set_ylabel('y') ax2.set_title('title inside 1')
最后,我们来绘制一个右下角的小图。这里我们采用一种更简单方法,即直接往plt里添加新的坐标系
# different method to add axes #################################### plt.axes([0.6, 0.2, 0.25, 0.25]) plt.plot(y[::-1], x, 'g') plt.xlabel('x') plt.ylabel('y') plt.title('title inside 2')
注意对y进行了逆序处理
次坐标轴
有时候我们会用到次坐标轴,即在同个图上有第2个y轴存在。同样可以用matplotlib做到第一个Y 坐标
import matplotlib.pyplot as plt import numpy as np x = np.arange(0, 10, 0.1) y1 = 0.05 * x**2 y2 = -1 *y1 fig, ax1 = plt.subplots()
第二个坐标轴
对ax1调用twinx()方法,生成如同镜面效果后的ax2
接着进行绘图, 将 y1, y2 分别画在 ax1, ax2 上:
ax2 = ax1.twinx() # mirror the ax1 ax1.plot(x, y1, 'g-') ax2.plot(x, y2, 'b-') ax1.set_xlabel('X data') ax1.set_ylabel('Y1 data', color='g') ax2.set_ylabel('Y2 data', color='b') plt.show()
同理:改变y2生成图像
相关文章推荐
- Python利用matplotlib.pyplot绘图时如何设置坐标轴刻度
- python中利用matplotlib.patches在一个坐标轴上添加画的图形
- Python 利用matplotlib绘制热力图 correlation heatmap X,Y 坐标轴字体重叠显示问题,将字体进行旋转
- Python实战小程序利用matplotlib模块画图代码分享
- 利用python中matplotlib打印混淆矩阵
- Python 中利用 matplotlib 进行曲线的部分填充 fill_betweenx
- python matplotlib绘图,修改坐标轴刻度为文字的实例
- python matplotlib 2D 散点图的制作(输入二维坐标)
- 利用python的matplotlib绘制分布图
- Python进阶(三十八)-数据可视化の利用matplotlib 进行折线图,直方图和饼图的绘制
- python中Matplotlib的坐标轴的坐标区间的设定
- 利用Python中的matplotlib模块抓取yahoo finance里的历史数据并绘图
- 【Python】 利用Matplotlib验证进出场点
- Python 使用matplotlib画图添加标注、及移动坐标轴位置
- 利用 python numpy +matplotlib 绘制股票k线图
- 利用python和matplotlib对囚犯与盒子问题做可视化分析
- python matplotlib 入门系列三:坐标轴
- Python利用matplotlib生成图片背景及图例透明的效果
- 学习python中matplotlib绘图设置坐标轴刻度、文本
- python使用matplotlib绘制xy坐标轴图