Matplotlib, 3D logaxis, incomplete figure - python

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

Related

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

Matplotlib 3D plot use colormap

I am trying to use ax.scatter to plot a 3D scattering plot. I've read the data from a fits file and stored data from three column into x,y,z. And I have made sure x,y,z data are the same size. z has been normolized between 0 and 1.
import numpy as np
import matplotlib
from matplotlib import pylab,mlab,pyplot,cm
plt = pyplot
import pyfits as pf
from mpl_toolkits.mplot3d import Axes3D
import fitsio
data = fitsio.read("xxx.fits")
x=data["x"]
y=data["y"]
z=data["z"]
z = (z-np.nanmin(z)) /(np.nanmax(z) - np.nanmin(z))
Cen3D = plt.figure()
ax = Cen3D.add_subplot(111, projection='3d')
cmap=cm.ScalarMappable(norm=z, cmap=plt.get_cmap('hot'))
ax.scatter(x,y,z,zdir=u'z',cmap=cmap)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.show()
What I am trying to achieve is use color to indicate the of size of z. Like higher value of z will get darker color. But I am keep getting a plot without the colormap I want, they are all the same default blue color. What did I do wrong? Thanks.
You can use the c keyword in the scatter command, to tell it how to color the points.
You don't need to set zdir, as that is for when you are plotting a 2d set
As #Lenford pointed out, you can use cmap='hot' in this case too, since you have already normalized your data.
I've modified your example to use some random data rather than your fits file.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
x = np.random.rand(100)
y = np.random.rand(100)
z = np.random.rand(100)
z = (z-np.nanmin(z)) /(np.nanmax(z) - np.nanmin(z))
Cen3D = plt.figure()
ax = Cen3D.add_subplot(111, projection='3d')
ax.scatter(x,y,z,cmap='hot',c=z)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.show()
As per the pyplot.scatter documentation, the points specified to be plotted must be in the form of an array of floats for cmap to apply, otherwise the default colour (in this case, jet) will continue to apply.
As an aside, simply stating cmap='hot' will work for this code, as the colour map hot is a registered colour map in matplotlib.

How to plot a 3d scatterplot with the color determined from another variable?

I made a 3d scatterplot that displays the position of galaxies in a cluster (basically like the latitude and longitude) as a function of their velocity. However, I've been asked to make the color of the data points be determined by another variable, h in the code. The purpose of the variable isn't important to know, but that in my actual code, every data point is determined from 4 arrays. After spending a long time looking up how to do this, I finally (almost) have it. The only problem is that when I plot it, the colors of the dots change as soon as I move the plot around to see it from a different direction. Also, I've been having issues trying to display a colorbar.
import pylab as p
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import matplotlib.cm as cm
ra=np.random.random((100))
dec=np.random.random((100))
h=np.random.random((100))
z=np.random.random((100))
datamin=min(h)
datamax=max(h)
fig=p.figure()
ax3D=fig.add_subplot(111, projection='3d')
ax3D.scatter(ra, dec, z, c=h, vmin=datamin, vmax=datamax,
marker='o', cmap=cm.Spectral)
p.title("MKW4s-Position vs Velocity")
p.show()
'Changing color upon redraw' issue was a bug but looks like it's fixed in the latest release (1.1.1). I've tested and confirmed that it's working as it should with 1.1.1.
For the colorbar, it needs a mappable. You can use the collection returned from scatter:
import pylab as p
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import matplotlib.cm as cm
ra=np.random.random((100))
dec=np.random.random((100))
h=np.random.random((100))
z=np.random.random((100))
datamin=min(h)
datamax=max(h)
fig=p.figure()
ax3D=fig.add_subplot(111, projection='3d')
collection = ax3D.scatter(ra, dec, z, c=h, vmin=datamin, vmax=datamax,
marker='o', cmap=cm.Spectral)
p.colorbar(collection)
p.title("MKW4s-Position vs Velocity")
p.show()

horizontal plot in python

I am looking for a plot that is rotated 90 degree in clockwise directions. An similar example of such plot is "hist(x, orientation='horizontal')". Is there any way to achieve similar orientation.
#Make horizontal plots.
import random
import matplotlib.pyplot as plt
x = random.sample(range(1000), 100)
x
plt.plot(x) #orientation='horizontal'
plt.show()
plt.plot(x) plots your x values automatically against the y-axis. In order to get a rotated plot you have to plot your x values against the x axis. So you'll need a to make vector for the y-axis, which has the same length as your sample.
import random
import matplotlib.pyplot as plt
import numpy as np
x=random.sample(1000)
y=np.arange(1000)
plt.plot(x,y)
Using plt.plot(x), matplotlib takes your x-values as its y-values and generates a vector for the x axis automatically.

PyLab: Plotting axes to log scale, but labelling specific points on the axes

Basically, I'm doing scalability analysis, so I'm working with numbers like 2,4,8,16,32... etc and the only way graphs look rational is using a log scale.
But instead of the usual 10^1, 10^2, etc labelling, I want to have these datapoints (2,4,8...) indicated on the axes
Any ideas?
There's more than one way to do it, depending on how flexible/fancy you want to be.
The simplest way is just to do something like this:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
x = np.exp2(np.arange(10))
plt.semilogy(x)
plt.yticks(x, x)
# Turn y-axis minor ticks off
plt.gca().yaxis.set_minor_locator(mpl.ticker.NullLocator())
plt.show()
If you want to do it in a more flexible manner, then perhaps you might use something like this:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
x = np.exp2(np.arange(10))
fig = plt.figure()
ax = fig.add_subplot(111)
ax.semilogy(x)
ax.yaxis.get_major_locator().base(2)
ax.yaxis.get_minor_locator().base(2)
# This will place 1 minor tick halfway (in linear space) between major ticks
# (in general, use np.linspace(1, 2.0001, numticks-2))
ax.yaxis.get_minor_locator().subs([1.5])
ax.yaxis.get_major_formatter().base(2)
plt.show()
Or something like this:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
x = np.exp2(np.arange(10))
fig = plt.figure()
ax = fig.add_subplot(111)
ax.semilogy(x)
ax.yaxis.get_major_locator().base(2)
ax.yaxis.get_minor_locator().base(2)
ax.yaxis.get_minor_locator().subs([1.5])
# This is the only difference from the last snippet, uses "regular" numbers.
ax.yaxis.set_major_formatter(mpl.ticker.ScalarFormatter())
plt.show()

Categories