如何更改 matplotlib 图形的边框宽度

发布于 2024-08-09 09:51:20 字数 366 浏览 3 评论 0原文

如何更改子图的边框宽度?

代码如下:

fig = plt.figure(figsize = (4.1, 2.2))
ax = fig.add_subplot(111)

ax.patch.set_linewidth(0.1) 
ax.get_frame().set_linewidth(0.1) 

最后两行不起作用,但以下行可以正常工作:

legend.get_frame().set_ linewidth(0.1)

How do I change the border width of a subplot?

Code is as follows:

fig = plt.figure(figsize = (4.1, 2.2))
ax = fig.add_subplot(111)

ax.patch.set_linewidth(0.1) 
ax.get_frame().set_linewidth(0.1) 

The last two lines do not work, but the following works fine:

legend.get_frame().set_ linewidth(0.1)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

池予 2024-08-16 09:51:21

您想调整边框线大小吗?您需要使用 ax.spines[side].set_linewidth(size)。

所以像这样:

[i.set_linewidth(0.1) for i in ax.spines.itervalues()]

You want to adjust the border line size? You need to use ax.spines[side].set_linewidth(size).

So something like:

[i.set_linewidth(0.1) for i in ax.spines.itervalues()]
韶华倾负 2024-08-16 09:51:21

这对我有用[x.set_linewidth(1.5) for x in ax.spines.values()]

This worked for me [x.set_linewidth(1.5) for x in ax.spines.values()]

尐籹人 2024-08-16 09:51:21

如果需要将 Artist 的一个或多个属性设置为特定值,matplotlib 有一个便捷方法 plt.setp (可以用来代替列表理解)。

plt.setp(ax.spines.values(), lw=0.2)
# or
plt.setp(ax.spines.values(), linewidth=0.2)

另一种方法是简单地使用循环。每个书脊都定义了一个 set() 方法,可用于设置一系列属性,例如线宽、alpha 等。

for side in ['top', 'bottom', 'left', 'right']:
    ax.spines[side].set(lw=0.2)

一个工作示例:

import matplotlib.pyplot as plt

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

fig, ax = plt.subplots(figsize=(4, 2))
ax.plot(y)
ax.set(xticks=x, yticks=x, ylim=(0,2), xlim=(0,2));

plt.setp(ax.spines.values(), lw=5, color='red', alpha=0.2);

result

If one or more properties of an Artist need to be set to a specific value, matplotlib has a convenience method plt.setp (can be used instead of a list comprehension).

plt.setp(ax.spines.values(), lw=0.2)
# or
plt.setp(ax.spines.values(), linewidth=0.2)

Another way is to simply use a loop. Each spine defines a set() method that can be used to set a whole host of properties such as linewidth, alpha etc.

for side in ['top', 'bottom', 'left', 'right']:
    ax.spines[side].set(lw=0.2)

A working example:

import matplotlib.pyplot as plt

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

fig, ax = plt.subplots(figsize=(4, 2))
ax.plot(y)
ax.set(xticks=x, yticks=x, ylim=(0,2), xlim=(0,2));

plt.setp(ax.spines.values(), lw=5, color='red', alpha=0.2);

result

桜花祭 2024-08-16 09:51:20

也许这就是您正在寻找的?它在全局范围内设置值。

import matplotlib as mpl

mpl.rcParams['axes.linewidth'] = 0.1

Maybe this is what you are looking for? It sets the value globally.

import matplotlib as mpl

mpl.rcParams['axes.linewidth'] = 0.1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文