python双Y轴

简介: import matplotlib.pyplot as pltimport numpy as npx = np.arange(0., np.e, 0.
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0., np.e, 0.01)
y1 = np.exp(-x)
y2 = np.log(x)

fig = plt.figure()
sns.set_style('white')
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()
fig.savefig('doubleY.pdf')
img_57295073cdf7f3f9bb707f276bd7659f.png
# 生成画布, 并设定标题
plt.figure(figsize=(8, 6), dpi=80)
plt.grid(True)
# 画图的另外一种方式
ax_1 = plt.subplot(111)
ax_1.plot(x, y_cos, color="blue", linewidth=2.0, linestyle="--", label="cos")
ax_1.legend(loc="upper left", shadow=True)
# 设置Y轴(左边)
ax_1.set_ylabel("cosy")
ax_1.set_ylim(-1.0, 1.0)
ax_1.set_yticks(np.linspace(-1, 1, 9, endpoint=True))

# 画图的另外一种方式
ax_2 = ax_1.twinx()
ax_2.plot(x, y_sin, color="green", linewidth=2.0, linestyle="-", label="sin")
ax_2.legend(loc="upper right",  shadow=True)

# 设置Y轴(右边)
ax_2.set_ylabel("siny")
ax_2.set_ylim(-2.0, 2.0)
ax_2.set_yticks(np.linspace(-2, 2, 9, endpoint=True))

# 设置X轴(共同)
ax_1.set_xlabel("x")
ax_1.set_xlim(-4.0, 4.0)
ax_1.set_xticks(np.linspace(-4, 4, 9, endpoint=True))

# 图形显示
plt.show()
目录
相关文章
|
24天前
|
数据可视化 数据挖掘 数据处理
python 盒装图纵坐标单位
【4月更文挑战第1天】
|
6月前
|
Python
python之使用while循环输出如下星图形
python之使用while循环输出如下星图形
134 1
python之使用while循环输出如下星图形
|
Python
【python实战】top3 英尺转换为米
【python实战】top3 英尺转换为米
280 0
【python实战】top3 英尺转换为米
|
18天前
|
Python
471: 旋转字(python)
471: 旋转字(python)
|
8月前
|
Shell 流计算 Python
[oeasy]python0083_[趣味拓展]字体样式_正常_加亮_变暗_控制序列
[oeasy]python0083_[趣味拓展]字体样式_正常_加亮_变暗_控制序列
41 0
|
Python
Python matplotlib修改柱状图柱子与图案周边的距离
本文介绍基于Python中matplotlib.pyplot模块,修改柱状图、条形图最两侧的柱子与图像边缘之间距离的方法~
161 1
Python matplotlib修改柱状图柱子与图案周边的距离
python小玩意——将动态图转数字
python小玩意——将动态图转数字
python小玩意——将动态图转数字
|
Python
用python拍饼图--学习笔记19
用python拍饼图--学习笔记19
68 0
用python拍饼图--学习笔记19
|
Python
python——大智慧--三重指数平滑--TRIX
python——大智慧--三重指数平滑--TRIX
112 0
|
Python
Python经典编程习题100例:第35例:文本颜色设置
Python经典编程习题100例:第35例:文本颜色设置
70 0

相关实验场景

更多