利用Matplotlib打造生动数据可视化(第二部分)

图片[1]-利用Matplotlib打造生动数据可视化(第二部分)-山海云端论坛

在上一节中我们介绍了使用matplotlib进行数据可视化的几种表现方法,包括折线图、散点图、饼状图以及柱状图; 今天我们来继续学习堆叠图、树地图、箱型图和提琴图,那么我们开始吧。

堆叠图和树地图

堆叠图可以很方便的比较数据间不同情况下的差异。

树地图的思想就是通过方块的面积来表示,面积越大,其代表的值就越大,反之亦然。

图片[2]-利用Matplotlib打造生动数据可视化(第二部分)-山海云端论坛
可视化结果:
  • 堆叠图:左上占比100%的堆叠面积图。它用于表示各种数据集而不会彼此重叠,它显示了每个组件相互堆叠以及每个组件如何构成完整的图形。其中各个组件可以用不同的颜色表示。
  • 树地图:右上借助squarify显示树地图。
代码如下:
<code>def test1(): # left data M, N = 16, 4 dadosEmp = np.random.random((N, M)) * 0.9 + 0.1 empilha = 100 * dadosEmp / np.sum(dadosEmp, axis=0) # right data folhas = 64 area = np.random.random(folhas) * 3 + 1 area = np.round_(area, decimals=2) cores = np.random.random(folhas) lado = area.sum() ** 0.5 # param cmapArvore = cm.get_cmap('rainbow') cores = cmapArvore(cores) from squarify import squarify partes = squarify(area, 0, 0, lado, lado) x = [parte['x'] for parte in partes] y = [parte['y'] for parte in partes] dx = [parte['dx'] for parte in partes] dy = [parte['dy'] for parte in partes] fig, (axA, axB) = plt.subplots(1, 2) # draw left axA.stackplot(np.arange(M), empilha, baseline='zero') axA.set_title('堆叠图') axA.set_ylabel('比率') axA.set_xticks(np.arange(M)) axA.set_yticks(np.linspace(0, 100, M)) axA.set_xticklabels([chr(i + ord('a')) for i in range(M)]) axA.legend(['G{}'.format(i + 1) for i in range(N)]) axA.grid(alpha=0.75, linestyle=':') # draw right axB.bar(x, dy, width=dx, bottom=y, color=cores, align='edge') for p, a in zip(partes, area): x, y, dx, dy = p['x'], p['y'], p['dx'], p['dy'] axB.text(x + dx * 0.5, y + dy * 0.5, a, va='center', ha='center') axB.set_title('树地图') plt.show()</code>

箱型图和提琴图

箱型图(Box Plot)于 1977 年由美国著名统计学家约翰·图基(John Tukey)发明。它能显示出一组数据的最大值、最小值、中位数、及上下四分位数。

小提琴图(Violin Plot)是用来展示数据分布状态以及概率密度的图表。这种图表结合了箱形图和密度图的特征

图片[3]-利用Matplotlib打造生动数据可视化(第二部分)-山海云端论坛
可视化结果:
  • 小提琴绘图:左上箱图和小提琴图形并列显示。
  • 直方图:右上堆叠直方图显示。
代码如下:
<code>def test2(): # 统计数据 entrev_dia = 1000 dias = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] ndias = len(dias) mu = 4 + np.random.random(ndias) * 2 sigma = 0.5 + np.random.random(ndias) * 2 horas = np.random.normal(mu, sigma, (entrev_dia, ndias)) horas += np.random.random((entrev_dia, ndias)) * 2 - 1 # 显示参数 cmapStat = cm.get_cmap('cool') posicao = np.arange(ndias) * 1.5 fig, (axA, axB) = plt.subplots(1, 2) # 箱图和小提琴图 bplots = axA.boxplot(horas, positions=posicao - 0.25, vert=True, widths=0.25, patch_artist=True, notch=True) violins = axA.violinplot(horas, positions=posicao + 0.25, widths=0.25, showmeans=True) for i, (box, violin) in enumerate(zip(bplots['boxes'], violins['bodies'])): cor = cmapStat(i / ndias) box.set_facecolor(cor) violin.set_facecolor(cor) violin.set_edgecolor('black') violin.set_alpha(0.75) axA.set_title('箱-小提琴图') axA.set_ylabel('睡眠时间') axA.set_xticks(posicao) axA.set_yticks(range(1, 10)) axA.set_xticklabels(dias) axA.set_xlim((-0.5, 6.5)) axA.grid(alpha=0.75, linestyle=':') # 直方图 n, bins, patches = axB.hist(horas, bins=50, stacked=True) for i, patchList in enumerate(patches): for patch in patchList: patch.set_facecolor(cmapStat(i / ndias)) axB.set_title('直方图') axB.set_xlabel('睡眠时间') axB.set_ylabel('人数统计') axB.legend(dias) plt.show()</code>

总结

本文详细地介绍了使用matplotlib画堆叠图、树地图、箱型图和提琴图的样例,并给出了相关可视化效果。

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容