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.
But we can see the details of the sine wave when displaying the image with plt.show instead and then zooming in.
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')
I am looking for a plotting function in matplotlib that plots the y-values as bars just like in an autocorrelogram but for a general function. Is there a method to do this in matplotlib or do I have to write my own function?
You could use stem
import numpy as np; np.random.seed(21)
import matplotlib.pyplot as plt
x = np.linspace(5,75)
y = np.random.randn(len(x))
plt.stem(x,y, linefmt="k", markerfmt="none", basefmt="C0", use_line_collection=True)
plt.show()
I have a 200x3 matrix in python which I would like to plot. However, by using Matplotlib I get the following figure. How can I plot an image which looks nicer?
my code:
import matplotlib.pyplot as plt
plt.imshow(spectrum_matrix)
plt.show()
You can use set_aspect():
import matplotlib.pyplot as plt
import numpy as np
spectrum_matrix = np.random.rand(200,3)
plt.imshow(spectrum_matrix)
plt.axes().set_aspect('auto')
plt.show()
Output:
I have created a matrix:
s1=np.random.randn(1000,1000)
v1=la.eigvals(s1)
matrix1=np.matrix(v1)
I want to plot the matrix in ipython.
What appropriate matplotlib function should I use?
A very simple example to "plot" a matrix:
import numpy as np
import pylab as plt
S = np.random.randn(100,100)
# Make symmetric so everything is real
S += S.T
W,V = np.linalg.eigh(S)
import pylab as plt
plt.imshow(V,interpolation="none")
plt.show()
I am trying to simply fill the area under the curve of a plot in Python using MatPlotLib.
Here is my SSCCE:
import json
import pprint
import numpy as np
import matplotlib.pyplot as plt
y = [0,0,0,0,0,0,0,0,0,0,0,863,969,978,957,764,767,1009,1895,980,791]
x = np.arange(len(y))
fig2, ax2 = plt.subplots()
ax2.fill(x, y)
plt.savefig('picForWeb.png')
plt.show()
The attached picture shows the output produced.
Does anyone know why Python is not filling the entire area in between the x-axis and the curve?
I've done Google and StackOverflow searches, but could not find a similar example. Intuitively it seems that it should fill the entire area under the curve.
I usually use the fill_between function for these kinds of plots. Try something like this instead:
import numpy as np
import matplotlib.pyplot as plt
y = [0,0,0,0,0,0,0,0,0,0,0,863,969,978,957,764,767,1009,1895,980,791]
x = np.arange(len(y))
fig, (ax1) = plt.subplots(1,1);
ax1.fill_between(x, 0, y)
plt.show()
See more examples here.
If you want to use this on a pd.DataFrame use this:
df.abs().interpolate().plot.area(grid=1, linewidth=0.5)
interpolate() is optional.
plt.fill assumes that you have a closed shape to fill - interestingly if you add a final 0 to your data you get a much more sensible looking plot.
import numpy as np
import matplotlib.pyplot as plt
y = [0,0,0,0,0,0,0,0,0,0,0,863,969,978,957,764,767,1009,1895,980,791,0]
x = np.arange(len(y))
fig2, ax2 = plt.subplots()
ax2.fill(x, y)
plt.savefig('picForWeb.png')
plt.show()
Results in:
Hope this helps to explain your odd plot.