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

cha*_*com 1 python matplotlib width

考虑以下定义PlotFigure () 的Python 模块“ plot_figure.py ” 。请注意,这是一个伪代码。

import matplotlib.pyplot as plt

def PlotFigure(x)
  # Some other routines..
  plt.plot(x)
  # Some other routines..
Run Code Online (Sandbox Code Playgroud)

我想调用 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()
Run Code Online (Sandbox Code Playgroud)

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

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

Imp*_*est 6

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

import matplotlib.pyplot as plt

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

和电话

plot_figure.PlotFigure(x_data, linewidth=3.)
Run Code Online (Sandbox Code Playgroud)

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

for line in plt.gca().lines:
    line.set_linewidth(3.)
Run Code Online (Sandbox Code Playgroud)