我的冷漠里藏着一半的害羞,一半的自卑。
通过做图表来分析数据实在是一个非常棒的方法,由于我偶尔忘记语法,还得翻之前的笔记,难受。下面就画了些常用的图,记一记,记一记。
1.折线图
代码语言:javascript复制import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
x1 = [1,2,3]
y1 = [5,7,4]
x2 = [1,2,3]
y2 =[10,14,12]
matplotlib.rcParams['font.sans-serif'] = ['SimHei']#中文显示问题
f=plt.figure(figsize=(12,10))#调整图像大小
plt.plot(x1,y1,label='First Line')
plt.plot(x2, y2, label='Second Line')
plt.xlabel("Plot Number",fontsize = 20)#x轴 标签及调整字体大小
plt.ylabel("Important var",fontsize = 20)#y轴 标签及调整字体大小
plt.title("标题", fontsize = 20)#标题
plt.legend()#显示图例
plt.xlim(0,4)#调整x轴大小
plt.ylim(0,15)#调整y轴大小
plt.grid()#添加格线
plt.show()
2.柱状图
代码语言:javascript复制matplotlib.rcParams['font.sans-serif'] = ['SimHei']
label_list = ["AUC","MAP","MRR","Prec","Rec","F1","NDGC","Call"]
num_list_1 = [ 0.8395789534863245,0.03947255032401116,0.4421931735657211,0.18845315904139434,0.061183509516103594, 0.07578339296217934, 0.20391161229968288, 0.5533769063180828]
num_list_2 = [0.8099706571182651, 0.07124258816665524,0.5007884899720522,0.29520697167755994,0.051690038913594256,0.07702843749844428, 0.30278346199328815,0.664488017429194]
x = range(len(num_list_1))
#f=plt.figure(figsize=(12,10))
rects_1 = plt.bar( x,height = num_list_1,width = 0.4,alpha = 0.8,color = "red",label = "MBPR")
rects_2 = plt.bar([i 0.4 for i in x],height = num_list_2,width = 0.4,alpha = 0.8,color = "blue",label = "BPR")#柱状图对比
plt.ylim(0,1)
plt.ylabel("指标")
plt.xticks([index 0.2 for index in x], label_list)#自定义x 轴含义
plt.xlabel("模型")
plt.title("MBPR、BPR对比图")
plt.legend()
plt.show()
3.散点图
代码语言:javascript复制x = [1,2,3,4,5,6]
y = [1,2,3,4,5,6]
#f = plt.figure(figsize=(12,10))
plt.scatter(x,y,color="r")
4.饼图
代码语言:javascript复制#f=plt.figure(figsize=(12,10))
slices = [7,2,2,13]
activities = ['sleeping','eating','working','playing']
cols = ['c','m','r','b']
plt.pie( slices,labels=activities,colors=cols,startangle=90,shadow= True ,explode=(0,0.3,0,0),autopct='%1.1f%%')
# autopct='%1.1f%%' 百分比
5.箱式图
代码语言:javascript复制import seaborn as sns
#f=plt.figure(figsize=(12,10))
sns.set_style("whitegrid")
data = np.random.normal(size=(20, 6)) np.arange(6) / 2
sns.boxplot(data=data)
6.热度图
代码语言:javascript复制%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np;
np.random.seed(0)
import seaborn as sns;
sns.set()
uniform_data = np.random.rand(3, 3)
#print (uniform_data)
heatmap = sns.heatmap(uniform_data)
7.子图
代码语言:javascript复制import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-10, 10, 100)
y =2*np.cos(x)**5 3*np.sin(x)**3
#定义画布和子图数量
fig,axes=plt.subplots(2,3,figsize=(20,18),facecolor='#ccddef')
#添加整个画布的标题
fig.suptitle('我是最大的标题',fontsize=20)
#利用text属性添加副标题
fig.text(0.45,0.9,'这是副标题')
#折线图
axes[0][0].plot(x,y)
#柱状图
axes[0][1].bar(x,y)
#直方图
axes[0][2].hist(y,bins=30)
#散点图
axes[1][0].scatter(x,y)
#条形图
axes[1][1].barh(x,y)
#饼图
axes[1][2].pie([1,2,3,4,5],labels=['A级','B级','C级','D级','E级'])
#axes[1,2].boxplot() 需要合适的数据,就不画了
#设置子图的xy轴范围,子图标题,标签背景颜色等,也可单独使用ax1.set_xlim()进行设置。部分属性不能直接使用set设置
ax1.set(xlim=[-10,12],ylim=[-6,4],title='This is TU1',xlabel='xlabel',ylabel='ylabel',facecolor='#ffeedd')