1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > python数据可视化 matplotlib(2) 小白 - 典型图形进阶版:堆积折线图/并列柱状图/阶梯图/箱线图

python数据可视化 matplotlib(2) 小白 - 典型图形进阶版:堆积折线图/并列柱状图/阶梯图/箱线图

时间:2019-01-01 10:30:58

相关推荐

python数据可视化 matplotlib(2) 小白 - 典型图形进阶版:堆积折线图/并列柱状图/阶梯图/箱线图

今天依然是matplotlib,在入门版的基础上加入了更多的组合和样式设置,matplotlib库是建立在Numpy基础上的绘图库。

典型统计图形对比

柱状图

堆积图:将若干统计图形堆叠起来的统计图形,是一种组合式图形。

将函数bar()中的参数bottom取值设定为列表y,此时plt.bar(x,y1,bottom=y)输出堆积柱状图。

分块图:对比多数据分布差异。

将函数bar()中x的取值加上bar_width实现并列柱状图的输出。

平行条形图:调用函数由bar()变为barch()

import matplotlib as mplimport matplotlib.pyplot as pltimport numpy as npplt.rcParams['font.sans-serif'] = ['KaiTi'] # 指定默认字体plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题#datax=np.arange(5)y=[6,10,4,5,1]y1=[2,3,3,8,5]bar_width=0.35tick_label=["A","B","C","D","E"]plt.bar(x,y,bar_width,color="#66c2a5",edgecolor='black',label="班级A")'''这个函数会输出堆积柱状图'''plt.bar(x,y1,bar_width,color="#8da0cb",bottom=y,edgecolor='black',label="班级B") #这个函数会输出堆积柱状图plt.xticks(x,tick_label)'''这是多数据并列柱状图plt.bar(x+bar_width,y1,bar_width,color="#8da0cb",edgecolor="black",label="班级B") #这个函数输出多数据并列柱状图plt.xticks(x+bar_width/2,tick_label)'''plt.legend()plt.show()

参数调整

使用关键字参数hatch="//"设置柱状体的填充样式,如“/”、“//”、“o”、“*”等,每种符号字符串都是一种几何样式,符号数量越多,几何图形密集程度越高。

import matplotlib as mplimport matplotlib.pyplot as pltimport numpy as npplt.rcParams['font.sans-serif'] = ['KaiTi'] # 指定默认字体plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题#datax=np.arange(5)y=[6,10,4,5,1]y1=[2,3,3,8,5]bar_width=0.2tick_label=["A","B","C","D","E"]plt.bar(x,y,bar_width,color="#E2EFD9",edgecolor='black',label="班级A",hatch="/")plt.bar(x+bar_width,y1,bar_width,color="#DEEBF6",edgecolor="black",label="班级B",hatch="//") #这个函数输出多数据并列柱状图plt.bar(x+2*bar_width,y,bar_width,color="#FADBD2",edgecolor='black',label="班级C",hatch="x")plt.bar(x+3*bar_width,y1,bar_width,color="#FFF2CC",edgecolor="black",label="班级D",hatch="o") #这个函数输出多数据并列柱状图plt.xticks(x+1.5*bar_width,tick_label)plt.legend()plt.show()

堆积折线图 stackplot()

堆积折线图:绘制不同数据集的折现而生成的,按照垂直方向上彼此堆叠且又不相互覆盖的排列顺序的组合图形。每一个颜色断层代表一条折线所属的数据区域。

plt.stackplot(x,y)

import matplotlib.pyplot as pltimport numpy as npx=np.arange(1,6)y=[0,4,3,5,6]y1=[1,3,2,2,7]y2=[3,4,1,6,5]labels=["BluePlanet","PinkPlanet","YellowPlanet"]colors=["#DEEBF6","#FADBD2","#FFF2CC"]plt.stackplot(x,y,y1,y2,labels=labels,colors=colors,edgecolor='grey')plt.legend(loc="upper left")plt.show()

阶梯图

阶梯图:反映数据的趋势变化或是周期规律,使用在时间序列数据的可视化任务中,凸显时序数据的波动周期和规律。

plt.step(x,y)

x = np.linspace(1,10,10)y = xplt.step(x,y,color="#F1937A",where="pre",lw=2)# where="post"plt.xlim(0,11)plt.xticks(np.arange(1,11,1))plt.ylim(0,11)plt.show()

间断条形图

间断条形图:实现定性数据的相同指标在时间维度下的变化情况。

ax.broken_barh([(110, 30), (150, 10)], (10, 9), facecolors='tab:blue'),各参数含义已解释在注释中。

import matplotlib.pyplot as pltfig, ax = plt.subplots()ax.broken_barh([(110, 30), (150, 10)], (10, 9), facecolors='tab:blue')ax.broken_barh([(10, 50), (100, 20), (130, 10)], (20, 9), #从10开始沿x轴正向移动50个单位; 参数(20,9)是指y轴数值从20开始facecolors=('tab:orange', 'tab:green', 'tab:red'))ax.set_ylim(5, 35)ax.set_xlim(0, 200)ax.set_xlabel('seconds since start')ax.set_yticks([15, 25])ax.set_yticklabels(['Bill', 'Jim'])ax.grid(True)ax.annotate('race interrupted', (61, 25), #箭头xytext=(0.8, 0.9), textcoords='axes fraction',arrowprops=dict(facecolor='black', shrink=0.05),fontsize=16,horizontalalignment='right', verticalalignment='top')plt.show()

箱线图

箱线图的组成部分:箱体、箱须和离群值,其中箱体主要由第一四分位数、中位数和第三四分位数组成。

import numpy as npimport matplotlib.pyplot as pltplt.rcParams['font.sans-serif'] = ['KaiTi'] # 指定默认字体plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题# Fixing random state for reproducibilitynp.random.seed(19680801)# fake up some dataspread = np.random.rand(50) * 100center = np.ones(25) * 50flier_high = np.random.rand(10) * 100 + 100flier_low = np.random.rand(10) * -100data = np.concatenate((spread, center, flier_high, flier_low))###############################################################################fig1, ax1 = plt.subplots()ax1.set_title('箱线图基本版(1 of 6)')ax1.boxplot(data)###############################################################################fig2, ax2 = plt.subplots()ax2.set_title('V型凹痕箱线图(2 of 6)')ax2.boxplot(data, notch=True)###############################################################################green_diamond = dict(markerfacecolor='g', marker='D')fig3, ax3 = plt.subplots()ax3.set_title('箱线图改离群点格式(3 of 6)')ax3.boxplot(data, flierprops=green_diamond)###############################################################################fig4, ax4 = plt.subplots()ax4.set_title('箱线图隐藏离群点格式(4 of 6)')ax4.boxplot(data, showfliers=False)###############################################################################red_square = dict(markerfacecolor='r', marker='s')fig5, ax5 = plt.subplots()ax5.set_title('箱线图水平放(5 of 6)')ax5.boxplot(data, vert=False, flierprops=red_square)################################################################################ Fake up some more dataspread = np.random.rand(50) * 100center = np.ones(25) * 40flier_high = np.random.rand(10) * 100 + 100flier_low = np.random.rand(10) * -100d2 = np.concatenate((spread, center, flier_high, flier_low))###############################################################################data = [data, d2, d2[::2]]fig7, ax7 = plt.subplots()ax7.set_title('多个箱线图(6 of 6)')ax7.boxplot(data)plt.show()

动态版箱线图可以点击 介个:/vv_eve/article/details/107555948

动态版柱状图需要点击 介个

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。