1

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. lines with multiple coulors

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))
Ivan
  • 599
  • 4
  • 18
  • 1
    You could use more points such that the curve appears smoother. In the simplest case one can just double the number of points, as in [this answer](https://stackoverflow.com/a/47856091/4124317). – ImportanceOfBeingErnest Apr 23 '18 at 17:31

1 Answers1

1

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))
Ivan
  • 599
  • 4
  • 18
  • Thanks, had the same problem, but I used cairosvg library on windows. Note that I still had an issue, solved by an answer (not the accepted one) in https://stackoverflow.com/questions/33812902/pycharm-cannot-find-library – BaptisteL Dec 07 '20 at 09:44