使用 matplotlib 更改图形大小时缩放图例框边框、虚线和点线
我正在尝试使用 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。
I'm trying to use matplotlib to prepare some figures for publication. In order to make the font sizes match the text of the manuscript I'm trying to create the figure in the final size to begin with, so that I avoid scaling the figure when inserting it into the manuscript.
The problem I'm having is that as the figure is then pretty small, I can scale font sizes, axis sizes, line widths etc., but what I've been unable to figure out is how to scale dashed or dotted lines, as well as the thickness of the legend border box. For a simplified and somewhat exaggerated example, consider
#!/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')
If you change the first executable line to small = False
you can see how it should look in "normal" size. Compared to the normal size, the small plot suffers from a legend box with too thick borders, and the dashed line is too coarse, i.e. too long dashes and too long distance between the dashes.
So my question is, is there a way to fix these two problems?
The matplotlib version I'm using is 0.99.1.2.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要调整破折号,请使用
和图例框线宽,
To adjust the dashes, use
and the legend box line width,