1

I use the Python library matplotlib to draw a graph with a lot of data. Upon executing plt.show() I can zoom in and see the details of the graph. However, I would like to save the graph into a svg file with plt.savefig and see these details which by default are not visible from the default non-zoomed-in view. How can I do that?

Please, note that increasing DPI or inch by inch dimensions is meaningless when working with vector graphics formats such as the svg file.

As an example, consider the following program.

import matplotlib.pyplot as plt
import numpy as np
import math

x = np.arange(0,100,0.00001)
y = x*np.sin(2*math.pi*(x**1.2))
plt.plot(y)
plt.savefig('test.svg')

We will get the following plot which even when we zoom, we cannot see the details of the sine wave periods.enter image description here

But we can see the details of the sine wave when displaying the image with plt.show instead and then zooming in.enter image description here

sentence
  • 8,213
  • 4
  • 31
  • 40
Dávid Natingga
  • 839
  • 3
  • 13
  • 30
  • The saved svg should have all the details you need. What exactly is the problem? – ImportanceOfBeingErnest Feb 04 '20 at 13:14
  • [This post](https://stackoverflow.com/questions/24525111/how-can-i-get-the-output-of-a-matplotlib-plot-as-an-svg) might be helpful. Note that increasing the dimension does help as `1/72" per unit` is used. – JohanC Feb 04 '20 at 13:15
  • @ImportanceOfBeingErnest It does not have all the details. I edited the post with an example that demonstrates my claim. – Dávid Natingga Feb 04 '20 at 13:41
  • @JohanC Thank you for the link. I adapted the example from there to demonstrate my problem. As you can see in the program I provided in the edit, I do not use any background and axis markers. Hence the linked post still does not solve my problem. – Dávid Natingga Feb 04 '20 at 13:43
  • 2
    You need to set a thinner line if you want to see something when zoomed in for that example. `plt.plot(y, linewidth=0.1)` – JohanC Feb 04 '20 at 14:06
  • @JohanC Thank you. With `linewidth=0.1` the problem was resolved too. – Dávid Natingga Feb 04 '20 at 14:43

1 Answers1

2

Add the size of the figure:

import matplotlib.pyplot as plt
import numpy as np
import math

x = np.arange(0,100,0.00001)
y = x*np.sin(2*math.pi*(x**1.2))
fig = plt.figure(figsize=(19.20,10.80))

plt.plot(y)
plt.savefig('test.svg')

and you get the kind of resolution you wish.


As correctly observed by JohanC, another good solution is to reduce the width of the line:

import matplotlib.pyplot as plt
import numpy as np
import math

x = np.arange(0,100,0.00001)
y = x*np.sin(2*math.pi*(x**1.2))
#fig = plt.figure(figsize=(19.20,10.80))

plt.plot(y, linewidth=0.1)
plt.savefig('test.svg')
NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
sentence
  • 8,213
  • 4
  • 31
  • 40
  • 1
    If you don't adapt the linewidth, you still won't see much when zoomed in. If you do adapt the linewidth and don't set a figsize, the image looks fine when zoomed in. – JohanC Feb 04 '20 at 14:09