matplotlib.pyplot常⽤画图⽅式函数封装(⼀)——.plot绘制折线图及设置。。
matplotlib.pyplot常⽤画图⽅式函数封装(⼀)——.plot绘制折线图及设置坐标轴箭头完美解决
py.plot常见绘图设置函数封装
本⽂主要针对运⽤py.plot作图时的常⽤设置进⾏了函数封装,⼀般来说,py.plot常⽤作绘制函数图像和折线图,对于绘制函数图像时的坐标轴箭头问题,本⽂做出了完美解决。进⾏⾃主封装的函数设定了许多默认参数,调⽤者可根据⾃⼰的具体业务进⾏设定。
绘制函数图像(完美解决坐标轴添加箭头)
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from matplotlib import font_manager
import seaborn as sns
# 设置字体
ft = font_manager.FontProperties(fname ="C://Windows/",size =18)
def function_fig(df,grid = True,save = False,dpi_value =72,fig_size =(20,8),show = True,arrow = True):
# 初始化画布
figure = plt.figure(figsize = fig_size,dpi = dpi_value)
ax = figure.add_subplot(111)
# 绘图
plt.plot(df.iloc[:,0],df.iloc[:,1],color ="blue",linewidth =2,
linestyle ="-")
# 设置坐标轴显⽰
# 设置坐标轴显⽰范围
plt.axis([-max(abs(df.iloc[:,0].min()-0.05*(df.iloc[:,0].max()-df.iloc[:,0].min())),abs(df.iloc[:,0].max()+0.05*(df.iloc[:,0].max()-df.iloc[:,0].min()))), max(abs(df.iloc[:,0].min()-0.05*(df.iloc[:,0].max()-df.iloc[:,0].min())),abs(df.iloc[:,0].max()+0.05*(df.iloc[:,0].max()-df.iloc[:,0].min()))), -max(abs(df.iloc[:,1].min()-0.05*(df.iloc[:,1].max()-df.iloc[:,1].min())),abs(df.iloc[:,1].max()+0.05*(df.iloc[:,1].max()-df.iloc[:,1].min()))), max(abs(df.iloc[:,1].min()-0.05*(df.iloc[:,1].max()-df.iloc[:,1].min())),abs(df.iloc[:,1].max()+0.05*(df.iloc[:,1].max()-df.iloc[:,1].min())))])    # 去除上、右边框
ax = a()
ax.spines["top"].set_color("none")
ax.spines["right"].set_color("none")
# 调整x轴和y轴
ax.spines["left"].set_position(("data",0))
ax.spines["bottom"].set_position(("data",0))
y_max =max(abs(df.iloc[:,1].min()-0.05*(df.iloc[:,1].max()-df.iloc[:,1].min())),abs(df.iloc[:,1].max()+0.05*(df.iloc[:,1].max()-df.iloc[:,1].min())))    x_max =max(abs(df.iloc[:,0].min()-0.05*(df.iloc[:,0].max()-df.iloc[:,0].min())),abs(df.iloc[:,0].max()+0.05*(df.iloc[:,0].max()-df.iloc[:,0].min())))    # 设置x轴和y轴的箭头
ax.arrow(0,(1-0.04*fig_size[0]/fig_size[1])*y_max,0,0.04*fig_size[0]/fig_size[1]*y_max,
head_width=0.04*fig_size[1]/fig_size[0]*x_max,
head_length=0.04*fig_size[0]/fig_size[1]*y_max,
fc='black',
length_includes_head=True)
ax.arrow(0.96*x_max,0,0.04*x_max,0,
head_width=0.04*y_max,
head_length=0.04*x_max,
fc='black',
length_includes_head=True)
# 设置⽹格线
if grid == True:
if save == True:
plt.savefig("./"+list()[0])+"&"+list()[1]+".jpg")
if show == True:
plt.show()
if __name__ =="__main__":
df = pd.read_excel("./data.xlsx",header =0,sheet_name ="Sheet1")
function_fig(df)
绘制结果:
绘制折线图
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from matplotlib import font_manager
import seaborn as sns
# 设置字体
ft = font_manager.FontProperties(fname ="C://Windows/",size =18) # ⾃定义折线图函数
def line_fig(df,grid = True,show = True,save = False,dpi_value =72,fig_size =(20,8)):    # 初始化画布
figure = plt.figure(dpi = dpi_value,figsize = fig_size)
ax = figure.add_subplot(111)
# 做折线图
plt.plot(df.iloc[:,0],df.iloc[:,1],color ="blue",linestyle ="-",marker ="o",markersize =6,            markeredgecolor ="crimson",markeredgewidth =6,linewidth =2)
# 设置坐标轴显⽰
plt.list()[0],fontproperties = ft,size =25)
plt.list()[1],fontproperties = ft,size =25)
# 设置坐标轴显⽰范围
plt.axis([df.iloc[:,0].min()-0.05*(df.iloc[:,0].max()-df.iloc[:,0].min()),
df.iloc[:,0].max()+0.05*(df.iloc[:,0].max()-df.iloc[:,0].min()),
df.iloc[:,1].min()-0.1*(df.iloc[:,1].max()-df.iloc[:,1].min()),
df.iloc[:,1].max()+0.1*(df.iloc[:,1].max()-df.iloc[:,1].min())])
# 去除上、右边框
ax = a()
ax.spines["top"].set_color("none")
ax.spines["right"].set_color("none")
# 设置⽹格线
if grid == True:
if save == True:
plt.savefig("./"+list()[0])+"&"+list()[1]+".jpg")
if show == True:
plt.show()
if __name__ =="__main__":
df = pd.read_excel("./data.xlsx",header =0,sheet_name ="Sheet1")
line_fig(df)
绘制结果如下:
by CyrusMay 2020 04 10
幻想着未来matplotlib中subplot
满头⽩发
公园的长椅上
你也许会说
⼀声谢谢我
如果这⼀⽣
到尽头
换你的这句话
很⾜够
——————五⽉天——————

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。