条形颜色演示
import matplotlib.pyplot as plt

'''
将plt.subplots()函数的返回值赋值给fig和ax俩个变量
plt.subplots()是一个函数,返回一个包含figure和axes对象的元组
'''
fig, ax = plt.subplots() fruits = ['apple', 'blueberry', 'cherry', 'orange']
counts = [40, 100, 30, 55]
bar_labels = ['red', 'blue', 'pink', 'orange']
bar_colors = ['tab:red', 'tab:blue', 'tab:pink', 'tab:orange']
'''
ax.bar(x,height,width,bottom,align)
x:一个标量序列,代表柱状图的x坐标,默认x取值是每个柱状图所在的中点位置,或者也可以是柱状图左侧边缘位置
hegiht:一个标量或者是标量序列,代表柱状图的高度
width:可选参数,标量或类数组,柱状图的默认宽度值为0.8
bottom可选参数,柱状图的y坐标默认为None
algin有俩个可选项{center,edge},默认为center,该参数决定x值位于柱状图的位置 '''
# ax.bar(fruits, counts, label=bar_labels, color=bar_colors)
ax.bar(fruits, counts, label=bar_labels, color=bar_colors)
#添加y轴标题
ax.set_ylabel('fruit supply')
#图表标题
ax.set_title('Fruit supply by kind and color')
ax.legend(title='Fruit color') # plt.savefig('./q1.jpg')`

条形标签演示
import matplotlib.pyplot as plt
import numpy as np species = ('Adelie', 'Chinstrap', 'Gentoo')
sex_counts = {
'Male': np.array([73, 34, 61]),
'Female': np.array([73, 34, 58]),
}
width = 0.6 # the width of the bars: can also be len(x) sequence fig, ax = plt.subplots()
'''
np.zeros函数的作用
返回来一个给定形状和类型的用0填充的数组
zeros(shape,dtype=float,order='C')
shape:形状
dtype:数据类型,可选参数
order:可选参数,c 行优先,f列优先
'''
bottom = np.zeros(3)
# print(bottom) for sex, sex_count in sex_counts.items():
p = ax.bar(species, sex_count, width, label=sex, bottom=bottom)
bottom += sex_count ax.bar_label(p, label_type='center') ax.set_title('Number of penguins by sex')
#ax.legend()
ax.legend() plt.show()

堆积条形图
import matplotlib.pyplot as plt
import numpy as np # data from https://allisonhorst.github.io/palmerpenguins/ species = (
"Adelie\n $\\mu=$3700.66g",
"Chinstrap\n $\\mu=$3733.09g",
"Gentoo\n $\\mu=5076.02g$",
)
weight_counts = {
"Below": np.array([70, 31, 58]),
"Above": np.array([82, 37, 66]),
}
width = 0.5 fig, ax = plt.subplots()
bottom = np.zeros(3) for boolean, weight_count in weight_counts.items():
p = ax.bar(species, weight_count, width, label=boolean, bottom=bottom)
bottom += weight_count ax.set_title("Number of penguins with above average body mass")
ax.legend(loc="upper right") plt.show()

带标签的分组条形图
# data from https://allisonhorst.github.io/palmerpenguins/

import matplotlib.pyplot as plt
import numpy as np species = ("Adelie", "Chinstrap", "Gentoo")
penguin_means = {
'Bill Depth': (18.35, 18.43, 14.98),
'Bill Length': (38.79, 48.83, 47.50),
'Flipper Length': (189.95, 195.82, 217.19),
} x = np.arange(len(species)) # the label locations
width = 0.25 # the width of the bars
multiplier = 0 fig, ax = plt.subplots(layout='constrained') for attribute, measurement in penguin_means.items():
offset = width * multiplier
rects = ax.bar(x + offset, measurement, width, label=attribute)
ax.bar_label(rects, padding=3)
multiplier += 1 # Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Length (mm)')
ax.set_title('Penguin attributes by species')
ax.set_xticks(x + width, species)
ax.legend(loc='upper left', ncols=3)
ax.set_ylim(0, 250) plt.show()

水平条形图
import matplotlib.pyplot as plt
import numpy as np # Fixing random state for reproducibility
np.random.seed(19680801) fig, ax = plt.subplots() # Example data
people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
y_pos = np.arange(len(people))
performance = 3 + 10 * np.random.rand(len(people))
error = np.random.rand(len(people)) ax.barh(y_pos, performance, xerr=error, align='center')
ax.set_yticks(y_pos, labels=people)
ax.invert_yaxis() # labels read top-to-bottom
ax.set_xlabel('Performance')
ax.set_title('How fast do you want to go today?') plt.show()

饼状图
import matplotlib.pyplot as plt

labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10] fig, ax = plt.subplots()
ax.pie(sizes, labels=labels)

饼图和圆环图
import matplotlib.pyplot as plt
import numpy as np fig, ax = plt.subplots(figsize=(6, 3), subplot_kw=dict(aspect="equal")) recipe = ["375 g flour",
"75 g sugar",
"250 g butter",
"300 g berries"] data = [float(x.split()[0]) for x in recipe]
ingredients = [x.split()[-1] for x in recipe] def func(pct, allvals):
absolute = int(np.round(pct/100.*np.sum(allvals)))
return f"{pct:.1f}%\n({absolute:d} g)" wedges, texts, autotexts = ax.pie(data, autopct=lambda pct: func(pct, data),
textprops=dict(color="w")) ax.legend(wedges, ingredients,
title="Ingredients",
loc="center left",
bbox_to_anchor=(1, 0, 0.5, 1)) plt.setp(autotexts, size=8, weight="bold") ax.set_title("Matplotlib bakery: A pie") plt.show()

matplotlib 图表生成的更多相关文章

  1. 一行代码让matplotlib图表变高大上

    1 简介 matplotlib作为Python生态中最流行的数据可视化框架,虽然功能非常强大,但默认样式比较简陋,想要制作具有简洁商务风格的图表往往需要编写众多的代码来调整各种参数. 而今天要为大家介 ...

  2. 2018.03.29 python-matplotlib 图表生成

    '''Matplotlib -> 一个python版的matlab绘图接口,以2D为主,支持python,numpy,pandas基本数据结构,高效图标库''' import numpy as ...

  3. matplotlib图表介绍

    Matplotlib 是一个python 的绘图库,主要用于生成2D图表. 常用到的是matplotlib中的pyplot,导入方式import matplotlib.pyplot as plt 一. ...

  4. [Python] Matplotlib 图表的绘制和美化技巧

    目录 在一张画布中绘制多个图表 加图表元素 气泡图 组合图 直方图 雷达图 树状图 箱形图 玫瑰图 在一张画布中绘制多个图表 Matplotlib模块在绘制图表时,默认先建立一张画布,然后在画布中显示 ...

  5. Python tkinter库将matplotlib图表显示在GUI窗口上,并实时更新刷新数据

    代码 1 ''' 2 使用matplotlib创建图表,并显示在tk窗口 3 ''' 4 import matplotlib.pyplot as plt 5 from matplotlib.pylab ...

  6. JFreeChart 图表生成实例(饼图、柱状图、折线图、时序图)

    import java.awt.BasicStroke; import java.awt.Color; import java.io.FileOutputStream; import java.io. ...

  7. matplotlib图表组成元素

    一.函数 1.plot()    --   展示变量的趋势与变化 用法: plt.plot(x,y,ls="-",lw=2,label="plot figure" ...

  8. Matplotlib 图表的样式参数

    1. import numpy as np import pandas as pd import matplotlib.pyplot as plt % matplotlib inline # 导入相关 ...

  9. Matplotlib 图表的基本参数设置

    1.图名,图例,轴标签,轴边界,轴刻度,轴刻度标签 # 图名,图例,轴标签,轴边界,轴刻度,轴刻度标签等 df = pd.DataFrame(np.random.rand(10,2),columns= ...

  10. python matplotlib 图表局部放大

    import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes ...

随机推荐

  1. Java表达式引擎选型调研分析

    1 简介 我们项目组主要负责面向企业客户的业务系统,企业的需求往往是多样化且复杂的,对接不同企业时会有不同的定制化的业务模型和流程.我们在业务系统中使用表达式引擎,集中配置管理业务规则,并实现实时决策 ...

  2. vue-router重写push方法,解决相同路径跳转报错,解决点击菜单栏打开外部链接

    修改vue-router的配置文件,默认位置router/index.js import Vue from 'vue' import Router from 'vue-router' /** * 重写 ...

  3. 浮点类型(double与float及其它们的输入输出)

    <1>浮点类型 (1)两种类型 double 字长64位(8个字节),有效数字15,范围大概为2.2* 10^-308 ~ 1.79*10^308,0,nan; float字长32位(4个 ...

  4. 瀑布图有什么作用?除了excel如何快速制作?

    瀑布图是一种特殊的数据可视化图表,具有以下作用: 1. 对比变化:瀑布图可以清晰地展示数据在不同因素作用下的变化情况.通过将数据分解成各个组成部分,并以阶梯状呈现,可以直观地对比每个因素对总体结果的影 ...

  5. 2021AI量化投资训练营重磅升级,自带编程的优势显而易见

    量化交易规模突破万亿大关 国内量化交易规模快速发展,今年量化基金已突破万亿大关,并且量化私募的整体业绩十分亮眼,过去5年一线量化私募的超额收益基本在20%~30%,量化交易的占比已达到20%-30%( ...

  6. 不会使用 EF Core 的 Code First 模式?来看看这篇文章,手把手地教你

    EF Core Code First 是什么 Code First 是 Entity Framework Core (简称 EF Core) 的一种开发模式,它允许开发人员使用纯粹的代码来定义数据模型 ...

  7. Scipy快速入门

    Scipy快速入门 注意事项 图床在国外,配合美区.日区网络使用更佳,如遇图片加载不出来,考虑换个VPN吧. 监修中敬告 本文处于Preview阶段,不对文章内容负任何责任,如有意见探讨欢迎留言. 联 ...

  8. Linux-LVM 磁盘扩容

    LVM技术详解:视频1.视频2.视频3 安装lvm2后才支持如下命令 yum install -y lvm2 序号 功能 PV物理卷命令 VG卷组命令 LV逻辑卷命令 01 扫描功能 pvscan v ...

  9. 2023河南省第五届“金盾信安杯”CRYPTO MISC 复现

    MISC 来都来了 题目 我的解答: 给了一个加密压缩包,010查看发现是伪加密,修改如下两部分: 头部和尾部的09分别改为00 然后解压得到: 尝试base64解码得到很零散的结果..大眼一看不知道 ...

  10. 君子不玩物丧志,亦常以借物调心,网站集成二次元网页小组件(widget)石蒜模拟器,聊以赏玩

    传世经典<菜根谭>中有言曰:"徜徉于山林泉石之间,而尘心渐息:夷犹于诗书图画之内,而俗气潜消.故君子虽不玩物丧志,亦常借物调心."意思是,徜徉在林泉山石之间,能够摒弃杂 ...