matplotlib aspect ratio for narrow matrices - python

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:

Related

How can I properly plot a infinity on a matplotlib scatter plot

I have a series of number, and one of it contains a infinity so I changed it into 1.797693134862315E+308.
however the plot does not show data properly
import matplotlib.pyplot as plt
from numpy.random import randn
from pylab import *
import numpy as np
fig=plt.figure()
ax2=fig.add_subplot(2,2,2)
ax2.scatter([1,2,3,50],[1,2,3,1.797693134862315E+308])
show()

Finding certain plotting style

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()

Only show round numbers on x-axis in point plot

If I use the following code I end up with an overcrowded x-axis. I would like to show only every 10th number on the x axis. Meaning [0,10,...].
Any idea how to do this?
import pandas as pd
import numpy as np
import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt
a = pd.DataFrame({'y':np.random.randn(100)})
a['time']=a.index
ax = sns.pointplot(x='time', y="y", data=a)
plt.show()
You may decide not to use a pointplot at all. A usual lineplot seems to suffice.
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
a = pd.DataFrame({'y':np.random.randn(100)})
plt.plot(a.index, a.y)
plt.show()
Now this gives ticks at steps of 20. The easiest option here would be to use
plt.xticks(range(0,101,10))
to get the steps of 10. Or equally possible,
plt.gca().locator_params(nbins=11)
to devide the axis into 11 bins.
Of course the use of an appropriate locator would be equally possible.

Function for ploting a matrix in ipython using matplotlib

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()

Matplotlib, 3D logaxis, incomplete figure

I am playing with matplotlib, I would like to have a 3d figure with logarithmic axis. I was trying some code, like the one below, but I can only see part of the figure at a time, if I try to move it, I can see other parts, but, not complete.
Does anyone have any idea how to make a 3D plot with log axis?
I can see the 3D image if the axis are linear, but as soon as I change to "log", I can only see part of it.
import matplotlib as mpl
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
data=np.array([[1,10,100],[10,1,1],[2,20,82]])
fig=plt.figure()
ax=Axes3D(fig)
#ax.set_xlim3d(0.1,15)
#ax.set_ylim3d(0.1,15)
#ax.set_zlim3d(0.1,15)
ax.xaxis.set_scale('log')
ax.yaxis.set_scale('log')
ax.zaxis.set_scale('log')
ax.scatter(data[:,0],data[:,1],data[:,2])
plt.show()
I updated matplotlib to 1.3.1, and now I can see the full figure. Now, I think the axis are not in a log scale. I made a plot in matplotlib and the same plot with gnuplot , and it can be seen that the distances between every power of 10, are comlpetely different.
The 3d scatter plot requires x,y,z arguments: if you are trying to plot z data[:,2] (3 points) function of x data[:,0] and y data[:,1], you will see 3 points when the xlim3d,ylim3d,zlim3d are set correctly. This can be done by setting the them to min() and max() of each x,y,z value:
import matplotlib as mpl
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
data=np.array([[1,10,100],[10,1,1],[2,20,82]])
fig=plt.figure()
ax=Axes3D(fig)
ax.set_xlim3d(data[:,0].min(),data[:,0].max())
ax.set_ylim3d(data[:,1].min(),data[:,1].max())
ax.set_zlim3d(data[:,2].min(),data[:,2].max())
ax.xaxis.set_scale('log')
ax.yaxis.set_scale('log')
ax.zaxis.set_scale('log')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.scatter(data[:,0],data[:,1],data[:,2])
plt.show()
Now if you wanted to plot the data array in 3d manner, 9 points in this case, you would need the respective x and y axis. This can be done with np.meshgrid(). In this example I have set x, y equidistant [1,2,3].
import matplotlib as mpl
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
data=np.array([[1,10,100],[10,1,1],[2,20,82]])
datax=np.array([1,2,3])
datay=np.array([1,2,3])
dataxM,datayM = np.meshgrid(datax, datay)
fig=plt.figure()
ax=Axes3D(fig)
ax.set_xlim3d(datax.min(),datax.max())
ax.set_ylim3d(datay.min(),datay.max())
ax.set_zlim3d(data.min(),data.max())
ax.xaxis.set_scale('log')
ax.yaxis.set_scale('log')
ax.zaxis.set_scale('log')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.scatter(dataxM,datayM,data)
plt.show()

Categories