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

问题描述 投票:0回答:4

如何更改

subplot
的边框宽度?

代码如下:

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)
python matplotlib width border axis
4个回答
39
投票

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

import matplotlib as mpl

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

38
投票

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

所以类似:

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

7
投票

这对我有用

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


0
投票

如果需要将 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);

© www.soinside.com 2019 - 2024. All rights reserved.