matplotlib_图形样式

刻度定位器与刻度格式器

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import AutoMinorLocator,MultipleLocator,FormatStrFormatter
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
x=np.linspace(0.5,3.5,100)
y=np.sin(x)
#创建画布对象
fig=plt.figure(figsize=(6,6))
#向画布添加1行1列的子区
ax=fig.add_subplot(111)
#在x轴与y轴的1倍处设置主刻度
ax.xaxis.set_major_locator(MultipleLocator(1))
ax.yaxis.set_major_locator(MultipleLocator(1))
#设置xy轴次刻度,且次刻度将主刻度等分为4
ax.xaxis.set_minor_locator(AutoMinorLocator(4))
ax.yaxis.set_minor_locator(AutoMinorLocator(4))
#设置x,y轴次刻度的文本样式
ax.xaxis.set_minor_formatter(FormatStrFormatter('%.2f'))
ax.yaxis.set_minor_formatter(FormatStrFormatter('%.2f'))
#设置主刻度的样式,与次刻度的样式,
ax.tick_params(axis="x",which="major",length=15,width=2,colors="r")
ax.tick_params(axis="both",which="minor",length=5,width=1,colors="b",labelsize=10)
ax.plot(x,y)
ax.set_xlim(0,4)
ax.set_ylim(0,1)
ax.grid(which="both")
plt.show()
ax.tick_params
参数:
axis:x,y,both(默认)
which:major(主),minor(次)
length:刻度的长度
width:刻度的宽度
colors:刻度线的颜色
labelsize:刻度对应刻度值的字体大小

有箭头指示的趋势图

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import AutoMinorLocator,MultipleLocator,FormatStrFormatter
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
x=np.linspace(0,10,2000)
y=np.sin(x)
fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot(x,y,ls="-")
ax.set_xlim(0)
ax.set_ylim(-1.5,1.5)
arrowprops={"arrowstyle":"-|>","color":"r"}
ax.annotate("",xy=(3*np.pi/2,np.sin(3*np.pi/2)+0.05),xytext=(np.pi/2,np.sin(np.pi/2)+0.05),color="r",arrowprops=arrowprops)
ax.arrow(0,-0.4,np.pi/2,1.2,fc='g',ec='g',head_width=0.05,head_length=0.1)
plt.show()
ax.arrow(0,-0.4,np.pi/2,1.2,fc='g',ec='g',head_width=0.05,head_length=0.1)
x:x轴起点
y:y轴起点
dx:x轴的增量
dy:y轴的增量
fc:颜色
ec:线条颜色
head_width=0.05,head_length=0.1:线条尾部样式

改变坐标轴的位置

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-5, 5, 100)
y1 = 0.5 * x

fig,ax=plt.subplots()
ax.plot(x,y1)

# 将右边 上边的两条边颜色设置为空 其实就相当于抹掉这两条边
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')

# 指定下边的边作为 x 轴   指定左边的边为 y 轴
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

#指定 data  设置的bottom(也就是指定的x轴)绑定到y轴的0这个点上
ax.spines['bottom'].set_position(('data', 0))
ax.spines['left'].set_position(('data', 0))

plt.show()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值