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

mlv*_*ljr 27 python graphing plot matplotlib

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

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

axes(axes_rect, **axes_style)
Run Code Online (Sandbox Code Playgroud)

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

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

... # some code

rcdefaults() # restore [global] defaults
Run Code Online (Sandbox Code Playgroud)

有一个简单/干净的方式(可能是一个人可以设置x- 和y- 轴参数单独,等)?

PS如果不是,为什么?

Mar*_*lex 63

上述答案不起作用,因为评论中对此进行了解释.我建议使用刺.

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)

# you can change each line separately, like:
#ax.spines['right'].set_linewidth(0.5)
# to change all, just write:

for axis in ['top','bottom','left','right']:
  ax.spines[axis].set_linewidth(0.5)

plt.show()
# see more about spines at:
#http://matplotlib.org/api/spines_api.html
#http://matplotlib.org/examples/pylab_examples/multiple_yaxis_with_spines.html
Run Code Online (Sandbox Code Playgroud)

  • 请注意,如果更改轴棘的线宽,则可能还需要使用`ax.tick_params(width = 0.5)`来更改刻度线的宽度。 (3认同)

kak*_*ty3 11

plt.setp(ax.spines.values(), linewidth=5)
Run Code Online (Sandbox Code Playgroud)


dou*_*oug 6

是的,这是一种简单而干净的方法.

从轴实例调用' 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()
Run Code Online (Sandbox Code Playgroud)

axhline/axvline函数接受额外的参数,这些参数应该允许你在美学上做任何你想做的任何事情,特别是~matplotlib.lines.Line2D属性中的任何一个都是有效的kwargs(例如,'alpha','linestyle',capstyle, joinstyle).

  • 至少在最新版本的matplotlib中,此代码在`y = 0`处创建一条水平线,在`x = 0`处创建一条垂直线.这与更改轴的颜色/厚度不同. (19认同)