【问题标题】:Matplotlib marker annotation fontsize not shrinking below 1pt in PDFMatplotlib 标记注释字体大小在 PDF 中不会缩小到 1pt 以下
【发布时间】:2020-09-26 19:09:22
【问题描述】:

我的代码中有两个 ax.annotate 语句。第一行文字我想变大。第二行文字应该缩小。

for i,j,k in zip(x_power,y_power,net_name_power):
    ax.annotate(str(k),  xy=(i, j), color='white', fontsize=1, weight='light', horizontalalignment='center', verticalalignment='bottom')
    ax.annotate('{} , {}'.format(str(round(i,3)),str(round(j,3))),  xy=(i, j), color='blue', fontsize=0.1, weight='light', horizontalalignment='center', verticalalignment='top')

ax.annotate 的字体大小不低于 fontsize=1。

Illustration of the problem

在图像中,白色文本是 fontsize=1,而蓝色文本是 fontsize=0.1。但正如我们所看到的,两个文本的大小相同。 我正在使用 plt.savefig('output.pdf') 以 PDF 格式转储绘图 .

我希望蓝色文本按比例缩小,以便整齐地适合红色圆形标记。有人可以提出解决方案吗?

谢谢!

【问题讨论】:

    标签: python matplotlib annotations size marker


    【解决方案1】:

    我相信最小字体点大小是 1 -- 看这里的源代码:

    if size < 1.0:
        _log.info('Fontsize %1.2f < 1.0 pt not allowed by FreeType. '
                  'Setting fontsize = 1 pt', size)
        size = 1.0
    

    您可以尝试使用字符串描述符:

    例如:fontsize='xx-small'

    font_scalings = {
        'xx-small': 0.579,
        'x-small':  0.694,
        'small':    0.833,
        'medium':   1.0,
        'large':    1.200,
        'x-large':  1.440,
        'xx-large': 1.728,
        'larger':   1.2,
        'smaller':  0.833,
        None:       1.0,
    

    【讨论】:

    • 谢谢@mullinscr。我尝试使用 fontsize='xx-small' ,但不幸的是,蓝色文本甚至大于最小尺寸(fontsize=1)。像这样:i.imgur.com/hZiYou0.png
    • 在这种情况下,最好的办法可能是将其他所有内容按比例放大以适应文本。
    • 我尝试缩放我的情节中的所有内容。我正在用两个变量 x_coordsy_coords 绘制散点图 xy。 x_coords = [i*5 for i in x_coords ]y_coords = [i*5 for i in y_coords ] 然后,plt.axis([0, max(x_coords), 0, max(y_coords )])。只有刻度值会缩放,但图中的所有内容都保持不变。这是正确的做法吗?
    猜你喜欢
    • 2014-09-09
    • 2013-01-16
    • 1970-01-01
    • 1970-01-01
    • 2014-08-03
    • 1970-01-01
    • 2014-06-22
    • 1970-01-01
    • 2023-04-05
    相关资源
    最近更新 更多