python中如何利用matplotlib画多个并列的柱状图
作者:哇咔君i 时间:2022-04-14 12:38:42
首先如果柱状图中有中文,比如X轴和Y轴标签需要写中文,解决中文无法识别和乱码的情况,加下面这行代码就可以解决了:
plt.rcParams['font.sans-serif'] = ['SimHei'] # 解决中文乱码
以下总共展示了三种画不同需求的柱状图:
画多组两个并列的柱状图:
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = ['SimHei']
labels = ['G1', 'G2', 'G3', 'G4', 'G5']
men_means = [20, 34, 30, 35, 27]
women_means = [25, 32, 34, 20, 25]
x = np.arange(len(labels)) # the label locations
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, men_means, width, label='Men')
rects2 = ax.bar(x + width/2, women_means, width, label='Women')
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()
def autolabel(rects):
"""Attach a text label above each bar in *rects*, displaying its height."""
for rect in rects:
height = rect.get_height()
ax.annotate('{}'.format(height),
xy=(rect.get_x() + rect.get_width() / 2, height),
xytext=(0, 3), # 3 points vertical offset
textcoords="offset points",
ha='center', va='bottom')
autolabel(rects1)
autolabel(rects2)
fig.tight_layout()
plt.show()
绘制好的柱状图如下:
画两组5个并列的柱状图:
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif']=['SimHei'] # 解决中文乱码
labels = ['第一项', '第二项']
a = [4.0, 3.8]
b = [26.9, 48.1]
c = [55.6, 63.0]
d = [59.3, 81.5]
e = [89, 90]
x = np.arange(len(labels)) # 标签位置
width = 0.1 # 柱状图的宽度,可以根据自己的需求和审美来改
fig, ax = plt.subplots()
rects1 = ax.bar(x - width*2, a, width, label='a')
rects2 = ax.bar(x - width+0.01, b, width, label='b')
rects3 = ax.bar(x + 0.02, c, width, label='c')
rects4 = ax.bar(x + width+ 0.03, d, width, label='d')
rects5 = ax.bar(x + width*2 + 0.04, e, width, label='e')
# 为y轴、标题和x轴等添加一些文本。
ax.set_ylabel('Y轴', fontsize=16)
ax.set_xlabel('X轴', fontsize=16)
ax.set_title('这里是标题')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()
def autolabel(rects):
"""在*rects*中的每个柱状条上方附加一个文本标签,显示其高度"""
for rect in rects:
height = rect.get_height()
ax.annotate('{}'.format(height),
xy=(rect.get_x() + rect.get_width() / 2, height),
xytext=(0, 3), # 3点垂直偏移
textcoords="offset points",
ha='center', va='bottom')
autolabel(rects1)
autolabel(rects2)
autolabel(rects3)
autolabel(rects4)
autolabel(rects5)
fig.tight_layout()
plt.show()
绘制好的柱状图如下:
3. 要将柱状图的样式画成适合论文中使用的黑白并且带花纹的样式:
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = ['SimHei'] # 解决中文乱码
labels = ['第一项', '第二项']
a = [50, 80]
b = [37, 69]
c = [78, 60]
d = [66, 86]
e = [80, 95]
# marks = ["o", "X", "+", "*", "O"]
x = np.arange(len(labels)) # 标签位置
width = 0.1 # 柱状图的宽度
fig, ax = plt.subplots()
rects1 = ax.bar(x - width * 2, a, width, label='a', hatch="...", color='w', edgecolor="k")
rects2 = ax.bar(x - width + 0.01, b, width, label='b', hatch="oo", color='w', edgecolor="k")
rects3 = ax.bar(x + 0.02, c, width, label='c', hatch="++", color='w', edgecolor="k")
rects4 = ax.bar(x + width + 0.03, d, width, label='d', hatch="XX", color='w', edgecolor="k")
rects5 = ax.bar(x + width * 2 + 0.04, e, width, label='e', hatch="**", color='w', edgecolor="k")
# 为y轴、标题和x轴等添加一些文本。
ax.set_ylabel('Y', fontsize=16)
ax.set_xlabel('X', fontsize=16)
ax.set_title('标题')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()
以上
来源:https://blog.csdn.net/weixin_44293949/article/details/114590319
标签:python,matplotlib,柱状图
0
投稿
猜你喜欢
MySQL的时间差函数(TIMESTAMPDIFF、DATEDIFF)、日期转换计算函数(date_add、day、date_format、str_to_date)
2024-01-27 07:05:37
在WordPress的后台中添加顶级菜单和子菜单的函数详解
2024-05-13 09:25:12
MYSQL必知必会读书笔记 第一章(基础)
2024-01-20 09:23:52
layui 数据表格 点击分页按钮 监听事件的实例
2024-04-19 10:45:12
jQuery技巧
2009-09-27 12:28:00
windows支持哪个版本的python
2023-05-15 08:42:16
Python torch.fft.rfft()函数用法示例代码
2022-02-15 02:03:36
Javascript实现的鼠标经过时播放声音
2010-05-18 20:03:00
JS 简单展开关闭切换代码
2024-05-05 09:14:30
Python开发微信公众平台的方法详解【基于weixin-knife】
2023-03-09 12:05:43
用SQL语句添加删除修改字段、一些表与字段的基本操作、数据库备份等
2024-01-26 16:53:22
python笔记之使用fillna()填充缺失值
2023-12-22 19:53:49
Python运行DLL文件的方法
2021-12-25 15:23:21
Python socket C/S结构的聊天室应用实现
2023-08-01 05:06:38
SQL Server Parameter Sniffing及其改进方法
2024-01-28 02:56:30
javascript的this关键字详解
2024-04-17 10:08:44
python实现控制电脑鼠标和键盘,登录QQ的方法示例
2023-11-19 12:10:26
Windows 本地安装 Mysql8.0图文教程
2024-01-15 17:24:05
教你怎么用Python操作MySql数据库
2024-01-13 06:46:48
如何在ADSI中查询用户属性?
2010-06-17 12:53:00