如何绘制和导出边缘平滑的多色线 [英] How to plot and export multi-colored line with smooth edges

查看:79
本文介绍了如何绘制和导出边缘平滑的多色线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用多种颜色绘制一条线,为此我遵循了

我尝试添加圆头 对于每一行,但它不会影响导出的 pdf 结果(而 plt.show() 绘图看起来更好,并且带有圆边).

参考代码:

  lc = mcoll.LineCollection(segments,array = z,cmap ='copper',linewidth = 3)ax.add_collection(lc)plt.savefig("{}_l.pdf".format(filenamebase))

解决方案

当我使用svg格式,然后用inkscape转换图时,问题就消失了.虽然原来的问题仍然存在,而且显然 pdf 的 savefig 有问题,但是 svg->pdf 技巧避免了这个问题并使线条的边缘变圆.

下面的脚本实现了这个技巧(并且还生成了 pdf + tex 文件以便于嵌入到文章中).

  def export_plot(文件名库,export_area_drawing =真,export_latex =真):plt.savefig("{}_l.pdf".format(filenamebase))如果 export_latex:ead =''如果export_area_drawing:ead ='--export-area-drawing'el ='--export-latex'plt.savefig("{}.svg" .format(filenamebase))cmd ='''env -u LD_LIBRARY_PATH inkscape -T -f {0} .svg \--export-background-opacity=0 \{1} {2} --export-pdf = {0} .pdf'''.format(文件名库,ead,el)操作系统(cmd)os.system('rm {}.svg'.format(filenamebase))

I tried to plot a single line with multiple colors and for that I followed this example.

Exported pdf result has breaks at the starts and the ends of the lines, while I would like the result to a smooth line without breaks. LineCollection plots every single line in one color, but I am also fine with smoothly changing colors.

I tried to add round ends for each line, but it does not affect the exported pdf result (while plt.show() plot looks nicer and with round edges in place).

Code for reference:

lc = mcoll.LineCollection(segments, array=z, cmap='copper', linewidth=3)
ax.add_collection(lc)
plt.savefig("{}_l.pdf".format(filenamebase))

解决方案

The problem has gone when I used svg format and then converted the plot with inkscape. Though the original problem remains, and apperently there is something wrong with savefig for pdf, but svg->pdf trick avoids the problem and makes the round edges of the lines.

The script below implements the trick (and also produces pdf + tex files for a convenient embedding into an article).

def export_plot(filenamebase, export_area_drawing = True, export_latex = True):

  plt.savefig("{}_l.pdf".format(filenamebase))

  if export_latex:
    ead = ''
    if export_area_drawing:
      ead = '--export-area-drawing'

    el = '--export-latex'
    plt.savefig("{}.svg".format(filenamebase))
    cmd = ''' env -u LD_LIBRARY_PATH inkscape -T -f {0}.svg \
              --export-background-opacity=0 \
              {1} {2} --export-pdf={0}.pdf'''.format(filenamebase, ead, el)
    os.system(cmd)
    os.system('rm {}.svg'.format(filenamebase))

这篇关于如何绘制和导出边缘平滑的多色线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆