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()
Related
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'm trying to plot a sparse matrix (10000 x 10000) elements. I've tried using the spy() function from both matplotlib and MATLAB. I see a big difference in the output, but I can't figure out why.
Why is there such a big difference?
Matlab:
matplotlib:
For the matplotlib version, I'm using an iPython notebook with Python 2.7
Code for import:
import matplotlib.pylab as pl
import networkx as nx
import numpy as np
import scipy.io
from MCL import MCL
from SpectralClusterizer import SpectralClusterizer
pl.xkcd()
%matplotlib inline
pl.rcParams['figure.figsize'] = (15.0, 12.0) # set default size of plots
%load_ext autoreload
%autoreload 2
Code for plotting:
pl.spy(A[perm][:, perm], markersize = 1, precision = 0)
pl.show()
Is there any possibility within pyqtgraph to get the blue graph instead of the red one that is standard method in matplotlib AND pyqtgraph in the following matplotlib example:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0,1,0.1)
y = [0,0,0,2,2,8,9,2,0,0]
plt.plot(x,y,color='red',drawstyle='standard')
plt.plot(x,y,color='blue',drawstyle='steps-post')
plt.show()
So far I did not find a way to create the blue graph in pyqtgraph with the given data x and y.
Thanks a lot
Regards
Michael
This is described in the pyqtgraph 'histogram' example. This is the translation:
import pyqtgraph as pg
import numpy as np
x = np.arange(0,1,0.1)
y = [0,0,0,2,2,8,9,2,0,0]
print len(x), len(y)
plt = pg.plot(x,y,pen='r')
curve2 = pg.PlotCurveItem(x,y[:-1],pen='b',stepMode=True)
plt.addItem(curve2)