【问题标题】:How to change linewidth when saving PDF figure保存PDF图形时如何更改线宽
【发布时间】:2019-08-20 12:54:51
【问题描述】:

我需要生成 PDF 格式的图形以获得更好的质量(eps 仍然会很好)。在图中,我需要一个振荡背景,也就是实际信号,线宽较细,中间有较粗(3 倍)的移动平均滤波线。

如果我将图像保存在 .png 中,则保留所需的线宽,同样不适用于 .pdf、.eps 和 .pgf,即使我没有找到任何东西,它们似乎也有最小线宽在这个意义上,在文档上。我在 Spyder 上运行 Python 3.7.3,更新了 matplotlib。我尝试在 ax.plot() 调用和 rcParams 中指定线宽,但行为没有改变:只有 .png 保持正确的格式。

我附上了执行实际绘图的函数:

import matplotlib.pyplot as plt
import numpy as np

def data_plot(xaxis, sequences, xlabel, ylabel, legend, widths,
             alphas, colors, fil):

    n = len(sequences)    
    fig, ax = plt.subplots(figsize=(10,6))

    for i in range(n):
        ax.plot(xaxis, sequences[i], linewidth=widths[i], alpha=alphas[i], 
                color=colors[i])
        ax.plot(xaxis[int(fil/2)-1:len(xaxis)-int(fil/2)], 
                np.convolve(sequences[i],np.ones(fil)/fil,mode='valid'),
                linewidth=4*widths[i], alpha=2*alphas[i], color=colors[i], 
                label=legend[i])

    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    ax.legend(loc='upper right', prop={'size': 12})

    plt.show()

seq = [np.random.uniform(12,14,500), np.random.uniform(11,12.5,500), 
np.random.uniform(12.7,15,500)]
x = np.linspace(1,100,500)
data_plot(x,seq,'x','y',['A','B','C'],
          [0.2,0.2,0.2],[0.5,0.4,0.3],['C0','C1','C2'],10)

plt.savefig('figure.png')
plt.savefig('figure.pdf')

【问题讨论】:

  • 请告诉您尝试的线宽是多少。
  • 也就是说,在询问代码的不良行为时,请始终使用minimal reproducible examples。
  • 对不起,你是对的。我用这个宽度参数调用了该方法:[0.4,0.3,0.2],但我试图改变它。如果我用 [0.1,0.1,0.1] 调用它,则行为也更加明显。这就是为什么我会说存在最低支持。
  • 我提供了一个最小的工作示例。该代码将图形保存在 .png 和 .pdf 中,对我来说它们是不同的。我希望 .pdf 看起来像上面描述的 .png 图像。
  • 您最终找到解决方案了吗?我也有类似的问题。

标签: python python-3.x matplotlib


【解决方案1】:

万一其他人登陆这里,我遇到了同样的问题,结果证明这是由于 PDF 查看器造成的。具体来说,使用 Foxit Reader 版本 11.0.0.49893;见here。卸载后downgrading 到 10.1.3 线宽显示properly。所以请与其他观众核实。

但是,这不是 OP 脚本的问题。运行它会保存空白图像。这是因为在plt.savefig 之前调用了plt.show;有关说明,请参阅here。 例如,您可以在函数内移动savefig,或返回fig。然后线宽将在保存的 PDF/PNG 文件上正确显示:saved plot

在下面粘贴代码。 *为了强调,将线宽从“4”增加到“20”。

import matplotlib.pyplot as plt
import numpy as np

def data_plot(xaxis, sequences, xlabel, ylabel, legend, widths,
             alphas, colors, fil):

    n = len(sequences)    
    fig, ax = plt.subplots(figsize=(10,6))

    for i in range(n):
        ax.plot(xaxis, sequences[i], linewidth=widths[i], alpha=alphas[i], 
                color=colors[i])
        ax.plot(xaxis[int(fil/2)-1:len(xaxis)-int(fil/2)], 
                np.convolve(sequences[i],np.ones(fil)/fil,mode='valid'),
                linewidth=20*widths[i], alpha=2*alphas[i], color=colors[i], 
                label=legend[i])
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    ax.legend(loc='upper right', prop={'size': 12})

    plt.show()
    return fig


seq = [np.random.uniform(12,14,500), np.random.uniform(11,12.5,500), 
np.random.uniform(12.7,15,500)]
x = np.linspace(1,100,500)
fig = data_plot(x,seq,'x','y',['A','B','C'],
          [0.2,0.2,0.2],[0.5,0.4,0.3],['C0','C1','C2'],10)

fig.savefig('figure.png')
fig.savefig('figure.pdf')

【讨论】:

  • 这并不能解决问题:问题是由于背景噪声较薄,而 .pdf 格式较厚。
猜你喜欢
  • 2013-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-18
相关资源
最近更新 更多