Matplotlib--axis坐标轴
程序员文章站
2022-05-27 16:31:26
...
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import norm,t
plt.rcParams['font.family'] = ["SimHei"]
plt.rcParams['axes.unicode_minus']=False
x = np.linspace(-5,10,100)
y1 = norm.pdf(x, loc=0, scale=1)
y2 = t.pdf(x, df=1)
设置刻度范围与样式
plt.figure()
plt.plot(x, y1)
plt.plot(x, y2, linestyle='--')
# 设置坐标轴范围
plt.xlim((-5,5))
plt.ylim((-0.5,0.5))
# 设置坐标轴刻度
plt.xticks(ticks=np.arange(-5,6,2),labels=['负五','负三','负一','一','三','五'],
rotation=15, size=20)
plt.yticks(ticks=[-0.1,0.5])
plt.show()
fig = plt.figure()
ax = fig.add_subplot()
plt.plot(x, y1)
plt.plot(x, y2, linestyle='--')
ax.spines['top'].set_color('red')
ax.spines['right'].set_color('none')
# 设置坐标轴刻度位置
ax.xaxis.set_ticks_position('top')
ax.yaxis.set_ticks_position('right')
ax.spines['left'].set_position(('data',0))
ax.spines['bottom'].set_position(('data',0.1))
设置坐标轴位置
添加新ax
fig, ax = plt.subplots()
plt.plot(x, y1)
plt.plot(x, y2, linestyle='--')
ax_1 = ax.twinx()
ax_1.set_ylim(0,20)
ax_1.spines['right'].set_position(('data',3))
ax_2 = ax.twiny()
ax_2.set_xlim(-25,25)
ax_2.spines['top'].set_position(('data',0.2))
ax.set_xlim(-5,5)
# 这两行失效了???
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')