matplotlib将矩形添加到图而不是轴 [英] matplotlib add rectangle to Figure not to Axes

查看:47
本文介绍了matplotlib将矩形添加到图而不是轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在matplotlib图形上添加一个半透明的皮肤.我当时正在考虑向图中添加一个矩形,其alpha< 1和一个足够高的zorder,以便将其绘制在所有内容之上.

我正在考虑类似的事情

  figure.add_patch(矩形((0,0),1,1,alpha = 0.5,zorder = 1000)) 

但我猜矩形只能由 Axes 处理.有转身吗?

解决方案

您可以在图形顶部使用 phantom 轴并更改补丁以使其看起来像您喜欢的样子,请尝试以下示例:

将 matplotlib.pyplot 导入为 pltfig = plt.figure()斧= fig.add_axes([0,0,1,1])ax.xaxis.set_visible(False)ax.yaxis.set_visible(False)ax.set_zorder(1000)ax.patch.set_alpha(0.5)ax.patch.set_color('r')ax2 = fig.add_subplot(111)ax2.plot(范围(10),范围(10))plt.show() 

I need to add a semi transparent skin over my matplotlib figure. I was thinking about adding a rectangle to the figure with alpha <1 and a zorder high enough so its drawn on top of everything.

I was thinking about something like that

figure.add_patch(Rectangle((0,0),1,1, alpha=0.5, zorder=1000))

But I guess rectangles are handled by Axes only. is there any turn around ?

解决方案

You can use a phantom axes on top of your figure and change the patch to look as you like, try this example:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
ax.set_zorder(1000)
ax.patch.set_alpha(0.5)
ax.patch.set_color('r')

ax2 = fig.add_subplot(111)
ax2.plot(range(10), range(10))

plt.show()

这篇关于matplotlib将矩形添加到图而不是轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆