PYTHON:饼图和折线图
一、饼图
import matplotlib.pyplot as plt
labels = '1', '2', '3', '4'
sizes = 15,20, 45, 10 # 标签对应的份额数
colors = 'yellowgreen', 'gold', 'lightskyblue', 'lightcoral'
explode = 0, 0.1, 0, 0
plt.pie(sizes, explode = explode, labels = labels, colors = colors, autopct = '%1.1f%%', shadow = True, startangle = 50)
plt.axis('equal')
plt.savefig('路径') #放在show之前,不然导出的图会是空白的
plt.show()
生成图:
二、折线图
import pandas as pd
from matplotlib import pyplot as plt
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['SimHei'] # 中文显示
def zxt_plot():
x = pd.to_datetime(data['DATE'], format = '%Y-%m').tolist() # 横坐标以时间为格式,如果直接使用array转化会导致时间转为数值
y = np.array(data['VALUE']).tolist()
plt.figure()
plt.plot(x, y, linewidth = 3, color = 'blue', marker = 'o',markerfacecolor = 'red', markersize = 5, label = u'xxx') # 画图
plt.legend() # 显示线的标签
plt.xlabel('月份') # x轴标签
plt.ylabel('增速(%)') # y轴标签
plt.title('固定资产投资额增速') # 标题
plt.savefig('路径') #保存
plt.show()
zxt_plot()