刻度定位及刻度格式
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import AutoMinorLocator, MultipleLocator, FuncFormatter
x = np.linspace(0.1,5,100)
y = np.sin(x)
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111)
ax.xaxis.set_major_locator(MultipleLocator(1)) # 设置主刻度线位置
ax.yaxis.set_major_locator(MultipleLocator(1)) # 设置次刻度线位置
ax.xaxis.set_minor_locator(AutoMinorLocator(4)) # 设置主刻度线位置
ax.yaxis.set_minor_locator(AutoMinorLocator(4)) # 设置次刻度线位置
def minor_tick(x,pos):
if not x%1:
return ""
return "%.2f" %x
ax.xaxis.set_minor_formatter(FuncFormatter(minor_tick)) # 设置次要7
ax.tick_params('y',which='major', # 刻度样式 主
ax.grid(line" alt="" />
坐标轴刻度标签和刻度线样式调整
fig = plt.figure(facecolor=(1,1,0.9))
ax = fig.add_axes([.1,.4,.5,.5]) #生成Figure实例
for ticklabel in ax.xaxis.get_ticklabels(): #x轴实例 textlabel
ticklabel.set_color('slateblue')
ticklabel.set_fontsize(18)
ticklabel.set_rotation(30)
for tickline in ax.yaxis.get_ticklines(): #y轴实例 textline
tickline.set_color('lightgreen')
tickline.set_markersize(20)
tickline.set_markeredgewidth(2)
plt.show()
python高级绘图-图形坐标及刻度样式参数
设置坐标刻度标签为货币和时间
import matplotlib.pyplot as plt
import numpy as np
from calendar import month_name,day_name #日期标签
from matplotlib.ticker import FormatStrFormatter
fig = plt.figure()
ax = fig.add_axes([.2,.2,.7,.7])
x = np.arange(1,8,1)
y = np.exp(x)-0.3
ax.plot(x,y,ls='-',lw=2,color='orange',marker='o',ms=5,mfc='c',mec='c')
ax.yaxis.set_major_formatter(FormatStrFormatter(r'$\yen%1.1f/article>)) # Y轴主坐标标签
plt.xticks(x,day_name[0:7],rotation=20)
plt.show()
python高级绘图-图形坐标及刻度样式参数
添加数据注释
x = np.linspace(0,6,100)
y = np.sin(x)
fig = plt.figure(figsize=(5,5))
ax =fig.add_subplot(111)
ax.plot(x,y,c='b',ls='-',lw=2)
ax.annotate('maximum', # 指向性注释 注释内容
xy=(np.pi/2,1), # 注释位置
xycoords='data', # 坐标系统,‘data’表示与直线图同坐标
xytext=((np.pi/2)+.15,.8), # 标记的左下角顶点位置
textcoords='data', # 标签坐标系统,同坐标
weight='bold', # 注释内容样式
color='r', # 注释颜色
arrowprops=dict(arrow" alt="" />
python高级绘图-图形坐标及刻度样式参数
注释文本样式调整
x = np.linspace(1,10,40)
y = np.random.randn(40)
plt.plot(x,y,ls='-',lw=2,
marker='o',
ms=10,
mfc='orange',
alpha=.5)
plt.grid(ls=':',color='gray',alpha=.5)
plt.text(6,0,'Text Label',size=20,rotation=30,
bbox=dict(box" alt="" />
python高级绘图-图形坐标及刻度样式参数
图片添加水印
plt.plot(x,y,ls='-',lw=2,
marker='o',
ms=10,
mfc='orange',
alpha=.6)
plt.grid(ls=':',color='gray',alpha=.5)
plt.text(1,1,'Text Label',fOntsize=30,color='gray',alpha=.5)
plt.show()
python高级绘图-图形坐标及刻度样式参数
曲线指示注释
x = np.linspace(0,10,1000)
y = np.sin(x)*np.cos(x)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y,ls='-',lw=2)
bbox = dict(box>',
connection" alt="" />
python高级绘图-图形坐标及刻度样式参数
添加箭头趋势线
x = np.linspace(0,10,2000)
y = np.sin(x)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y,ls='-',lw=2)
arrowprops = dict(arrow>',color='r')
ax.annotate('',
(3*np.pi/2,np.sin(3*np.pi/2)+.05),
xytext=(np.pi/2,np.sin(np.pi/2)+.05),
color='r',
arrowprops=arrowprops)
ax.arrow(0,-.4, # 参数x,y
np.pi/2, # 参数x的水平增量
1.2, # 参数y的水平增量
head_" alt="" />
python高级绘图-图形坐标及刻度样式参数
箭头画桑基图
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
from matplotlib.sankey import Sankey
flows = [.2,.1,.4,.3,-.6,-.05,-.15,-.2]
labels = ['','','','','family','trip','education','sport']
orientatiOns=[1,1,0,-1,1,-1,1,0]
sankey = Sankey()
sankey.add(flows=flows, # 正值表示流入,负值表示流出
labels=labels,
orientatiOns=orientations, # -1表示显示位置在下方,0表示水平,1表示上方
color='c',
fc='lightgreen',
patchlabel='life Cost',
alpha=.7)
diagrams = sankey.finish()
diagrams[0].texts[4].set_color('r') # labels标签样式
diagrams[0].texts[4].set_weight('bold')
diagrams[0].text.set_fontsize(20) # patchlabel标签样式
diagrams[0].text.set_fontweight('bold')
plt.title('Sankey Chart')
plt.show()
python高级绘图-图形坐标及刻度样式参数
标题和坐标轴投影
import matplotlib.pyplot as plt
import matplotlib.patheffects as pes
import numpy as np
x = np.linspace(.5,3.5,100)
y = np.sin(x)
fOntsize= 20
plt.plot(x,y,ls='--',lw=2)
title = '$y=\sin({x})/article>
xaxis_label = '$x\_axis/article>
yaxis_label = '$y\_axis/article>
title_text_obj = plt.title(title,fOntsize=fontsize,va='bottom')
xaxis_label_text_obj = plt.xlabel(xaxis_label,fOntsize=fontsize-3,alpha=1)
yaxis_label_text_obj = plt.ylabel(yaxis_label,fOntsize=fontsize-3,alpha=1)
title_text_obj.set_path_effects([pes.withSimplePatchShadow()])
pe = pes.withSimplePatchShadow(offset=(1,-1),shadow_rgbFace='r',alpha=.3)
xaxis_label_text_obj.set_path_effects([pe])
yaxis_label_text_obj.set_path_effects([pe])
# offset 文本投影偏离距离
# shadow_rgbFace 投影颜色
# alpha 投影透明度0~1
plt.show()
python高级绘图-图形坐标及刻度样式参数
坐标轴标签添加文本框
fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111)
box = dict(facecolor='orange',pad=2,alpha=.4)
ax.plot(x,y,c='b',ls='--',lw=2)
title = '$y=\sin({x})/article>
xaxis_label = '$x\_axis/article>
yaxis_label = '$y\_axis/article>
ax.set_xlabel(xaxis_label,fOntsize=18,bbox=box) # bbox坐标轴标签文本框样式
ax.set_ylabel(yaxis_label,fOntsize=18,bbox=box)
ax.set_title(title,fOntsize=23,va='bottom')
ax.yaxis.set_label_coords(-.08,.5) # 坐标轴标签位置0~1,负值代表反方向
ax.xaxis.set_label_coords(1,-.05)
ax.grid(ls='-.',lw=1,color='gray',alpha=.5)
plt.show()
python高级绘图-图形坐标及刻度样式参数