matplotlib.pyplot常用画图方式函数封装(一)——.plot绘制折线图及设置坐标轴箭头完美解决
生活随笔
收集整理的這篇文章主要介紹了
matplotlib.pyplot常用画图方式函数封装(一)——.plot绘制折线图及设置坐标轴箭头完美解决
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
matplotlib.pyplot常用畫圖方式函數封裝(一)——.plot繪制折線圖及設置坐標軸箭頭完美解決
- py.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/Fonts/simsun.ttc",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.xticks(fontproperties = ft,size = 22)plt.yticks(fontproperties = ft,size = 22)# 設置坐標軸顯示范圍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 = plt.gca()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:plt.grid(linestyle = "-",alpha = 0.5)if save == True:plt.savefig("./"+str(df.columns.tolist()[0])+"&"+df.columns.tolist()[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/Fonts/simsun.ttc",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.xticks(df.iloc[:,0],fontproperties = ft,size = 22)plt.yticks(fontproperties = ft,size = 22)plt.xlabel(df.columns.tolist()[0],fontproperties = ft,size = 25)plt.ylabel(df.columns.tolist()[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 = plt.gca()ax.spines["top"].set_color("none")ax.spines["right"].set_color("none")# 設置網格線if grid == True:plt.grid(axis = "y",linestyle = "-",alpha = 0.5)if save == True:plt.savefig("./"+str(df.columns.tolist()[0])+"&"+df.columns.tolist()[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.pyplot常用画图方式函数封装(一)——.plot绘制折线图及设置坐标轴箭头完美解决的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 简单数据分布分析及python实现
- 下一篇: 爬虫篇——User-Agent爬取备用及