脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python中如何利用matplotlib画多个并列的柱状图

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

2022-08-28 11:59哇咔君i Python

python是一个很有趣的语言,可以在命令行窗口运行,下面这篇文章主要给大家介绍了关于python中如何利用matplotlib画多个并列的柱状图的相关资料,需要的朋友可以参考下

首先如果柱状图中有中文,比如X轴和Y轴标签需要写中文,解决中文无法识别和乱码的情况,加下面这行代码就可以解决了:

?
1
plt.rcParams['font.sans-serif'] = ['SimHei']   # 解决中文乱码

以下总共展示了三种画不同需求的柱状图:

画多组两个并列的柱状图:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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个并列的柱状图:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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. 要将柱状图的样式画成适合论文中使用的黑白并且带花纹的样式:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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画多个并列的柱状图

以上

总结

到此这篇关于python中如何利用matplotlib画多个并列柱状图的文章就介绍到这了,更多相关python matplotlib画并列柱状图内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_44293949/article/details/114590319

延伸 · 阅读

精彩推荐
  • PythonDjango框架模板介绍

    Django框架模板介绍

    今天小编就为大家分享一篇关于Django框架模板介绍,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧...

    小茗同学。5422021-05-15
  • Pythonzookeeper python接口实例详解

    zookeeper python接口实例详解

    这篇文章主要介绍了zookeeper python接口实例详解,具有一定借鉴价值,需要的朋友可以参考下...

    swpihchj11442021-01-06
  • Python用Python简单实现个贪吃蛇小游戏(保姆级教程)

    用Python简单实现个贪吃蛇小游戏(保姆级教程)

    本文基于Windows环境开发,适合Python新手,文中有非常详细的代码示例,对正在学习python的小伙伴们很有帮助,需要的朋友可以参考下...

    克金森沐沐8972021-11-23
  • PythonPython 查看list中是否含有某元素的方法

    Python 查看list中是否含有某元素的方法

    今天小编就为大家分享一篇Python 查看list中是否含有某元素的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    Recar12542021-03-10
  • PythonPython Selenium库的基本使用教程

    Python Selenium库的基本使用教程

    这篇文章主要给大家介绍了关于Python Selenium库的基本使用教程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    凯耐9432021-08-21
  • Pythonpytorch实现特殊的Module--Sqeuential三种写法

    pytorch实现特殊的Module--Sqeuential三种写法

    今天小编就为大家分享一篇pytorch实现特殊的Module--Sqeuential三种写法。具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 ...

    fly_Xiaoma3712020-04-21
  • PythonWindows下python3.7安装教程

    Windows下python3.7安装教程

    这篇文章主要为大家详细介绍了Windows下python3.7安装教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    duandian0110462021-03-24
  • Pythondjango框架之cookie/session的使用示例(小结)

    django框架之cookie/session的使用示例(小结)

    这篇文章主要介绍了django框架之cookie/session的使用示例(小结),详细的介绍了cookie和session技术的接口获取等问题,具有一定的参考价值,感兴趣的小伙伴们...

    zzzzou11932021-04-07