简体   繁体   English

删除matplotlib文本绘图边框

[英]Remove matplotlib text plot border

How to remove matplotlib text border, while making the text be in the first plane, in front of the plotted line? 如何删除matplotlib文本边框,同时使文本位于绘制线前面的第一个平面中?

import matplotlib.pyplot as plt

x = [1, 2, 3]
y = [1, 2, 3]

plt.plot(x, y)
plt.text(2.85, 2.9, 'label', bbox={'facecolor':'white', 'alpha':1, 'pad':10})

plt.show()

Are you asking how to make the text more visible without adding the box behind it? 您是否询问如何在不添加背后的框的情况下使文本更加醒目? If so, have a look at the last couple of examples. 如果是这样,请看看最后几个例子。


Controlling the drawing order 控制绘图顺序

The text is already in front of the line, it's just hard to distinguish the two. 文字已经在线前,很难区分这两者。 However, in general, the order of the elements is controlled by the zorder kwarg. 但是,通常,元素的顺序由zorder kwarg控制。

To demonstrate this, I'll change the colors and size of the font in your example to make things a touch more clear: 为了证明这一点,我将更改示例中字体的颜色和大小,以使事情更清晰:

import matplotlib.pyplot as plt

x = [1, 2, 3]
y =[1, 2, 3]

fig, ax = plt.subplots()
ax.plot(x, y, linewidth=10, color='yellow')
ax.text(2, 2, 'label', ha='center', size=72)

# For the moment, hide everything else...
ax.axis('off')
fig.tight_layout()

plt.show()

在此输入图像描述

If we decrease the z-order of the text below that of the line or increase the zorder of the line above that of the text, the line will be in front. 如果我们将文本的z顺序降低到该行的z顺序以下或者将该行的zorder增加到高于该行的行的zorder,则该行将在前面。 By default, most plotted data types have a zorder of 1, while annotations such as text have a zorder of 3, if I recall correctly. 默认情况下,大多数绘制的数据类型的zorder为1,而文本等注释的zorder为3,如果我没记错的话。 It's just the relative values of zorder that matter, though. 不过,这只是zorder的相对值。 In other words, it doesn't matter whether we do ax.text(..., zorder=0) or ax.plot(..., zorder=4) , we'll get the same result. 换句话说,无论我们是否执行ax.text(..., zorder=0)ax.plot(..., zorder=4) ,我们都会得到相同的结果。

import matplotlib.pyplot as plt

x = [1, 2, 3]
y =[1, 2, 3]

fig, ax = plt.subplots()
ax.plot(x, y, linewidth=10, color='yellow')
ax.text(2, 2, 'label', ha='center', size=72, zorder=0)

# For the moment, hide everything else...
ax.axis('off')
fig.tight_layout()

plt.show()

在此输入图像描述


A more subtle box for clearer labels 更清晰的盒子,更清晰的标签

However, what you're probably wanting to accomplish is a cleaner way to display the label and the line together. 但是,您可能希望实现的是一种更清晰的方式来显示标签和线条。

In that case, you have several different options. 在这种情况下,您有几种不同的选择。

Let's go back to your original example. 让我们回到你原来的例子。 You can display the box, behind the text, but remove the edge color on the box. 您可以在文本后面显示框,但删除框上的边缘颜色。 So, if you add 'edgecolor':'none' to the dict in the bbox kwarg, you'll get something similar to this: 所以,如果你在bbox kwarg的dict中添加'edgecolor':'none' ,你会得到类似的东西:

import matplotlib.pyplot as plt

x = [1, 2, 3]
y =[1, 2, 3]

plt.plot(x, y)
plt.text(2.85, 2.9, 'label',
         bbox={'facecolor':'white', 'edgecolor':'none', 'pad':10})

plt.show()

在此输入图像描述

Or as an example of what it would look like using the earlier code snippet with a yellow line: 或者作为使用带有黄线的早期代码片段的示例:

在此输入图像描述

Using a stroke effect for clear labels 使用笔触效果清除标签

However, this doesn't look as nice if we have more than just a simple line. 但是,如果我们不仅仅是一条简单的线条,这看起来就不那么好了。 Therefore, you might also want to consider using a stroke path effect: 因此,您可能还需要考虑使用笔划路径效果:

import matplotlib.pyplot as plt
import matplotlib.patheffects as pe

x = [1, 2, 3]
y =[1, 2, 3]

fig, ax = plt.subplots()
ax.plot(x, y, linewidth=10, color='yellow')
ax.text(2, 2, 'label', ha='center', size=72,
       path_effects=[pe.withStroke(linewidth=10, foreground='w')])

# For the moment, hide everything else...
ax.axis('off')
fig.tight_layout()
fig.set(facecolor='white')

plt.show()

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM