我有一个图形,它具有一些对线宽敏感的相当微妙的功能。我想把这个数字保存为一个可以轻松打印的PDF格式(即在接收端没有缩放比例,只需Command + P就可以了)。不幸的是,当我设置figsize =(8.5,11)以正确确定打印PDF的大小时,matplotlib会选择一个非常粗的默认线宽和文本大小,从而导致图形混乱(图例太大,条形图中的线条交叠)。如果我设置figsize =(17,22),则在将PDF缩放为50%进行打印后,我会得到非常可行的默认线宽和文本大小。这是我一直在使用的,但由于政治原因,这个解决方案已经变得不可行,而且我实际上不希望每次改变时都在插图中扩展PDF。保存PDF时matplotlib线宽
如果我可以使用位图,我可以通过设置figsize =(17,22)并将dpi设置为目标dpi的一半来获得所需的结果,但这对PDF无效,因为dpi参数似乎被忽略。我想一个PDF是
- 看起来像boxes_good.png(大小,欺骗位细线,小文)
- 有8.5x11in尺寸(或打印像它)
- 可编辑在Illustrator(不是包裹在一个PDF位图)
我不禁怀疑,作为一个PDF格式保存文档时,有拉一个简单的方法“双尺寸,半DPI”伎俩,但我放弃了开始工作,并开始尝试直接操纵线宽和文本大小。我成功地修改了文本大小,但不是线宽。下面是我试过的东西记录:
# Tried:
# fig.set_size_inches(17,22)
# fig.savefig('boxes.pdf', dpi=100,150,300)
# dpi parameter has no effect on linewidth, text size, or the PDF's dimensions
# 'markerscale=.5' on plt.legend and pax.legend
# no effect
# mp.rcParams['font.size']=8
# worked! made text smaller, now for linewidth...
# mp.rcParams['lines.linewidth']=5
# no effect
# fig.set_linewidth(5)
# no effect
# pax.axhline(linewidth=5)
# only changes x axis not box surrounding subplot
# fig.set_size_inches(8.5,11) immediately before plt.savefig('boxes.pdf')
# identical results to calling plt.figure(figsize=(8.5,11)) in the first place
# I tried passing plt.figure(figsize=(17,22)) and swapping it to 8.5x11 using
# fig.set_size_inches right before saving, but the lines were thick and the text
# was large in the PDF, exactly as if I had set figsize=(8.5,11) to begin with
这里是的资源文件(我已经减少了阴谋要领,这么明显的造型变通方法可能不是可行的解决方案)
import numpy as np
import matplotlib as mp
import matplotlib.pyplot as plt
x = np.arange(200)
bottom_red_bar = -np.random.random(200)
bottom_black_bar = np.random.random(200) * bottom_red_bar
fig = plt.figure()
for subplotnum in [1,2,3]:
pax = plt.subplot(310+subplotnum)
pax.set_ylim([-1,1])
bot_rb = pax.bar(x, bottom_red_bar,1,color='r')
bot_bb = pax.bar(x+(1-.3)/2,bottom_black_bar,.3,color='k')
pax.legend([bot_rb,bot_bb],['Filler Text 1','Filler Text 2'],loc=4)
fig.set_size_inches(8.5,11)
fig.savefig('boxes_bad.png',dpi=300) # Lines are too thick
fig.set_size_inches(17,22)
fig.savefig('boxes_good.png',dpi=150) # Lines are just right
fig.set_size_inches(8.5,11)
plt.savefig('boxes.pdf') # Lines are too thick
fig.set_size_inches(17,22) # Lines are just right
plt.savefig('boxes.pdf') # but the PDF needs scaling before printing
所以我在调整整个图形的线宽之后,或者让matplotlib保存一个带有与figsize不同的维度元数据的pdf。有什么建议么?