简体   繁体   English

Loglog绘图中的Matplotlib箭头

[英]Matplotlib arrow in loglog plot

I'm trying to draw an arrow into a loglog plot with matplotlib, which looks like this: 我正在尝试使用matplotlib绘制箭头到loglog图中,如下所示:

在此输入图像描述

I know that it has been suggested to turn off the axis ( Matplotlib: Draw a vertical arrow in a log-log plot ), but I do need the axes. 我知道有人建议关闭轴( Matplotlib:在对数 - 对数图中绘制一个垂直箭头 ),但我确实需要轴。 In addition, the suggestion did not seem to change anything (apart from turning the axes off, as expected): 此外,该建议似乎没有任何改变(除了关闭轴,正如预期的那样):

plt.figure();plt.loglog([1,10,60],[1,0.1,0.005])
plt.axis('off')
plt.arrow(2,0.002,5,0.098,'k',head_length=0.3)

My work around so far has been to create an invisible inset (meaning: axes off) with a linear axes environment and plot the arrow in the inset, which works but is really a bit unpleasant. 到目前为止,我的工作是用线性轴环境创建一个不可见的插图(意思是:轴关闭)并在插图中绘制箭头,这有效但实际上有点不愉快。 Is there a simpler way? 有更简单的方法吗? Or do people recommend to add these type of additional features with eg. 或者人们建议添加这些类型的附加功能,例如。 inkscape, after the main plot is done? inkscape,主要情节完成后?

You can use plt.annotate rather than plt.arrow . 您可以使用plt.annotate而不是plt.arrow This is noted in the documentation for plt.arrow : 这在plt.arrow文档中有plt.arrow

The resulting arrow is affected by the axes aspect ratio and limits. 生成的箭头受轴纵横比和限制的影响。 This may produce an arrow whose head is not square with its stem. 这可能会产生一个箭头,其头部与其茎不成直角。 To create an arrow whose head is square with its stem, use annotate() 要创建一个头部与其主干呈正方形的箭头,请使用annotate()

For example: 例如:

import matplotlib.pyplot as plt

plt.figure()
plt.loglog([1,10,60],[1,0.1,0.005])
plt.annotate('', xy=(5, 0.098), xytext=(2, 0.002), 
            arrowprops=dict(facecolor='black', shrink=0.),
            )

plt.ylim(0.001, 10)

plt.show()

在此输入图像描述

Note that you may need to adjust the axes limits to fit the arrow into the plot. 请注意,您可能需要调整轴限制以使箭头适合绘图。 Here I had to change ylim . 在这里,我不得不改变ylim

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM