在图形的最高层上绘制圆 [英] Draw Circles on Top Level of Figure

查看:21
本文介绍了在图形的最高层上绘制圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个图,试图在组合色图和轮廓图的顶部绘制一个圆.圆一直在轮廓下方而不是轮廓上方绘制(请参见下图).我尝试重新排序 imshow、contour 和 Circle 的调用方式,以查看是否可以将圆圈显示在顶部,但我没有任何运气.有没有办法强制 Circle 位于图形的最顶层?感谢您的帮助!

解决方案

使用

I'm working on a figure where I'm trying to draw a circle on top of a combination colormap and contour plot. The circle keeps getting drawn under the contours instead of on top of them (see the figure below). I've tried reordering how I call imshow, contour, and Circle to see if I can get the circle to display on top, but I haven't had any luck. Is there a way to force Circle to be on the top most level of the figure? Thanks for your help!

解决方案

Use the zorder kwarg. That controls which elements go on top of each other. So, in this case, you want to increase the zorder of the circle. You may need to experiment to find a zorder that gives you the result you require, but the rule is that higher zorder objects appear on top of lower zorder objects.

Its hard to know exactly without any of your code, but assuming you've used pcolormesh, contour and a Circle patch, this example shows the effect of not setting a zorder (white circle), and setting zorder=10 (red circle).

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle

# Fake data
x = np.arange(100)
y = np.arange(100)
X, Y = np.meshgrid(x, y)
z = X**0.5 * Y**0.5

fig, ax = plt.subplots(1)
ax.set_aspect('equal')

ax.pcolormesh(X, Y, z, cmap='viridis')
ax.contour(X, Y, z, colors='k', linewidths=3)

circ1 = Circle((65, 65), 30, facecolor='None', edgecolor='w', lw=5)
circ2 = Circle((35, 35), 30, facecolor='None', edgecolor='r', lw=5, zorder=10)

ax.add_patch(circ1)
ax.add_patch(circ2)

plt.show()

Note that the white circle lies beneath the black contour lines, but by increasing the zorder to 10, the red circle lies on top of the contour lines.

这篇关于在图形的最高层上绘制圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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