Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Matplotlib中绘制椭圆以注释绘图_Python_Matplotlib_Annotate - Fatal编程技术网

Python 在Matplotlib中绘制椭圆以注释绘图

Python 在Matplotlib中绘制椭圆以注释绘图,python,matplotlib,annotate,Python,Matplotlib,Annotate,我最近发现matplotlib是Matlab绘图的更好的替代品。不幸的是,我对python的了解几乎为零 考虑下图和相关代码 import numpy as np import scipy.io from matplotlib import pyplot as plt plt.figure(figsize=[3.3, 3.3]) plt.rcParams.update({'font.size': 8, 'text.usetex': True}) plt.plot([1, 2, 3, 4],

我最近发现matplotlib是Matlab绘图的更好的替代品。不幸的是,我对python的了解几乎为零

考虑下图和相关代码

import numpy as np
import scipy.io
from matplotlib import pyplot as plt

plt.figure(figsize=[3.3, 3.3])
plt.rcParams.update({'font.size': 8, 'text.usetex': True})

plt.plot([1, 2, 3, 4], [1, 4, 9, 16], color='r')
plt.plot([1, 2, 3, 4], [2, 5, 10, 17], color='r')
plt.plot([1, 2, 3, 4], [11, 14, 19, 26], color='b')
plt.plot([1, 2, 3, 4], [12, 15, 20, 27], color='b')

plt.annotate('blue curves', xy=(1.5, 22.5), xytext=(1.5, 22.5), ha='center', va='center')
plt.annotate('', xy=(2, 15), xytext=(1.5, 22), arrowprops=dict(width=0.1, headwidth=2, headlength=2, color='grey'))
plt.annotate('red curves', xy=(2.5, 22.5), xytext=(2.5, 22.5), ha='center', va='center')
plt.annotate('', xy=(3, 10), xytext=(2.5, 22), arrowprops=dict(width=0.1, headwidth=2, headlength=2, color='grey'))

plt.grid()
plt.xlabel('x')
plt.ylabel('y')

plt.savefig('filename.pdf', format='pdf')

plt.show()

我想在每个包围曲线的箭头上添加一个小椭圆/圆。椭圆/圆应具有与箭头相同的样式(即颜色、厚度等)。有没有一种简单的方法可以在不修改现有代码的情况下做到这一点


我试图从中获得一些灵感,但没有成功。

如果圆很好,一个简单的解决方案是在箭头位置添加一个分散点(我没有安装乳胶):


当您可以在
plt
对象上调用
plot
annotate
时,您需要访问
图和
轴。对图、图和轴之间的差异给出了很好的解释

必须添加的代码(在
plt.show()之前)


智能方法;)真的很聪明。然而,尽管在这个例子中圆形是很好的,但我更喜欢选择绘制椭圆。但是谢谢你的快速回答:)谢谢!很容易扩展到椭圆。此外,我还忽略了情节、人物和轴线之间的差异。非常有用!
import numpy as np
import scipy.io
from matplotlib import pyplot as plt

plt.figure(figsize=[3.3, 3.3], dpi=100)
plt.rcParams.update({'font.size': 8, 'text.usetex': False})

plt.plot([1, 2, 3, 4], [1, 4, 9, 16], color='r')
plt.plot([1, 2, 3, 4], [2, 5, 10, 17], color='r')
plt.plot([1, 2, 3, 4], [11, 14, 19, 26], color='b')
plt.plot([1, 2, 3, 4], [12, 15, 20, 27], color='b')

plt.annotate('blue curves', xy=(1.5, 22.5), xytext=(1.5, 22.5), ha='center', va='center')
plt.annotate('', xy=(2, 15), xytext=(1.5, 22), arrowprops=dict(width=0.1, headwidth=2, headlength=2, color='grey'))
plt.annotate('red curves', xy=(2.5, 22.5), xytext=(2.5, 22.5), ha='center', va='center')
plt.annotate('', xy=(3, 10), xytext=(2.5, 22), arrowprops=dict(width=0.1, headwidth=2, headlength=2, color='grey'))

plt.plot(2, 15, 'o', ms=10 * 2, mec='grey', mfc='none', mew=1.5)
plt.plot(3, 10, 'o', ms=10 * 2, mec='grey', mfc='none', mew=1.5)

plt.grid()
plt.xlabel('x')
plt.ylabel('y')

plt.savefig('filename.pdf', format='pdf')

plt.show()
from matplotlib.patches import Ellipse

fig = plt.gcf()  # access the current figure
ax = plt.gca()   # access the current axis
xmin, xmax = ax.get_xlim()  # get the minimum and maximum extent of the x-axis
ymin, ymax = ax.get_ylim()  # get the minimum and maximum extent of the y-axis
aspect_ratio = fig.get_figheight() / fig.get_figwidth()  # get aspect ratio, we will use it to scale the width and height of the ellipse to appear as a circle
ax.add_patch(Ellipse(xy=(2, 15), width=(xmax-xmin)*aspect_ratio/5, height=(ymax-ymin)/5, color="grey", fill=False, lw=2))
ax.add_patch(Ellipse(xy=(3, 10), width=(xmax-xmin)*aspect_ratio/5, height=(ymax-ymin)/5, color="grey", fill=False, lw=2))