关于matplotlib的twinx()的使用
2017-04-12 18:47
337 查看
#!/usr/bin/python #coding: utf-8 import numpy as np import matplotlib.pyplot as plt x = np.arange(1, 21, 0.1) y1 = x * x y2 = np.log(x) plt.plot(x, y1) # 添加一条坐标轴,y轴的 plt.twinx() plt.plot(x, y2) plt.show()
#!/usr/bin/python #coding: utf-8 import numpy as np import matplotlib.pyplot as plt x = np.arange(1, 20, 1) y1 = x * x y2 = np.log(x) fig = plt.figure() ax1 = fig.add_subplot(111) # print(ax1) ax1.plot(x, y1, label = "$y1 = x * x$", color = "r") ax1.legend(loc = 0) # 设置对应坐标轴的名称 ax1.set_ylabel("y1") ax1.set_xlabel("Compare y1 and y2") # 设置x轴刻度的数量 ax = plt.gca() ax.locator_params("x", nbins = 20) # 添加坐标轴,并在新添加的坐标轴中画y2 = log(x)图像 ax2 = plt.twinx() ax2.set_ylabel("y2") ax2.plot(x, y2, label = "$y2 = log(x)$") ax2.legend(loc = 0) plt.show() # 也可以设置两个x轴,方法和双y轴相同,要把plot中对应的x和y互换,这样显示的结果和双y轴基本相同
相关文章推荐
- 关于使用matplotlib-legend方法失效问题
- 关于使用pyinstaller 打包带有matplotlib 等出现UnicodeDecodeError解决方法
- 关于使用pyinstaller 打包带有matplotlib 等出现UnicodeDecodeError解决方法
- python使用matplotlib绘图 -- barChart
- networkx使用笔记(三)之好汉篇Matplotlib(4)
- 按钮实现Python--绘图工具matplotlib的使用
- 关于matplotlib的DISPLAY错误
- python使用matplotlib绘图 -- barChart
- 使用 python Matplotlib 库绘图
- Python:使用matplotlib绘制图表
- 使用 python Matplotlib 库绘图
- python使用matplotlib绘图 -- barChart
- 使用py2exe和matplotlib
- Python:使用matplotlib绘制图表
- Python--绘图工具matplotlib的使用
- Python绘图和数值工具:matplotlib 和 numpy下载与使用
- python使用matplotlib绘图 -- barChart
- Matplotlib简介和pyplot的简单使用——局部放大图
- Matplotlib简介和pyplot的简单使用——Artists
- 使用matplotlib的示例:调整字体-设置colormap和colorbar