前言
Python 作图
一、曲线图
代码实例:
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(1,11)
y = x**2 + 5
plt.plot(x,y,color='b',linewidth = 3, linestyle='-')
plt.title("Matplotlib demo")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
plt.show()
二、柱状图
代码实例:
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['font.sans-serif']=['SimHei']
matplotlib.rcParams['axes.unicode_minus']=False
label_list=["12月","1月",'2月','3月','4月','5月']
list1=[58064,59531,61326,54704,53049,51155]
list2=[47011,48099,47140,46476,45134,45705]
x=range(len(list1))
rects1=plt.bar(left=x,height=list1,width=0.4,color='blue',label="北京")
rects2=plt.bar(left=[i + 0.4 for i in x],height=list2,width=0.4,color='green',label="上海")
plt.ylim(40000,80000)
plt.ylabel("平均房价")
plt.xticks([index+0.2 for index in x],label_list)
plt.xlabel("月份")
plt.title("北京上海近六个月房产均价")
plt.legend()
for rect in rects1:
height=rect.get_height()
plt.text(rect.get_x() + rect.get_width()/2,height+1,str(height),ha="center",va="bottom")
for rect in rects2:
height=rect.get_height()
plt.text(rect.get_x() + rect.get_width()/2,height+1,str(height),ha="center",va="bottom")
plt.show()
三、饼图
代码实例:
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
matplotlib.rcParams['axes.unicode_minus'] = False
label_list = ["优秀","良好","中等","及格","不及格"]
size = [10,19,19,23,29]
color = ["red","green","blue"]
explode = [0,0,0,0,0.05]
patches = plt.pie(size, explode=explode,colors=color, labels=label_list,
labeldistance=1.1,autopct="%1.1f%%",shadow=False,startangle=90,
pctdistance=0.6)
plt.axis("equal")
plt.legend()
plt.show()