考虑以下定义 PlotFigure() 的 python 模块“plot_figure.py”。请注意,这是一个伪代码。
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.)
关于python - 在 Pyplot 中,一旦绘图已经绘制完成,我们如何更改绘图的线宽?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42756865/