首页 > 其他分享 >Matplotlib.plot 绘制条形图

Matplotlib.plot 绘制条形图

时间:2022-11-04 16:56:42浏览次数:28  
标签:plot plt bar index Matplotlib width matplotlib 条形图

Python代码:

# 导入第三方包
import matplotlib
import numpy as np
import matplotlib.pyplot as plt

# matplotlib其实是不支持显示中文的 显示中文需要额外设置
# 设置字体类型,宋体:SimSun  华文楷体:STKaiti  微软雅黑:Microsoft YaHei
matplotlib.rcParams['font.family'] = 'STKaiti'

# 设置字体尺寸
matplotlib.rcParams['font.size'] = 12

# 工作经验(年)
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# 开发岗月薪
y_dev = [15605, 19135, 21941, 24512, 32985, 34337, 35123, 36452, 37812, 39928, 42035]
# 产品岗月薪
y_prod = [9235, 10712, 11058, 12352, 17575, 18023, 18541, 19823, 20352, 21578, 24675]

# 创建画布  num: 画布名称(默认figure1)
# figsize=(8, 6)  dpi=100  相当于设置画布分辨率为 800 * 600
plt.figure(num='e_bar', figsize=(8, 6), dpi=100)

# 变成ndarray数组,便于 x + width 显示第二个条形图
x_index = np.array(x)
# 条形图宽度
width = 0.16

# bar(x, y, width=条形图宽度, label=标签)
plt.bar(x_index - width, y_dev, width=width * 2, label='软件研发')
plt.bar(x_index + width, y_prod, width=width * 2, label='产品经理')

# 设置横纵坐标标签
plt.xlabel(xlabel='工作经验(年)')
plt.ylabel(ylabel='月薪(元)')

# 设置X轴刻度
plt.xticks(ticks=x)

# 设置图像标签
plt.title(label='工作经验与月薪的关系图')

# 是否加上网格
plt.grid(False)

# 对空白区域进行自动填充
# plt.tight_layout()

# 借助zip方法按顺序同时枚举多个元素
for x0, y1, y2 in zip(x_index, y_dev, y_prod):
    # 对应坐标显示文字
    # x, y: 横纵坐标  s: 显示文字
    plt.text(x=x0 - width, y=y1, s=y1, ha='center', va='bottom', fontsize=11)
    plt.text(x=x0 + width, y=y2, s=y2, ha='center', va='bottom', fontsize=11)

# 保存图像
# plt.savefig('img/e_bar.png')

# 展示图片
plt.show()

条形图效果:

 

标签:plot,plt,bar,index,Matplotlib,width,matplotlib,条形图
From: https://www.cnblogs.com/zq-zq/p/16858339.html

相关文章

  • Matplotlib.plot 绘制折线图
    Python代码:#导入第三方包importmatplotlibimportmatplotlib.pyplotasplt#matplotlib其实是不支持显示中文的显示中文需要额外设置#设置字体类型,宋体:SimS......
  • Matlab中plot函数参数解析
    功能二维曲线绘图 语法1234567plot(Y)plot(X1,Y1,...)plot(X1,Y1,LineSpec,...)plot(...,'PropertyName',PropertyValue,...)plot(axes_hand......
  • python之matplotlib
    matplotlib可以将数据绘制成图像呈现,风格与matlab画图相似,是一款很好用的python库。这篇文档记录matplotlib的学习过程。主要参考见参考1。1概述Matplotlib代码库十分......
  • matlibplot从入门到精通——基本使用
    导入库importmatplotlib.pyplotaspltimportnumpyasnp#%matplotlibinline生成数据#definesomedatax=np.linspace(0,10,100)#100pointsstartingfrom0mu,si......
  • matplotlib
    importmatplotlibasplt#matlab语法plt.figure(figsize=(9,3))#表示图表的长度为9,高度为3#在2行1列位置,画第一个图plt.subplot(211)plt.bar(seasons.stock1)#2......
  • Python matplotlib 学习——建立画布和坐标系
    #导入包importmatplotlib.pyplotasplt#让图表在jupyter展示出来%matplotlibinline#解决中文乱码问题plt.rcParams["font.sans-serif"]='SimHei'#解决负号无法显示plt.......
  • 第3章 Jupyter Notebook, numpy和matplotlib
     3-1jupyternotebook基础Notbook示例  Notbook源码1[1]2forxinrange(5):3print('helloworld')4helloworld5helloworld6hello......
  • Barplot和boxplot作图详解——R语言
    当数据以简单的可视化的形式呈现时,数据便更具有意义并且更容易理解,因为人眼很难从原始数据中得出重要的信息。因此,数据可视化成为了解读数据最重要的方式之一。条形图和箱......
  • Plot函数用法详解——R语言
    plot是R中的基本画图工具,直接plot(x),x为一个数据集,就能画出图,soeasy!但是细节往往制胜的关键,所以就详细来看看plot的所有可设置参数及参数设置方法。R语言的基础绘图系统主......
  • Visualization optimization scheme using plotly
    1. WorkSummary  Forthisproject,wefirstbrowsedthepaperwebsitetoselectanareawhichwewereinterestedin,andthenwechoseaessayinthisare......