【问题标题】:Matplotlib: draw ellipse to annotate on top of logarithmic scaleMatplotlib:绘制椭圆以在对数刻度上进行注释
【发布时间】: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


    【解决方案1】:

    answer in the Q&A you linked to 确实为您的问题提供了解决方案。您想使用那里记录的复合转换。我已经修改了你脚本的相关部分来做到这一点。

    注意:使用此方法所需的一项更改是椭圆的高度以坐标轴坐标(我使用的高度为 0.5)而不是数据坐标(height=10)给出。可能有一种方法可以在数据坐标中与另一个转换一起给出它,但我没有在这里包含它。我还稍微移动了椭圆中心,使其以两条红线为中心。

    from matplotlib.transforms import ScaledTranslation
    
    # Ellipse centre coordinates
    x, y = 3, 8
    
    # use the axis scale tform to figure out how far to translate 
    ell_offset = ScaledTranslation(x, y, ax.transScale)
    
    # construct the composite tform
    ell_tform = ell_offset + ax.transLimits + ax.transAxes
    
    # Create the ellipse centred on the origin, apply the composite tform
    ax.add_patch(Ellipse(xy=(0, 0), width=0.2, height=0.5, color="grey", fill=False, lw=1, zorder=5, transform=ell_tform))
    

    【讨论】:

    • 非常感谢!我觉得链接的答案可能会有所帮助,但我没有设法将其解决方案包括在内。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-01
    • 1970-01-01
    • 2017-09-08
    • 2012-03-03
    相关资源
    最近更新 更多