【问题标题】:Scale legend box border, dashed and dotted lines when the figure size is changed with matplotlib使用 matplotlib 更改图形大小时缩放图例框边框、虚线和虚线
【发布时间】:2011-03-12 13:44:38
【问题描述】:

我正在尝试使用 matplotlib 准备一些要发布的数据。为了使字体大小与手稿的文本相匹配,我尝试以最终大小创建图形,以便在将图形插入手稿时避免缩放图形。

我遇到的问题是,由于图非常小,我可以缩放字体大小、轴大小、线宽等,但我一直无法弄清楚如何缩放虚线或点线线条,以及图例边框框的粗细。对于一个简化且有些夸张的示例,请考虑


#!/usr/bin/python

small = True


from matplotlib import use
use('pdf')

from matplotlib import rc
rc('ps', usedistiller='xpdf')
rc('text', usetex=True)

if small:
    figsize = (1.0, 0.5)
    rc('font', size=2)
    rc('axes', labelsize=2, linewidth=0.2)
    rc('legend', fontsize=2, handlelength=10)
    rc('xtick', labelsize=2)
    rc('ytick', labelsize=2)
    rc('lines', lw=0.2, mew=0.2)
    rc('grid', linewidth=0.2)
else:
    figsize = (8,8)

import numpy as np

x = np.arange(0, 10, 0.001)
y = np.sin(x)

import matplotlib.pyplot as plt
f = plt.figure(figsize=figsize)
a = f.add_subplot(111)
a.plot(x, y, '--', label='foo bar')
a.legend()
f.savefig('mplt.pdf')

如果您将第一个可执行行更改为small = False,您可以看到它在“正常”大小下的外观。与正常大小相比,小图的图例框边框太粗,虚线太粗,即虚线太长,虚线之间的距离太长。

所以我的问题是,有没有办法解决这两个问题?

我使用的 matplotlib 版本是 0.99.1.2。

【问题讨论】:

    标签: python matplotlib plot


    【解决方案1】:

    要调整dashes,请使用

    a.plot(x, y, '--', label='foo bar', dashes=(2,2))
    

    还有legend box的线宽,

    lg = a.legend()
    fr = lg.get_frame()
    fr.set_lw(0.2)
    

    【讨论】:

    • 谢谢,成功了!可惜它们不能由 rc 参数设置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 2012-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-28
    相关资源
    最近更新 更多