# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False # 用来正常显示负号
fig=plt.figure(figsize=(12,6)) # 定义图并设置画板尺寸
fig.set(alpha=0.2) # 设定图表颜色alpha参数
# fig.tight_layout() # 调整整体空白
plt.subplots_adjust(bottom=0.06,top=0.94,left=0.08,right=0.94,wspace =0.36, hspace =0.5) # 设置作图范围、子图间距
# 子图基本设置
ax1=fig.add_subplot(241) # 定义子图1
ax1.set(title=u"子图标题",xlabel=u"x轴标题",ylabel=u"y轴标题") # 设置标题
ax1.set_title(u"子图标题",fontdict={'fontsize':10}) # 设置标题字体大小
ax1.axis([-5,5,-2,5]) # 设置坐标范围
ax1.spines["top"].set_color("none") # 上边框设置为不显示
ax1.spines["right"].set_color("none") # 右边框设置为不显示
ax1.spines["left"].set_position(("data",0)) # 左边框位置设置在0刻度
ax1.spines["bottom"].set_position(("data",0)) # 下边框位置设置在0刻度
ax1.xaxis.set_ticks_position("bottom") # x轴位置设置在下边框
ax1.yaxis.set_ticks_position("left") # y轴位置设置在左边框
# ax1.set_xlim(-5,5) # 设置x轴刻度范围
# ax1.set_ylim(-2,5) # 设置y轴刻度范围
plt.xticks([-2*np.pi,-np.pi,0,np.pi,2*np.pi],["$-2\pi$","$-\pi$","$0$","$\pi$","$2\pi$"]) # 设置x轴刻度及标签
plt.yticks([-2,-1,0,1,2,3]) # 设置y轴刻度及标签
# 绘制简单线形图
x=np.arange(-2*np.pi,2*np.pi,0.01) # 定义自变量序列
y1=np.sin(3*x)/x # 定义因变量序列1
y1_2 = np.sin(2*x)/x # 定义因变量序列2
ax1.plot(x,y1,"k--") # 绘图1。 颜色参数:b蓝色,g绿色,r红色,c蓝绿色,m洋红,y黄色,k黑色,w白色
for i in range(0,len(x),1000): # 添加坐标标签
plt.text(x[i],y1[i],(x[i],y1[i]))
plt.grid(True) # 添加网格
plt.legend(["first series","second series","third series"],loc=1) # 添加图例
ax1.plot(x,y1_2,color="#87a3cc",linestyle="--") # 绘图2。
# plt.annotate() # 用于添加注释
# 绘制直方图
ax2=fig.add_subplot(242)
x2= np.random.randint(0,100,200) # 生成200个0-100的整数
n,bins,patches = plt.hist(x2,bins=5) # 绘制直方图,面元划分5份
# print(n,bins,patches)
# 绘制条形图
ax3=fig.add_subplot(243)
x3= [0,1,2,3,4] # 定义自变量序列3
y3= [5,6,7,8,9] # 定义因变量序列3
ax3.bar(x3,y3,error_kw={'ecolor':'0.1 ','capsize':6},alpha=0.7,label='First') # 绘制条形图 yerr=stdl,
plt.legend(loc=2)
# 绘制水平条形图
ax4=fig.add_subplot(244)
x4= [0,1,2,3,4] # 定义自变量序列4
y4= [5,6,7,8,9] # 定义因变量序列
ax4.barh(x4,y4,error_kw={'ecolor':'0.1 ','capsize':6},alpha=0.7,label='First') # 绘制条形图 yerr=stdl,
plt.legend(loc=0)
# 绘制多序列堆积条状图
ax5=fig.add_subplot(245) # 子图5
ax5.set_title('A Multiseries Bar Chart' ,fontsize=10)
ax5.axis([0,5,0,25]) # 坐标范围
x5= np.arange(5) # 定义自变量序列
y5_l=[5,7,8,4,7] # 因变量序列5_1
y5_2=[8,6,4,9,7] # 因变量序列5_2
y5_3=np.array([7,6,6,8,6]) # 因变量序列5_3
y5_4=np.array([2,6,5,3,4]) # 因变量序列5_4
y5_5=[3,6,5,5,4] # 因变量序列5_5
bw = 0.2 # 条粗细常量
ax5.bar (x5, y5_l, bw, color='b',hatch='xx') # hatch参数设置影线
ax5.bar(x5+bw,y5_2,bw,color="g",hatch='///')
ax5.bar(x5+2*bw,y5_3,bw,color='r',hatch="\\\\\\\'")
ax5.bar(x5+2*bw,y5_4,bw,color='k',bottom=y5_3) # bottom参数设置堆积
ax5.bar(x5+2*bw,y5_5,bw,color='y',bottom=y5_3+y5_4) # bottom参数设置堆积
plt.xticks( x5+1.5* bw , ['A ','B ','C ','D ',' E ']) # 设置x轴刻度及标签
# 分两侧条形图
ax6=fig.add_subplot(246) # 子图6
ax6.set_ylim(-7,7) # 设置y轴刻度范围
x6= np.arange(8)
y6_1= np.array([1,3,4,6,4,3,2,3])
y6_2= np.array([1,2,5,4,3,3,2,1])
ax6.bar(x6,y6_1,0.9,facecolor='r',edgecolor='w')
ax6.bar(x6,-y6_2,0.9,facecolor='b',edgecolor='w')
plt.xticks(())
plt.grid(True)
for x, y in zip(x6, y6_1): # 正轴序列添加标签
plt.text(x, y +0.05, '%d' % y, ha='center',va= 'bottom')
for x, y in zip(x6, y6_2): # 负轴序列添加标签
plt.text(x, -y-0.05, '%d' % y, ha='center', va='top')
# 绘制饼图
ax7=fig.add_subplot(247) # 子图7
# ax7.axis('equal')
x7= [ 'Nokia','Samsung', 'Apple',' Lumia'] # 饼图标签序列
y7= [10,30,45,15] # 定义百分比序列
colors = ['yellow','green','red','blue'] # 颜色序列
explode = [0.3,0,0,0] # 抽离效果显示,参数序列
ax7.pie(y7,labels=x7,colors=colors,autopct='%.2f%%',explode=explode,shadow=True,startangle=90)
# 用DataFrame绘制图形
ax8=fig.add_subplot(248)
dict8= {'series1':[1,3,4,3,5],
'series2':[2,4,5,2,4],
'series3':[3,2,3,1,3]} # 用于绘图的字典数据
# pd.DataFrame(dict8).plot(ax=ax8,kind='bar', stacked=True) # 绘制堆积条状图
# pd.DataFrame(dict8)['series1'].plot(ax=ax8,kind='pie',figsize=(6,6)) # 绘制饼图
plt.show()