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()

绘制好的柱状图如下:

python中如何利用matplotlib画多个并列的柱状图

画两组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()

绘制好的柱状图如下:

python中如何利用matplotlib画多个并列的柱状图

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()

python中如何利用matplotlib画多个并列的柱状图

以上

来源:https://blog.csdn.net/weixin_44293949/article/details/114590319

标签:python,matplotlib,柱状图
0
投稿

猜你喜欢

  • python模拟表单提交登录图书馆

    2021-11-06 19:20:42
  • python GUI库图形界面开发之PyQt5开发环境配置与基础使用

    2023-11-16 04:45:22
  • django实现将后台model对象转换成json对象并传递给前端jquery

    2023-01-06 22:17:01
  • javascript中类的创建和继承

    2008-05-08 12:07:00
  • DIV与Table布局在大型网站的可用性比较

    2007-10-08 12:46:00
  • asp伪继承初探_实例代码

    2011-04-19 10:32:00
  • PHP基于phpqrcode类生成二维码的方法示例详解

    2023-07-15 22:57:52
  • xWin的HTC分享

    2009-09-13 18:50:00
  • python中实现词云图的示例

    2021-08-04 22:11:33
  • 有关简洁网页设计需知的6点技巧

    2012-04-25 20:55:01
  • Python中列表与元组的乘法操作示例

    2021-05-09 17:11:25
  • python time模块时间戳 与 结构化时间详解

    2021-04-09 11:06:42
  • python pands实现execl转csv 并修改csv指定列的方法

    2022-11-20 01:45:27
  • JavaScript 关于引用那点事

    2009-11-28 18:44:00
  • 天极网页版式设计的思考

    2008-01-18 12:44:00
  • python 实现全球IP归属地查询工具

    2023-10-05 16:31:33
  • Google中国新首页风格再度变脸

    2008-10-27 13:37:00
  • 理解和使用Oracle 8i分析工具LogMiner

    2010-07-16 13:22:00
  • python常见的格式化输出小结

    2022-12-29 20:23:31
  • SQL语句练习实例之三——平均销售等待时间

    2011-10-24 20:11:47
  • asp之家 网络编程 m.aspxhome.com