设置axes.linewidth而不更改rcParams全局字典

发布于 2024-08-26 21:39:39 字数 420 浏览 7 评论 0原文

,似乎无法执行以下操作(它会引发错误,因为 axes 没有 set_linewidth 方法):

axes_style = {'linewidth':5}
axes_rect = [0.1, 0.1, 0.9, 0.9]

axes(axes_rect, **axes_style)

并且必须使用以下老技巧:

rcParams['axes.linewidth'] = 5 # set the value globally

... # some code

rcdefaults() # restore [global] defaults

因此 有一种简单/干净的方法(可能可以单独设置 x 和 y 轴参数等)?

如果没有,为什么?

So, it seems one cannot do the following (it raises an error, since axes does not have a set_linewidth method):

axes_style = {'linewidth':5}
axes_rect = [0.1, 0.1, 0.9, 0.9]

axes(axes_rect, **axes_style)

and has to use the following old trick instead:

rcParams['axes.linewidth'] = 5 # set the value globally

... # some code

rcdefaults() # restore [global] defaults

Is there an easy / clean way (may be one can set x- and y- axes parameters individually, etc)?

If no, why?

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

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

发布评论

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

评论(4

水晶透心 2024-09-02 21:39:39
  • 这个答案不起作用,正如评论中所解释的那样。我建议使用spines
  • 正如 Czechnology 的评论中提到的,也考虑更改刻度。
import matplotlib.pyplot as plt

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))

ax1.set_title('Normal spine and ticks')
ax2.set_title('Adjusted spine and ticks')

# change each spine separately:
# ax.spines['right'].set_linewidth(0.5)

# change all spines
for axis in ['top','bottom','left','right']:
    ax2.spines[axis].set_linewidth(4)

# increase tick width
ax2.tick_params(width=4)

plt.show()

输入图像描述 在这里

  • This answer does not work, as it is explained in the comments. I suggest using spines.
  • As mentioned in a comment by Czechnology, consider changing the ticks too.
import matplotlib.pyplot as plt

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))

ax1.set_title('Normal spine and ticks')
ax2.set_title('Adjusted spine and ticks')

# change each spine separately:
# ax.spines['right'].set_linewidth(0.5)

# change all spines
for axis in ['top','bottom','left','right']:
    ax2.spines[axis].set_linewidth(4)

# increase tick width
ax2.tick_params(width=4)

plt.show()

enter image description here

清音悠歌 2024-09-02 21:39:39
plt.setp(ax.spines.values(), linewidth=5)
plt.setp(ax.spines.values(), linewidth=5)
影子的影子 2024-09-02 21:39:39

是的,有一种简单而干净的方法可以做到这一点。

从轴实例调用“axhline”和“axvline”似乎是 MPL 文档中认可的技术。

无论如何,它都很简单,并且可以让您对轴的外观进行细粒度的控制。

例如,此代码将创建一个绘图并将 x 轴着色为绿色,并将 x 轴的线宽从默认值“1”增加到值“4”; y轴被涂成红色并且y轴的线宽从“1”增加到“8”。

from matplotlib import pyplot as PLT
fig = PLT.figure()
ax1 = fig.add_subplot(111)

ax1.axhline(linewidth=4, color="g")        # inc. width of x-axis and color it green
ax1.axvline(linewidth=4, color="r")        # inc. width of y-axis and color it red

PLT.show()

axhline/axvline 函数接受额外的参数,这些参数应该允许您做任何您想要的美观的事情,特别是任何 ~matplotlib.lines.Line2D 属性都是有效的 kwargs(例如,'alpha','linestyle',capstyle,联合风格)。

Yes, there's an easy and clean way to do this.

Calling 'axhline' and 'axvline' from an axis instance appears to be the technique endorsed in the MPL Documentation.

In any event, it is simple and gives you fine-grained control over the appearance of the axes.

So for instance, this code will create a plot and color the x-axis green and increase the line width of the x-axis from a default value of "1" to a value of "4"; the y-axis is colored red and the line width of the y-axis is increased from "1" to "8".

from matplotlib import pyplot as PLT
fig = PLT.figure()
ax1 = fig.add_subplot(111)

ax1.axhline(linewidth=4, color="g")        # inc. width of x-axis and color it green
ax1.axvline(linewidth=4, color="r")        # inc. width of y-axis and color it red

PLT.show()

The axhline/axvline function accepts additional arguments which ought to allow you do any pretty much whatever you want aesthetically, in particular any of the ~matplotlib.lines.Line2D properties are valid kwargs (e.g., 'alpha', 'linestyle', capstyle, joinstyle).

握住你手 2024-09-02 21:39:39

如果您使用 pyplot 递归创建(非矩形)轴,则可以更改每个轴的线宽参数。

例如:

import matplotlib.pyplot as plt

plt.figure(figsize = figsize)
fig, ax = plt.subplots(figsize = figsize)
for shape in sf.shapeRecords():
    x = [i[0] for i in shape.shape.points[:]]
    y = [i[1] for i in shape.shape.points[:]]
    ax.plot(x, y, 'k', linewidth=5)

有关文档,请参阅 MPL.axes 文档 (向下滚动直到“其他参数”-> **kwargs)

注意“如果使用一个绘图命令绘制多条线,则 kwargs 将应用于所有这些线。”

也许这个解决方案与其他地方提出的不同问题有关,但我发现这个页面正在寻找我自己问题的解决方案,所以也许它可以帮助其他人寻找相同的东西。

If you're recursively creating (non-rectangular) axes using pyplot, you can change the linewidth parameter for each ax.

For instance:

import matplotlib.pyplot as plt

plt.figure(figsize = figsize)
fig, ax = plt.subplots(figsize = figsize)
for shape in sf.shapeRecords():
    x = [i[0] for i in shape.shape.points[:]]
    y = [i[1] for i in shape.shape.points[:]]
    ax.plot(x, y, 'k', linewidth=5)

For documentation, see MPL.axes documentation (Scroll down until "Other Parameters" -> **kwargs)

N.B. "If you make multiple lines with one plot command, the kwargs apply to all those lines."

Maybe this solution is related to a different question asked elsewhere, but I found this page looking for a solution to my own problem, so maybe it can help others looking for the same thing.

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