没有合适的资源?快使用搜索试试~ 我知道了~
首页Python绘图Matplotlib之坐标轴及刻度总结
学习https://matplotlib.org/gallery/index.html 记录,描述不一定准确,具体请参考官网 Matplotlib使用总结图 import matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['SimHei'] # 用来正常显示中文标签 plt.rcParams['axes.unicode_minus']=False # 用来正常显示负号 import pandas as pd import numpy as np 新建隐藏坐标轴 from mpl_toolkits.axisartist.
资源详情
资源评论
资源推荐

Python绘图绘图Matplotlib之坐标轴及刻度总结之坐标轴及刻度总结
学习https://matplotlib.org/gallery/index.html 记录,描述不一定准确,具体请参考官网
Matplotlib使用总结图
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False # 用来正常显示负号
import pandas as pd
import numpy as np
新建隐藏坐标轴新建隐藏坐标轴
from mpl_toolkits.axisartist.axislines import SubplotZero
import numpy as np
fig = plt.figure(1, (10, 6))
ax = SubplotZero(fig, 1, 1, 1)
fig.add_subplot(ax)
"""新建坐标轴"""
ax.axis["xzero"].set_visible(True)
ax.axis["xzero"].label.set_text("新建y=0坐标")
ax.axis["xzero"].label.set_color('green')
# ax.axis['yzero'].set_visible(True)
# ax.axis["yzero"].label.set_text("新建x=0坐标")
# 新建一条y=2横坐标轴
ax.axis["新建1"] = ax.new_floating_axis(nth_coord=0, value=2,axis_direction="bottom")
ax.axis["新建1"].toggle(all=True)
ax.axis["新建1"].label.set_text("y = 2横坐标")
ax.axis["新建1"].label.set_color('blue')
"""坐标箭头"""
ax.axis["xzero"].set_axisline_style("-|>")
"""隐藏坐标轴"""
# 方法一:隐藏上边及右边
# ax.axis["right"].set_visible(False)
# ax.axis["top"].set_visible(False)
#方法二:可以一起写
ax.axis["top",'right'].set_visible(False)
# 方法三:利用 for in
# for n in ["bottom", "top", "right"]:
# ax.axis[n].set_visible(False)
"""设置刻度"""
ax.set_ylim(-3, 3)
ax.set_yticks([-1,-0.5,0,0.5,1])
ax.set_xlim([-5, 8])
# ax.set_xticks([-5,5,1])

#设置网格样式
ax.grid(True, linestyle='-.')
xx = np.arange(-4, 2*np.pi, 0.01)
ax.plot(xx, np.sin(xx))
# 于 offset 处新建一条纵坐标
offset = (40, 0)
new_axisline = ax.get_grid_helper().new_fixed_axis
ax.axis["新建2"] = new_axisline(loc="right", offset=offset, axes=ax)
ax.axis["新建2"].label.set_text("新建纵坐标")
ax.axis["新建2"].label.set_color('red')
plt.show()
# 存为图像
# fig.savefig('test.png')
from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA
import matplotlib.pyplot as plt
host = host_subplot(111, axes_class=AA.Axes)
plt.subplots_adjust(right=0.75)
par1 = host.twinx()
par2 = host.twinx()
offset = 100
new_fixed_axis = par2.get_grid_helper().new_fixed_axis
par2.axis["right"] = new_fixed_axis(loc="right",
axes=par2,
offset=(offset, 0))
par1.axis["right"].toggle(all=True)
par2.axis["right"].toggle(all=True)
host.set_xlim(0, 2)
host.set_ylim(0, 2)
host.set_xlabel("Distance")
host.set_ylabel("Density")
par1.set_ylabel("Temperature")
par2.set_ylabel("Velocity")
p1, = host.plot([0, 1, 2], [0, 1, 2], label="Density")
p2, = par1.plot([0, 1, 2], [0, 3, 2], label="Temperature")
p3, = par2.plot([0, 1, 2], [50, 30, 15], label="Velocity")
par1.set_ylim(0, 4)
par2.set_ylim(1, 65)

host.legend()
host.axis["left"].label.set_color(p1.get_color())
par1.axis["right"].label.set_color(p2.get_color())
par2.axis["right"].label.set_color(p3.get_color())
plt.draw()
plt.show()
# 第二坐标
fig, ax_f = plt.subplots()
# 这步是关键
ax_c = ax_f.twinx()
ax_d = ax_f.twiny()
# automatically update ylim of ax2 when ylim of ax1 changes.
# ax_f.callbacks.connect("ylim_changed", convert_ax_c_to_celsius)
ax_f.plot(np.linspace(-40, 120, 100))
ax_f.set_xlim(0, 100)
# ax_f.set_title('第二坐标', size=14)
ax_f.set_ylabel('Y轴',color='r')
ax_f.set_xlabel('X轴',color='c')
ax_c.set_ylabel('第二Y轴', color='b')
ax_c.set_yticklabels(["$0$", r"$\frac{1}{2}\pi$", r"$\pi$", r"$\frac{3}{2}\pi$", r"$2\pi$"])
# ax_c.set_ylim(1,5)
ax_d.set_xlabel('第二X轴', color='g')
ax_d.set_xlim(-1,1)
plt.show()
刻度及标记刻度及标记
import mpl_toolkits.axisartist.axislines as axislines
fig = plt.figure(1, figsize=(10, 6))
fig.subplots_adjust(bottom=0.2)

# 子图1
ax1 = axislines.Subplot(fig, 131)
fig.add_subplot(ax1)
# for axis in ax.axis.values():
# axis.major_ticks.set_tick_out(True) # 标签全部在外部
ax1.axis[:].major_ticks.set_tick_out(True) # 这句和上面的for循环功能相同
ax1.axis["left"].label.set_text("子图1 left标签") # 显示在左边
# 设置刻度
ax1.set_yticks([2,4,6,8])
ax1.set_xticks([0.2,0.4,0.6,0.8])
# 子图2
ax2 = axislines.Subplot(fig, 132)
fig.add_subplot(ax2)
ax2.set_yticks([1,3,5,7])
ax2.set_yticklabels(('one','two','three', 'four', 'five')) # 不显示‘five'
ax2.set_xlim(5, 0) # X轴刻度
ax2.axis["left"].set_axis_direction("right")
ax2.axis["left"].label.set_text("子图2 left标签") # 显示在右边
ax2.axis["bottom"].set_axis_direction("top")
ax2.axis["right"].set_axis_direction("left")
ax2.axis["top"].set_axis_direction("bottom")
# 子图3
ax3 = axislines.Subplot(fig, 133)
fig.add_subplot(ax3)
# 前两位表示X轴范围,后两位表示Y轴范围
ax3.axis([40, 160, 0, 0.03])
ax3.axis["left"].set_axis_direction("right")
ax3.axis[:].major_ticks.set_tick_out(True)
ax3.axis["left"].label.set_text("Long Label Left")
ax3.axis["bottom"].label.set_text("Label Bottom")
ax3.axis["right"].label.set_text("Long Label Right")
ax3.axis["right"].label.set_visible(True)
ax3.axis["left"].label.set_pad(0)
ax3.axis["bottom"].label.set_pad(20)
plt.show()
import matplotlib.ticker as ticker
# Fixing random state for reproducibility
np.random.seed(19680801)
fig, ax = plt.subplots()
ax.plot(100*np.random.rand(20))
# 设置 y坐标轴刻度
formatter = ticker.FormatStrFormatter('$%1.2f')
剩余15页未读,继续阅读




















weixin_38576811
- 粉丝: 4
- 资源: 890
上传资源 快速赚钱
我的内容管理 收起
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助

会员权益专享
安全验证
文档复制为VIP权益,开通VIP直接复制

评论0