对比Excel学Python(二)数据可视化
就是利用Python生成各种图表,也是本书的核心。
1、条形图
#导入要用的matplotlib库
import matplotlib.pyplot as plt import numpy as np
#解决乱码问题 plt.rcParams["font.sans-serif"]='SimHei'
#(在Y轴上分为1等份,在X轴上分为1等份,画布位于1象限)
plt.subplot(1,1,1) #传入基础数据 x = np.array(["东区","南区","西区","北区"]) y1 = np.array([7566,6555,5335,6310]) y2 = np.array([4500,4555,3335,5310]) #设置基本属性 plt.title("柱线图",loc="center") plt.xlabel("分区") plt.ylabel("任务量") plt.barh(x,height=0.5,label = "任务量",width = y1) #显示图例 plt.legend()
#不显示网格 plt.grid(False) #迭代赋值 for a,b in zip(x,y1): plt.text(b,a,a,ha="center",va="bottom",fontsize = 12)
#将图片存入桌面 plt.savefig(r"C:\Users\admin\Desktop\新建文件夹\条形图")
2、折线图
#折线图 import matplotlib.pyplot as plt import numpy as np %matplotlib inline plt.rcParams["font.sans-serif"]='SimHei' plt.subplot(1,1,1) # plt.subplots(1,1) plt.xlabel("月份",) plt.ylabel("注册量") # plt.xticks(ticks,labels) # plt.yticks(ticks,labels) # plt.xticks(np.arange(12),["0","1月份","2月份","3月份","4月份","5月份","6月份","7月份","8月份","9月份","10月份","11月份"]) # plt.yticks(np.arange(1000,7000,1000),["1000人","2000人","3000人","4000人","5000人","6000人","7000","8000"]) plt.xticks(np.arange(12)) x = np.array([1,2,3,4,5,6,7,8,9,10,11]) y = np.array([866,2335,5710,6482,6120,1605,3813,4428,4631,1001,1002]) plt.plot(x,y,color = "r",linestyle = "dashdot",linewidth = 1,marker = "v",markersize = 5,label = "注册用户数") # linewidth = 1,marker = "o",) plt.title("XXX公司1-9月注册用户量",loc = "center") for a,b in zip(x,y): plt.text(a,b,b,ha='center',va = 'bottom',fontsize = 10) plt.grid(b = True) plt.legend() # plt.savefig(r"C:\Users\admin\Desktop\新建文件夹\折线图")
3、气泡图
#气泡图 import matplotlib.pyplot as plt import numpy as np %matplotlib inline plt.rcParams["font.sans-serif"]='SimHei' plt.subplot(1,1,1) plt.xlabel("月份",) plt.ylabel("注册量") plt.title("XXX公司1-9月注册用户量",loc = "center") x = np.array([1,2,3,4,5,6,7,8,9,10,11]) y = np.array([6,35,10,82,20,15,13,28,31,10,12]) # colors = y*10 #无用? area = y*20 plt.scatter(x,y,marker = "o",s = area) for a,b in zip(x,y): plt.text(a,b,b,ha='center',va = 'center',fontsize = 12,color = "white") plt.savefig(r"C:\Users\admin\Desktop\新建文件夹\气泡图")
4、柱形图-堆积图
#柱形图 import matplotlib.pyplot as plt import numpy as np plt.rcParams["font.sans-serif"]='SimHei' plt.subplot(1,1,1) # x = np.array(["东区","南区","西区","北区"]) x = np.array([1,2,3,4]) plt.xticks(x+0.1,["东区","南区","西区","北区"]) y1 = np.array([7566,6555,5335,6310]) y2 = np.array([4500,4555,3335,5310]) plt.title("柱线图",loc="center") plt.xlabel("分区") plt.ylabel("任务量") plt.bar(x,y1,label = "任务量",width = 0.3) # plt.bar(x+0.3,y2,label = "完成量",width = 0.3) plt.bar(x,y2,label = "完成量",width = 0.3) plt.legend() plt.grid(False) for a,b in zip(x,y1): plt.text(a,b,b,ha="center",va="bottom",fontsize = 12) # for a,b in zip(x+0.3,y2): # plt.text(a,b,b,ha="center",va="bottom",fontsize = 12) for a,b in zip(x,y2): plt.text(a,b,b,ha="center",va="bottom",fontsize = 12) # plt.savefig(r"C:\Users\admin\Desktop\新建文件夹\柱形图") plt.savefig(r"C:\Users\admin\Desktop\新建文件夹\堆积图")
5、面积图
#面积图 import matplotlib.pyplot as plt import numpy as np %matplotlib inline plt.rcParams["font.sans-serif"]='SimHei' plt.subplot(1,1,1) plt.xlabel("月份",) plt.ylabel("注册量") plt.title("XXX公司1-9月注册用户量",loc = "center") x = np.array([1,2,3,4,5,6]) y1 = np.array([6360,6555,5335,6310,5357,6666]) y2 = np.array([4500,4555,3335,5310,4444,5674]) plt.stackplot(x,y1,y2) plt.savefig(r"C:\Users\admin\Desktop\新建文件夹\面积图")
6、树地图
#树地图 #squarify.plot(size,label,color,value,edgecolor,linewidth)r import matplotlib.pyplot as plt import numpy as np %matplotlib inline plt.rcParams["font.sans-serif"]='SimHei' import squarify size = np.array([3.4,0.693,0.585,0.570,0.562,0.531,0.530,0.524,0.501,0.478,0.468,0.436]) xingzuo = np.array(["未知","摩揭座","天秤座","双鱼座","天竭座","金牛座", "处女座","双子座","射手座","狮子座","水瓶座","白羊座"]) rate = np.array(['34%','6.93%',"5.85%","5.70%","5.62%","5.31%","5.30%","5.24%","5.01%","4.78%","4.68%","4.36%"]) colors = ['steelblue','#9999ff','red','indianred','green','yellow','orange'] plot = squarify.plot(sizes = size, label = xingzuo, color = colors, value = rate, edgecolor = "white", linewidth = 3) plt.title("星座",fontdict = {'fontsize':12}) plt.axis("off") # plt.tick_params(top = 'off',right = 'off') plt.savefig(r"C:\Users\admin\Desktop\新建文件夹\树地图")
7、饼图
import matplotlib.pyplot as plt import numpy as np x = np.array([5555,6666,7777,8888]) labels = ["A","B","C","D"] explode = [0.1,0,0,0] labeldistance = 1.1 plt.pie(x,labels=labels,autopct='%.1f%%',shadow=True,explode = explode,radius=1.0,labeldistance=labeldistance) # explode = explode,radius=1.0,labeldistance=labeldistance) #错误示范
8、双环形图
这个是从网上找的案例,一起总结在一块。
import matplotlib as mpl import matplotlib.pyplot as plt # 设置图片大小 plt.figure(figsize = (10, 8)) # 生成数据 labels = ['A', 'B', 'C', 'D', '其他'] share_laptop = [0.45, 0.25, 0.15, 0.05, 0.10] share_pc = [0.35, 0.35, 0.08, 0.07, 0.15] colors = ['c', 'r', 'y', 'g', 'gray'] # 外环 wedges1, texts1, autotexts1 = plt.pie(share_laptop, autopct = '%3.1f%%', radius = 1, pctdistance = 0.85, colors = colors, startangle = 180, textprops = {'color': 'w'}, wedgeprops = {'width': 0.3, 'edgecolor': 'w'} ) # 内环 wedges2, texts2, autotexts2 = plt.pie(share_pc, autopct = '%3.1f%%', radius = 0.7, pctdistance = 0.75, colors = colors, startangle = 180, textprops = {'color': 'w'}, wedgeprops = {'width': 0.3, 'edgecolor': 'w'} ) # 图例 plt.legend(wedges1, labels, fontsize = 12, title = '公司列表', loc = 'center right', bbox_to_anchor = (1, 0.6)) # 设置文本样式 plt.setp(autotexts1, size=15, weight='bold') plt.setp(autotexts2, size=15, weight='bold') plt.setp(texts1, size=15) # 标题 plt.title('2017年笔记本及PC电脑市场份额', fontsize=20) plt.savefig(r"C:\Users\admin\Desktop\新建文件夹\环形图") plt.show()