在 Pyplot 中,一旦绘图已经绘制完成,我们如何更改绘图的线宽?

In Pyplot, How can we change the line width of a plot once it is already plotted?

考虑以下 python 模块“plot_figure.py”定义 PlotFigure()。注意是伪代码。

import matplotlib.pyplot as plt

def PlotFigure(x)
  # Some other routines..
  plt.plot(x)
  # Some other routines..

我想调用plot_figure.PlotFigure,但是在绘制图形后,我想更改图形的线宽。尽管 PlotFigure() 可能包含其他例程,图中的线条是使用 plt.plot() 绘制的,如上面的伪代码所示。

下面是调用plot_figure.PlotFigure()

的代码
#!/usr/bin/python
import matplotlib.pyplot as plt
import plot_figure
x_data = [ # some data ]
plot_figure.PlotFigure(x_data)

#***I would like to change the line width of the figure here***

plt.show()

我知道我可以使用 fig = plt.gcf() 获取图形句柄,但 plt.setp(fig, linewidth=2) 不起作用。

有人可以对此提出一些建议吗?

首先请注意,设置线宽(或任何其他绘图参数)的通用方法是将其作为绘图命令的参数。

import matplotlib.pyplot as plt

def PlotFigure(x, **kwargs):
    # Some other routines..
    plt.plot(x, linewidth=kwargs.get("linewidth", 1.5) )
    # Some other routines..

和通话

plot_figure.PlotFigure(x_data, linewidth=3.)

如果这真的不是一个选项,您需要从图中获取线条。
最简单的情况是只有一个轴。

for line in plt.gca().lines:
    line.set_linewidth(3.)