最近在用python进行数据处理相关工作,这块主要分享一点绘图和可视化的简单东西,也是做个小笔记让自己更熟悉地运用python。
1.折线图
import pandas as pd
import matplotlib.pyplot as plt
path = "E:\\dataset.xlsx"
data = pd.read_excel(path,sheet_name="up20", encoding = 'utf-8')
x = data.iloc[:,0]
y1 = data.iloc[:,1]
y2 = data.iloc[:,2]
y3 = data.iloc[:,3]
y4 = data.iloc[:,6]
y5 = data.iloc[:,7]
gra1 = plt.plot(x,y1,'r--',label='85%')
gra2 = plt.plot(x,y2,'g--',label='80%')
gra3 = plt.plot(x,y3,'b--',label='75%')
gra4 = plt.plot(x,y4,'y--',label='μ')
gra5 = plt.plot(x,y5,'k--',label='all-ave')
plt.plot(x,y1,'ro-',x,y2,'g+-',x,y3,'b^-',x,y4,'y--',x,y5,'k--') #5条线在一个图中
plt.title('The difference Conditions')
plt.xlabel('dt')
plt.ylabel('play-time')
plt.legend()
plt.show()
代码显示效果:
data
2.直方图
import pamdas as pd
import matplotlib.pyplot as plt
import seaborn as sns
path = "F:\\dataset.csv"
data = pd.resd_csv(path,encoding='utf-8')
data.head()
sns.distplot(data.iloc[:,-1],kde = False)
plt.ylabel('Frequency')
plt.title('distribution')
代码效果:
3.箱型图
import pandas as pd
import matplotlib.pyplot as plt
path = "F:\\dataset.xlsx"
data = pd.read_excel(path,sheet_name="newdt", encoding = 'utf-8')
data.iloc[:,1].plot.box()
plt.rcParams['font.sans-serif']=['SimHei'] #用来正确显示中文
plt.grid(linestyle='--', linewidth=2,alpha = 0.3)
plt.xlabel('playtime')
plt.title(u'箱型图‘)
plt.show()
代码效果: