Matplotlib在figure中画矩形

本文介绍了如何使用Python的matplotlib库在散点图上添加矩形注释。首先展示了如何创建一个无填充的紫色矩形,然后演示了填充红色矩形并调整透明度的方法。接着,通过text()函数添加了描述矩形的文本注释。最后,展示了在同一图表上绘制多个不同颜色矩形以高亮不同区域的技巧。
摘要由CSDN通过智能技术生成

当您进行数据可视化时,您可能希望通过添加一些注释来突出显示绘图的特定区域。 在这篇文章中,我们将学习如何在 Python 中使用 matplotlib 制作的绘图上添加矩形。 我们将首先添加一个具有特定颜色的简单矩形,然后学习如何用选择的颜色填充矩形。 接下来我们还将看到一个添加文本来描述添加矩形的示例。 最后我们将看到如何在 python 中的 matplotlib 绘图上添加多个矩形。

1. 在使用 matplotlib 制作的绘图上绘制矩形

matplotlib 中的 patches 模块允许我们在绘图顶部添加矩形等形状。 让我们将 patches 加载为 mpatches

import matplotlib.pyplot as plt
import pandas as pd
import matplotlib.patches as mpatches

penguins_data="https://raw.githubusercontent.com/datavizpyr/data/master/palmer_penguin_species.tsv"
# load penguns data with Pandas read_csv
df = pd.read_csv(penguins_data, sep="\t")
df.head()

plt.scatter(x=df.culmen_length_mm,
            y=df.culmen_depth_mm)
plt.xlabel("Culmen Length (mm)",fontweight ='bold', size=14)
plt.ylabel("Culmen Depth (mm)", fontweight ='bold',size=14)
left, bottom, width, height = (31, 15, 14, 7)
rect=mpatches.Rectangle((left,bottom),width,height, 
                        fill=False,
                        color="purple",
                       linewidth=2)
                       #facecolor="red")
plt.gca().add_patch(rect)
plt.show

在这里插入图片描述

2.在使用 matplotlib 制作的绘图上绘制填充颜色的矩形

之前,我们绘制了一个简单的矩形。现在,让我们以几种不同的方式对其进行自定义。 首先,我们将用颜色填充矩形以更好地突出绘图的部分。

我们可以通过不使用 fill=False 并使用 facecolor 指定颜色来用颜色填充矩形。 在这里,我们还指定填充的透明度级别。

plt.scatter(x=df.culmen_length_mm,
            y=df.culmen_depth_mm)
plt.xlabel("Culmen Length (mm)",fontweight ='bold', size=14)
plt.ylabel("Culmen Depth (mm)", fontweight ='bold',size=14)
left, bottom, width, height = (31, 15, 14, 7)
rect=mpatches.Rectangle((left,bottom),width,height, 
                        #fill=False,
                        alpha=0.1,
                       facecolor="red")
plt.gca().add_patch(rect)
plt.show()

在这里插入图片描述

3.在使用 matplotlib 制作的绘图上为矩形添加文本注释

我们可以使用 matplotlib 中的 text() 函数添加文本来描述矩形。 在这里,我们在绘制矩形后立即添加文本注释。

plt.scatter(x=df.culmen_length_mm,
            y=df.culmen_depth_mm)
plt.xlabel("Culmen Length (mm)",fontweight ='bold', size=14)
plt.ylabel("Culmen Depth (mm)", fontweight ='bold',size=14)
left, bottom, width, height = (31, 15, 14, 7)
rect=mpatches.Rectangle((left,bottom),width,height, 
                        #fill=False,
                        alpha=0.1,
                        #color="purple",
                       #linewidth=2,
                       facecolor="red")
plt.gca().add_patch(rect)
# add text with text() function in matplotlib
plt.text(31, 21.3,'rectangle',fontsize=16, color="red", weight="bold")
plt.show()

在这里插入图片描述

4.在使用 matplotlib 制作的绘图上绘制多个矩形

添加多个矩形以突出显示绘图的多个部分也很容易。 在这个例子中,我们两次使用相似的代码块来添加两个不同颜色的矩形。

# make scatter plot
plt.scatter(x=df.culmen_length_mm,
            y=df.culmen_depth_mm)
plt.xlabel("Culmen Length (mm)",fontweight ='bold', size=14)
plt.ylabel("Culmen Depth (mm)", fontweight ='bold',size=14)
# add first rectangle with patches
left, bottom, width, height = (31, 15, 14, 7)
rect=mpatches.Rectangle((left,bottom),width,height, 
                        #fill=False,
                        alpha=0.1,
                        #color="purple",
                       #linewidth=2,
                       facecolor="red")
plt.gca().add_patch(rect)
 
# add second rectangle with patches
left, bottom, width, height = (48, 17.5, 7, 4)
rect=mpatches.Rectangle((left,bottom),width,height, 
                        #fill=False,
                        alpha=0.1,
                        #color="purple",
                       #linewidth=2,
                       facecolor="green")
plt.gca().add_patch(rect)
plt.show()

在这里插入图片描述

参考资源

[1] How To Draw a Rectangle on a Plot in Matplotlib?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值