The following code generates a simple plot, where the y axes has a label generated by a LaTeX command
import numpy as np
import matplotlib.pyplot as plt
%matplotlib notebook
plt.figure()
hatdelta = '$\hat{\Delta}$'
xlist = np.array([ 0, 1 ])
ylist = np.array([ 1, 2 ])
plt.errorbar(xlist, ylist, fmt='o', capsize=2)
ax = plt.gca()
ax.set_ylabel(hatdelta, fontsize=16)
plt.draw()
plt.show()
I am using jupyter-notebook to run the code.
This is the actual result:
A close inspection of the y axes reveals that the LaTeX is incorrectly rendered:
The hat is not centered over the symbol. However, the LaTeX code should correctly center the hat. This is, for example, the output of a LaTeX source with the same command:
Is it possible to fix this incorrect LaTeX rendering?
You have two options.
import numpy as np
import matplotlib.pyplot as plt
# To use LaTeX
plt.rcParams['text.usetex'] = True
plt.figure()
hatdelta = '$\hat{\Delta}$'
xlist = np.array([ 0, 1 ])
ylist = np.array([ 1, 2 ])
plt.errorbar(xlist, ylist, fmt='o', capsize=2)
ax = plt.gca()
ax.set_ylabel(hatdelta, fontsize=16)
plt.draw()
plt.show()
Or
import numpy as np
import matplotlib.pyplot as plt
plt.figure()
# Improve \hat
hatdelta = '$\hat{\,\Delta}$'
xlist = np.array([ 0, 1 ])
ylist = np.array([ 1, 2 ])
plt.errorbar(xlist, ylist, fmt='o', capsize=2)
ax = plt.gca()
ax.set_ylabel(hatdelta, fontsize=16)
plt.draw()
plt.show()
Related
I have this code:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1)
a = np.array([1,2,3])
b = a
ax1.plot(a,b)
ax2 = ax1.twinx()
ax2.set_position(matplotlib.transforms.Bbox([[0.125, 0.125], [0.9, 0.2]]))
c = np.array([4,5,6])
d = c
ax2.plot(c,d)
plt.show()
When I run this with Python 2, it results in:
The problem is when I try to use the same code using Python 3 I get this picture:
How can I have the same result using Python 3?
This was a bug, which has now been fixed (so it has nothing to do with the python version, but rather the matplotlib version in use). You could use an inset_axes instead of just a usual subplot. The latter could look like this:
import numpy as np
from matplotlib.transforms import Bbox
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(111, label="first")
ax2 = fig.add_subplot(111, label="second")
ax2.set_position(Bbox([[0.125, 0.125], [0.9, 0.2]]))
ax1.get_shared_x_axes().join(ax1, ax2)
ax2.yaxis.tick_right()
ax2.tick_params(bottom=False, labelbottom=False)
ax2.set_facecolor("none")
a = np.array([1,2,3])
ax1.plot(a,a)
c = np.array([4,5,6])
ax2.plot(c,c)
plt.show()
I have two 2D arrays and I want to use to produce an image similar to the one that fallows, just with different limits on the axis.
Here is my attempt so far:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.set_xlim(-2.01, 2.01)
ax.set_ylim(-2.01, 2.01)
ax.set_zlim(-2.01, 2.01)
cmap = plt.cm.gray
im = ax.imshow(np.asarray(array1), cmap=cmap)
im.remove()
fig.colorbar(im)
plt.show()
The arrays I have, (array1 and array2) are two dimensional with sizes n by n. Any help or a point in the right direction will be greatly appreciated!
With help of Matplotlib - Plot a plane and points in 3D simultaneously, I am able to achieve this:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_zlim(0, 1)
ax.set_xticks([0, 0.2, 0.4, 0.6, 0.8, 1])
ax.set_yticks([0, 0.5, 1])
ax.set_zticks([0, 0.2, 0.4, 0.6, 0.8, 1])
cmap = plt.cm.gray
#plot vertical surface
y = 0.5
xx, zz = np.meshgrid(np.linspace(0,1,10), np.linspace(0,1,10))
p = ax.plot_surface(xx, y, zz, cmap=cmap, alpha=0.5)
x = 0.2
yy, zz = np.meshgrid(np.linspace(0,1,10), np.linspace(0,1,10))
p = ax.plot_surface(x, yy, zz, cmap=cmap, alpha=0.5)
fig.colorbar(p)
plt.show()
Note that I didn't use normal or dot just as another question do, because here you want to plot vertical planes.
Here's what I got(I'm working on the right occlusion):
I am updating a 3d scatter plot with every iteration of a loop. When the plot is redrawn, the gridlines "go through" or "cover" the points, which makes my data more difficult to visualize. If I build a single 3d plot (no loop updating) this does not happen. The code below demonstrates the simplest case:
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import time
X = np.random.rand(100, 3)*10
Y = np.random.rand(100, 3)*5
plt.ion()
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X[:, 0], X[:, 1], X[:, 2])
plt.draw()
for i in range(0, 20):
time.sleep(3) #make changes more apparent/easy to see
Y = np.random.rand(100, 3)*5
ax.cla()
ax.scatter(Y[:, 0], Y[:, 1], Y[:, 2])
plt.draw()
Has anyone else encountered this problem?
It looks like MaxNoe is right in the sense that the problem is in the ax.cla()or plt.cla() call. In fact it seems it is something like a known issue.
Then there is a problem, since the clear axes method doesn't work in 3D plots and for 3D scatters there is no clean way to change the coordinates of the data points (a la sc.set_data(new_values)), as discussed in this mail list (I didn't find anything more recent).
In the mail list, however, Ben Roon points to a workaround that might be useful for you, too.
Workaround:
You need to set the new coordinates of the datapoints in the internal _ofsets3d variable of the Line3DCollectionobject returned by the scatter function.
Your example adapted would look like:
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import time
X = np.random.rand(100, 3)*10
Y = np.random.rand(100, 3)*5
plt.ion()
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
sc = ax.scatter(X[:, 0], X[:, 1], X[:, 2])
fig.show()
for i in range(0, 20):
plt.pause(1)
Y = np.random.rand(100, 3)*5
sc._offsets3d = (Y[:,0], Y[:,1], Y[:,2])
plt.draw()
I could narrow it down to the use of cla():
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x, y = np.meshgrid(np.linspace(-2,2), np.linspace(-2,2))
ax.plot_surface(x,y, x**2+y**2)
fig.savefig("fig_a.png")
ax.cla()
ax.plot_surface(x,y, x**2+y**2)
fig.savefig("fig_b.png")
these are the resulting plots:
This is but a workaround, as it does not resolve the issue with ax.cla() pointed out by MaxNoe. It is also not particularly pretty since it clears the entire figure, however it does the desired task:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig1 = plt.figure()
ax1 = fig1.add_subplot(111, projection='3d')
x, y = np.meshgrid(np.linspace(-2,2), np.linspace(-2,2))
ax1.plot_surface(x,y, x**2+y**2)
fig1.savefig("fig_a.png")
fig1.clf()
ax1 = fig1.add_subplot(111, projection='3d')
ax1.plot_surface(x,y, x**2+y**2)
fig1.savefig("fig_b.png")
I'd suggest using ax = fig.gca(projection='3d') instead of ax = fig.add_subplot(111, projection='3d') .
I am trying to plot a colorbar in Python using the matplotlib libraries. The plot works well, but the color bar wont show the end ticks on the color bar.
Using the following command plots the colorbar properly, but I only have ticks ranging from -1.6 to +1.6 ( the top and bottom ticks are absent). The range of my data is from -2 to +2.
fig.colorbar(surf, shrink=1, aspect=12)
This is seen in the following figure:
I tried using the command:
cbar.set_ticks([-2,-1,0,1,2]);
But that also failed to get the correct result.
UPDATE:
I tried using the ticks=[-2,0,2] parameter, but that did not work as well. This is a snippet of my plotting code:
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
CHI = np.linspace(-45, 45, 35);
M = np.linspace(0, 1, 35)
CHI, M = np.meshgrid(CHI, M)
R = 2*M*np.sin( 2 * np.deg2rad(CHI) )
surf = ax.plot_surface(CHI, M, R, rstride=1, cstride=1, cmap=cm.hsv,linewidth=0, antialiased=False)
ax.set_xlim(-45,45)
cbar = plt.colorbar(surf, shrink=1, aspect=12, ticks=[-2,-1,0,1,2])
plt.show()
This produces the following plot:
As seen, the colorbar lacks the end ticks, viz. -2 and 2
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
CHI = np.linspace(-45, 45, 35);
M = np.linspace(0, 1, 35)
CHI, M = np.meshgrid(CHI, M)
R = 2*M*np.sin( 2 * np.deg2rad(CHI) )
surf = ax.plot_surface(CHI, M, R, rstride=1, cstride=1, cmap=cm.hsv,linewidth=0, antialiased=False)
surf.set_clim([-2, 2]) # <- this is the important line
ax.set_xlim(-45,45)
cbar = plt.colorbar(surf, shrink=1, aspect=12, ticks=[-2,-1,0,1,2])
plt.show()
Something is going wrong with the auto-scaling (I would guess issues with floating point equality tests), if you explicitly set the clim to [-2, 2] it works.
You should be able to set the ticks parameter when calling plt.colorbar:
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
cmap = plt.get_cmap('jet')
data = np.random.randint(-2,3, size=(10,10))
im = ax.imshow(data, interpolation='nearest', cmap=cmap)
cbar = plt.colorbar(im, ticks=[-2,-1,0,1,2])
plt.show()
I'd like to plot a rotated parabolic cylinder. Like holding one point of the plane of symmetry with two fingers and let this plane rotate about this point.
There is the rotation matrix (for the y-axis).
And the parametrised surface is:
(u,v,u**2)
I've already plotted this surface for rotations about the x-axis:
Below is the modified(!) code to plot the rotations about the y-axis.
from math import cos,sin,pi
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.auto_scale_xyz([0, 500], [0, 500], [0, 0.15])
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
Us = np.arange(-1, 1, 0.005)
Vs = np.arange(-1, 1, 0.005)
for ch in [0.2*i*pi for i in range(3)]:
Xs = Us*cos(ch) + sin(ch)*(Us**2)
Ys = Vs
Xs, Ys = np.meshgrid(Xs, Ys)
Zs = -Us*sin(ch) + cos(ch)*(Us**2)
Axes3D.plot3D(ax,Xs,Ys,Zs, alpha=0.05)
And it doesn't work. It produces nonsense. This is the output i get:
The operations are identical, so i really can't see why it doesn't work. Can anyone tell me what i'm doing wrong?
I need to make this work in order to just look at the way algebraic surfaces transform when subjected to certain rotations. The code above is a crudely simplified snippet of what i am currently debugging. So the answer i am hoping for regards this particular code.
Here is another version which uses your explicit rotation equations:
import matplotlib as mpl
mpl.use('TkAgg')
from math import cos,sin,pi
import numpy as np
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.auto_scale_xyz([0, 500], [0, 500], [0, 0.15])
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
U = np.arange(-1, 1, 0.005)
V = np.arange(-1, 1, 0.005)
def animate():
for ch in [0.2*i*pi for i in range(3)]:
Us, Vs = np.meshgrid(U, V)
Xs = Us*cos(ch) + sin(ch)*(Us**2)
Ys = Vs
Zs = -Us*sin(ch) + cos(ch)*(Us**2)
tmp = ax.plot_surface(Xs,Ys,Zs, alpha=0.05)
fig.canvas.draw()
tmp.remove()
win = fig.canvas.manager.window
fig.canvas.manager.window.after(100, animate)
plt.show()
The rotate_axes3d_demo does it with ax.view_init this way:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
plt.ion()
sin = np.sin
cos = np.cos
pi = np.pi
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.auto_scale_xyz([0, 500], [0, 500], [0, 0.15])
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
Us, Vs = np.meshgrid(np.linspace(-1, 1, 200), np.linspace(-1, 1, 200))
ax.plot_surface(Us,Vs,Us**2)
for angle in range(0, 360):
ax.view_init(angle, 30)
plt.draw()