Python matplotlib更改现有略图上的线宽
我正在使用skimage中封装的plotting函数在两组关键点之间绘制匹配。特别是,我正在使用此功能: 这很好,但是画的线很粗。我知道使用matplotlib可以通过绘图中的Python matplotlib更改现有略图上的线宽,python,matplotlib,scikit-image,Python,Matplotlib,Scikit Image,我正在使用skimage中封装的plotting函数在两组关键点之间绘制匹配。特别是,我正在使用此功能: 这很好,但是画的线很粗。我知道使用matplotlib可以通过绘图中的linewidth参数控制线宽。在这种情况下,绘图函数由链接中的plot\u matches函数调用 顺便说一下,我使用它的方式如下: import matplotlib.pyplot as plt from skimage.feature import plot_matches fig, ax = plt.subplo
linewidth
参数控制线宽。在这种情况下,绘图函数由链接中的plot\u matches
函数调用
顺便说一下,我使用它的方式如下:
import matplotlib.pyplot as plt
from skimage.feature import plot_matches
fig, ax = plt.subplots()
plt.gray()
plot_matches(ax, img1, img2, k1[:, [0, 1]],
k2[:, [0, 1]], matches, only_matches=True)
ax.axis('off')
plt.show()
在这种情况下,是否有办法控制linewidth属性?不幸的是,
plot\u仅根据其内部设置匹配打印参数,并且没有进一步的关键字参数传递到matplotlib函数
您可以选择在调用该函数之前使用th更改标准线宽
plt.rcParams["lines.linewidth"] = 3
plot_matches(...)
您可能需要在打印后将其设置回默认值(1.5),以便能够在进一步生成的打印中使用标准。另一种可能的解决方案是获取line2D对象的列表,然后使用set_linewidth()
plot_matches
的问题在于,它可以根据输入在轴上创建多个可能的美工器,因此没有像许多打印函数那样返回新对象的值。您可以扩展@DavidG的答案,以检查通过调用plot\u matches
专门添加了哪些行。这将使您的代码稍微复杂一些,但可能值得付出努力,具体取决于您的需要:
import matplotlib.pyplot as plt
from skimage.feature import plot_matches
def my_plot_matches(ax, *args, line_options={}, **kwargs):
nlines = len(ax.lines)
plot_matches(ax, *args, **kwargs)
new_lines = ax.lines[nlines:]
if new_lines and line_options:
plt.setp(new_lines, **line_options)
fig, ax = plt.subplots()
plt.gray()
my_plot_matches(ax, img1, img2, k1[:, [0, 1]], k2[:, [0, 1]], matches, only_matches=True, line_options={'linewidth': 2})
ax.axis('off')
plt.show()
此版本依赖于这样一个事实,即轴上与plot_匹配的任何图形都会附加到线对象的内部列表中。我假设在plot\u matches
之前列表中没有的任何内容都在列表的末尾,需要修改。这样做可以满足我的需要!非常感谢!您还可以访问最后绘制的一组线并修改它们。@madpysicator您是指另一个答案中提出的解决方案?这将改变所有的路线。否则,是的,您可以在有问题的行上循环,例如i的、zip中的行(范围(匹配.shape[0])、ax.lines[:-1]):line.set_线宽(3)
。听起来更复杂。@ImportanceOfBeingErnest。当然我有更复杂的想法,但当时在手机上,所以无法及时写出来或看到其他答案。@ImportanceOfBeingErnest。我添加了一个答案。很好。我以前没有尝试过这个解决方案,但我也会尝试一下。如果你问我,太复杂了。;-)除非你说出一个我的答案行不通的例子。@ImportanceOfBeingErnest。完全同意。基本上是因为无聊而增加的。任何一天我都会同意你的答案。好吧,无聊的话+1!我也很高兴知道。谢谢你。
import matplotlib.pyplot as plt
from skimage.feature import plot_matches
def my_plot_matches(ax, *args, line_options={}, **kwargs):
nlines = len(ax.lines)
plot_matches(ax, *args, **kwargs)
new_lines = ax.lines[nlines:]
if new_lines and line_options:
plt.setp(new_lines, **line_options)
fig, ax = plt.subplots()
plt.gray()
my_plot_matches(ax, img1, img2, k1[:, [0, 1]], k2[:, [0, 1]], matches, only_matches=True, line_options={'linewidth': 2})
ax.axis('off')
plt.show()