【发布时间】:2021-06-24 07:53:27
【问题描述】:
使用 matplotlib,我想在符号图上画一个椭圆。
考虑下图和相关代码。
import numpy as np
import scipy.io
from matplotlib import pyplot as plt
from matplotlib.patches import Ellipse
plt.figure(figsize=[3.3, 3.3])
plt.rcParams.update({'font.size': 8, 'text.usetex': True})
plt.semilogy([1, 2, 3, 4], [4, 8, 12, 16], color='r')
plt.semilogy([1, 2, 3, 4], [2, 4, 6, 8], color='r')
plt.semilogy([1, 2, 3, 4], [12, 15, 20, 27], color='b')
ax = plt.gca()
plt.annotate('blue curve', 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, 15), xytext=(2.5, 22), arrowprops=dict(width=0.1, headwidth=2, headlength=2, color='grey'))
ax.add_patch(Ellipse(xy=(3, 10), width=0.2, height=10, color="grey", fill=False, lw=1, zorder=5))
plt.grid()
plt.xlabel('x')
plt.ylabel('y')
plt.savefig('filename.pdf', format='pdf')
plt.show()
如您所见,椭圆由于 y 轴上的对数刻度而变形。我试图在自然尺度上重叠一个新轴,我也试图从现有问题中获得灵感(例如this one):这两种方法都不起作用,因为不幸的是,我对 python 的了解接近于零。
有没有一种简单的方法来绘制一个(非变形的)椭圆而无需大量修改现有代码?
【问题讨论】:
标签: python matplotlib annotate