Python 画图(二):柱状图

您所在的位置:网站首页 python数据集绘图 Python 画图(二):柱状图

Python 画图(二):柱状图

#Python 画图(二):柱状图| 来源: 网络整理| 查看: 265

本文目标在于利用 Python 快速画出符合自己要求的柱状图。

最近在处理某一组成绩数据的时候,涉及了柱状图的画法,因此此处进行一下记录。

加载库import matplotlib.pyplot as plt import matplotlib.font_manager as mfm from matplotlib import style style.use('ggplot') # 加载'ggplot'风格 # 加载中文字体 font_path = "/System/Library/Fonts/STHeiti Light.ttc" # 本地字体链接 prop = mfm.FontProperties(fname=font_path)单一柱图

其中 total[i] 表示分数在 [i*10,(i+1)*10] 区间内的人数。

total = [3, 5, 6, 7, 8, 6, 4, 3, 3, 22, 4, 8, 7, 13, 7, 7, 15, 10, 6, 52, 8, 2, 3, 26, 1, 1, 0, 2, 0, 3]

如果直接对于上述数据执行下述代码,将得到如下结果。

plt.bar(range(len(total)), total) plt.title('单一柱图', fontproperties=prop) plt.savefig("单一柱图.png", dpi=700, fontproperties=prop) plt.show()在这里插入图片描述

不难发现,上述结果的效果不好。首先第一点,分数段是 [0,300],而此处显示的是 /10 后的数值。其次,最后一根柱子没有数据显示,但其实应该显示 [290,300] 的数据,因此我们需要对此进行改进。

自定义横坐标

因此我们选择自定义横坐标的方式来进行效果改进,具体代码如下。

x_labels = [] for item in range(0, 300, 10): x = item + 10 if x == 10: x_labels.append("{}~{}".format(0, 10)) elif x % 50 == 0: x_labels.append("{}".format(x)) else: x_labels.append(None) x = range(len(total)) plt.bar(x, total) plt.title('单一柱图', fontproperties=prop) plt.xticks(x, x_labels) plt.savefig("单一柱图.png", dpi=700, fontproperties=prop) plt.show()

横坐标还可以调整字体大小以及旋转角度:

plt.xticks(x, x_labels, rotation=30, fontsize=5)

我们在 30 个区间中仅选取了一部分进行标注,以及令第一个区间以 0~10 的方式进行显示。

在这里插入图片描述多柱图

在展示完单一柱图之后,我们进入多柱同时显示的代码内容。首先是数据内容的展示,A[i][j] 表示第 i 题得分在 [j*10,(j+1)*10] 范围内的人数。

A = [[28, 3, 5, 6, 3, 7, 2, 4, 9, 95], [58, 6, 13, 13, 5, 12, 17, 11, 10, 102], [60, 22, 21, 41, 11, 5, 1, 2, 3, 4]]

然后我们执行下述代码即可得到下述多柱图。

# 生成横坐标 x_labels = [] for item in range(0, 100, 10): x = item + 10 if x == 10: x_labels.append("{}~{}".format(0, 10)) else: x_labels.append("{}".format(x)) # 生成横坐标范围 x = np.arange(10) # 生成多柱图 plt.bar(x + 0.00, A[0], color='orange', width=0.3, label="A") plt.bar(x + 0.30, A[1], color='royalblue', width=0.3, label="B") plt.bar(x + 0.60, A[2], color='brown', width=0.3, label="C") # 图片名称 plt.title('多柱图', fontproperties=prop) # 横坐标绑定 plt.xticks(x + 0.30, x_labels) # 生成图片 plt.legend(loc="best") plt.savefig("多柱图.png", dpi=700, fontproperties=prop) plt.show()在这里插入图片描述其它参数示例

最后,我们尝试一下改变图形风格,以及将 y 轴变为 log 型增长。另外,其中的 hatch 参数为柱子上的阴影选项,例如可以为 '\\'。

import numpy as np import matplotlib import matplotlib.pyplot as plt from matplotlib import style plt.figure(figsize=(15, 5), dpi=70) # 设置图像大小 x_labels = ['adult-a', 'cod-rna', 'gisette', 'madelon', 'magic04', 'mini-boo-ne', 'mushrooms', 'phishing', 'real-sim', 'splice', 'w8a'] x_values = [ [6.089643839999999, 5.344304520000001, 0.4359904, 9.83603284, 6.41019676, 308.755843, 0.03612756, 0.8888024399999999, 3.5747096, 0.5192512, 1.53061884], [4.4124038, 5.535605, 244.2987548, 10.5653444, 2.2559656, 59.1372296, 1.5807804, 2.9477072000000004, 32.0803932, 1.7867012000000002, 7.2184973999999995], [3.7415935999999994, 0.5491254000000001, 3.1457894, 0.10271919999999998, 0.0997276, 298.7826466, 0.26350219999999996, 0.1412342, 7.425538800000001, 0.0494696, 3.9464446000000004] ] models = ['SVM', 'ODM', 'IODM'] color = ['#4473c5', '#ec7e32', '#a5a5a5'] hatch = ['', '', ''] def draw_time(models, x_labels, x_values, color, hatch): plt.cla() x = np.arange(len(x_labels)) * 2 for i in range(3): plt.bar(x + 0.5 * i, x_values[i], color=color[i], hatch=hatch[i], width=0.5, label=models[i], edgecolor='black', linewidth=0.2, alpha=1) plt.xticks(x + 0.60, x_labels) ax = plt.gca() ax.set_yscale('log', basey=10) ## ax.set_yticks([10**(-2), 10**(-1), 10**(0), 10**(1), 10**(2)]) plt.legend(loc="best", prop={"size":14}) plt.xlabel('Data sets', fontsize=16) plt.ylabel('CPU time (sec)', fontsize=16) ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') plt.rc('axes', axisbelow=True) plt.grid(linestyle = '--', linewidth = 0.3, color= 'gray', alpha = 0.2) plt.savefig('time.pdf', dpi=700) draw_time(models, x_labels, x_values, color, hatch)在这里插入图片描述后记

至此,Python 柱状图 的基础操作就介绍完毕了,本文也算是对于上一篇 Python 画图(一):各类基础操作 文章的一个补充,不过仍然还有很多其他类型图的画法没有介绍,感兴趣的朋友可以继续深入研究!

最后祝大家画图快乐,在 Python 的画图之路上更进一步!



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3