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

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

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

  1. 画多组两个并列的柱状图:
import matplotlib
import matplotlib.pyplot as plt
import numpy as npplt.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 barsfig, 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 offsettextcoords="offset points",ha='center', va='bottom')autolabel(rects1)
autolabel(rects2)fig.tight_layout()plt.show()

绘制好的柱状图如下:

  1. 画两组5个并列的柱状图:
import matplotlib
import matplotlib.pyplot as plt
import numpy as npplt.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 npplt.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画多个并列的柱状图(展示3种图)相关推荐

  1. python如何画出多个独立的图片_在Python中用Matplotlib绘制多个图形并组合显示,利用,多图,合并,展示...

    有个需求就是利用Matplotlib画几个像模像样的统计图然后合并在一张图中,因为此前很少用这方面的东西,所以折腾了不少时间,今天介绍一下. 1.subplot多合一 其实,利用python 的mat ...

  2. python中用matplotlib画多幅图时出现图形部分重叠的解决方案

    1.解决方法:使用函数  tight_layout() 2.具体使用方法 import matplotlib.pyplot as pltfig = plt.figure()''' 具体的画图程序 '' ...

  3. python 画柱状图-Python 使用 matplotlib 画柱状图教程

    Python 使用 matplotlib 画图是非常方便的,之前的文章记录了<Python 使用 matplotlib 画折线图教程>,今天就再次记录一下使用 matplotlib 画柱状 ...

  4. python画柱状图-Python 使用 matplotlib 画柱状图教程

    Python 使用 matplotlib 画图是非常方便的,之前的文章记录了<Python 使用 matplotlib 画折线图教程>,今天就再次记录一下使用 matplotlib 画柱状 ...

  5. python使用matplotlib 画柱状图代码_Python 使用 matplotlib 画柱状图教程

    Python 使用 matplotlib 画图是非常方便的,之前的文章记录了<Python 使用 matplotlib 画折线图教程>,今天就再次记录一下使用 matplotlib 画柱状 ...

  6. Python中用Matplotlib做多个纵轴(多y轴)

    转载自:Python中用Matplotlib做多个纵轴(多y轴) Matlab里做多给轴的函数很直接,双轴是plotyy, 三轴是plotyyy, 四轴是plot4y,更多应该是multiplotyy ...

  7. 如何使用python画折线图-Python 使用 matplotlib 画折线图教程

    话不多说,直接上代码.最近修改一篇论文,实验部分的图全部重画了一下,也正好在此进行整理.使用 Python 的 matplotlib 库来画图还是比较方便的,今天整理一下用 matplotlib 画折 ...

  8. python怎么画条形图-python使用Matplotlib画条形图

    本文实例为大家分享了python使用Matplotlib画条形图的具体代码,供大家参考,具体内容如下 数据 中国的四个直辖市分别为北京市.上海市.天津市和重庆市,其2017年上半年的GDP分别为124 ...

  9. python画饼图-python使用Matplotlib画饼图

    本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下 函数参数 plt.pie(x, explode=None, labels=None, colors=None, a ...

最新文章

  1. 让计算机自己写作文,写电脑的作文300字左右
  2. Linux and the Device Tree
  3. 【Cson原创】javascript中length属性的探索
  4. 《杀死比尔1 Kill Bill Volume One》电影原声Cool!
  5. 2020各大网站rss订阅源地址_RSS订阅器inoreader一些使用心得
  6. css的属性是变量是怎么表达,CSS自定义属性(变量)
  7. 函数遍历IOS中block的使用
  8. 常见RPM,YUM,DNF指令
  9. 在Linux下如何查CC攻击?
  10. 全球首款5G手机出炉?其实只是一个笑话
  11. 实战 | F1060路由模式ISIS典型组网配置案例
  12. LVS-DR工作原理图文详解(转载)
  13. 金山词霸没有响应, 金山词霸与卡巴冲突问题解决方法?
  14. docker修改容器ip范围
  15. logisitic 回归 +极大似然法 + 梯度下降法 (迭代优化)
  16. 自然语言处理(NLP)和语音识别(ASR)的区别
  17. 基于python提火车票信息_python3.X 抓取火车票信息【修正版】
  18. js截取指定字符串后面的所有字符
  19. c++11的regex使用
  20. 抖音表情包项目怎么变现?普通人如何利用表情包短视频月入过万?

热门文章

  1. 程序员家的孩子休学是怎么书写休学申请书的
  2. 计算机存储健,电脑健盘关机是什么健
  3. 多元生态布局,天九共享保障联营企业家收益
  4. 看漫画学知识:详解获得苹果推荐的4大要素
  5. 五个安卓黑科技手机app,看看不如用用!心动不如行动哦
  6. [Mugeda HTML5技术教程之10]发布内容
  7. 基于FPGA的通信显示系统设计
  8. 【uni-app系列】uni-app之App打包
  9. Flink整合Drools规则引擎
  10. 【Echarts】通过柱状图实例,一文让你学会Echarts的基础使用!!!