Axes3D.text() Annotate 3D Scatter Plot - python

Can't get the 3D text working to annotate the scatter plot points.
Tried Axes3D.text, plt.text but keep getting 'missing required positional argument 's'. How do you annotate in 3D in a loop?
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse
import pandas as pd
import numpy as np
df = pd.read_csv (r'J:\Temp\Michael\Python\9785.csv')
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
#Scatter plot
for i in df.index:
x = df.at[i,'x']
y = df.at[i,'y']
z = df.at[i,'h']
ax.scatter(xs=x, ys=y, zs=z, s=20,color='red',marker='^')
label = df.at[i,'to']
Axes3D.text(x+0.8,y+0.8,z+0.8, label, zdir=x)
TypeError: text() missing 1 required positional argument: 's'

Changing: ax = fig.add_subplot(111, projection='3d')
to: ax = fig.gca(projection='3d')
solved the problem. Used ax.text.
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse
import pandas as pd
import numpy as np
df = pd.read_csv (r'J:\Temp\Michael\Python\9785.csv')
fig = plt.figure()
ax = fig.gca(projection='3d')
#Scatter plot
for i in df.index:
df.set_index('to')
x = df.at[i,'x']
y = df.at[i,'y']
z = df.at[i,'h']
ax.scatter(xs=x, ys=y, zs=z, s=20,color='red',marker='^')
ax.text(x+0.8,y+0.8,z+0.8, df.at[i,'to'], size=10, zorder=1)

Related

Format the y-axis label in the higher thousands with many zeros to scientific notation?

How can I tighten the y-axis by using scientific notation?
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
x=[1,2,3,4,5,6,7,8,9,10]
y=np.array([1,1,1,2,10,2,1,1,1,1])*100000
ax.plot(x, y)
With plt.ticklabel_format:
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
x=[1,2,3,4,5,6,7,8,9,10]
y=np.array([1,1,1,2,10,2,1,1,1,1])*100000
ax.plot(x, y)
plt.ticklabel_format(axis="y", style="sci", scilimits=(0,5))
plt.show()

How do you scale a polygon patch in matplotlib?

In the example below, I create a rectangular patch using matplotlib.patches.Polygon. Is there a way to scale the patch before adding it to the plot?
I've tried using matplotlib.transforms.Affine2D in a variety of ways with no success. As usual, the matplotlib documentation on transformations is woefully insufficient.
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot([-3,3],[-3,3])
x = [-1,0,1,1,0,-1]
y = [1,1,1,-1,-1,-1]
poly = Polygon( zip(x,y), facecolor='red', edgecolor='red', alpha=0.5)
ax.add_patch(poly)
plt.show()
If by scale you mean multiplication by a factor, you can easily do this via numpy.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot([-3,3],[-3,3])
x = [-1,0,1,1,0,-1]
y = [1,1,1,-1,-1,-1]
scale = 2
poly = Polygon( np.c_[x,y]*scale, facecolor='red', edgecolor='red', alpha=0.5)
ax.add_patch(poly)
plt.show()
The same can be achieved with a matplotlib.transforms.Affine2D() transform.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
import matplotlib.transforms as transforms
fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot([-3,3],[-3,3])
x = [-1,0,1,1,0,-1]
y = [1,1,1,-1,-1,-1]
trans = transforms.Affine2D().scale(2) + ax.transData
poly = Polygon( np.c_[x,y], facecolor='red', edgecolor='red', alpha=0.5,
transform=trans)
ax.add_patch(poly)
plt.show()
Although it seems a bit overkill for a simple scaling like this.

Gridlines Above 3D Scatter Plot in matplotlib

When making a 3D scatter plot with matplotlib I cannot seem to control whether the axes are above or below the plot. For example the following code will always have the x and y axes above the plot if ax1.elev < 0
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
fig = plt.figure('Test')
X = np.random.rand(1,100)
Y = np.random.rand(1,100)
Z = np.random.rand(1,100)
ax1 = fig.add_subplot(111, projection = '3d')
ax1.scatter(X,Y,Z)
ax1.view_init(-10,45)
Is it possible to force the x and y axes and the gridlines and planes to be below the plot even though ax1.elev < 0?
I take as an example the code of this question (thanks crayzeewulf). Except for the z-axis, we do it for the x- and y-axis
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
fig = plt.figure('Test')
X = np.random.rand(1,100)
Y = np.random.rand(1,100)*10
Z = np.random.rand(1,100)
ax1 = fig.add_subplot(111, projection = '3d')
ax1.scatter(X,Y,Z)
ax1.view_init(-10,45)
tmp_planes = ax1.zaxis._PLANES
ax1.xaxis._PLANES = ( tmp_planes[3], tmp_planes[2],
tmp_planes[1], tmp_planes[0],
tmp_planes[5], tmp_planes[4])
ax1.yaxis._PLANES = ( tmp_planes[3], tmp_planes[2],
tmp_planes[1], tmp_planes[0],
tmp_planes[5], tmp_planes[4])
view_1 = (25, -135)
view_2 = (-10, 45)
init_view = view_2
ax1.view_init(*init_view)

Pandas Series Plot and GridSpec

I'm having problems with the following code:
import numpy as np
import pandas as pd
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
xx = np.arange(10)
yy = np.arange(10)
zz = pd.Series(np.arange(10))
fig = plt.figure()
gs = gridspec.GridSpec(1,6)
ax0 = plt.subplot(gs[0,0:2])
zz.plot(ax=ax0)
ax1 = plt.subplot(gs[0,2:4])
zz.plot(ax=ax1)
ax2 = plt.subplot(gs[0,4:])
#plt.plot(xx,yy)
zz.plot(ax=ax2)
plt.savefig('test.png')
plt.close()
I get the error:
IndexError: index 4 is out of bounds for axis 1 with size 4
However, when I substitute the
zz.plot(ax=ax2)
with
plt.plot(xx,yy)
it works fine. Can anyone help me understand why Pandas generates this error?

Polar plot without grid in matplotlib

Is there a way to turn of the grid for polar plots in matplotlib? I tried matplotlib.pyplot.rgrids([], []), but it doesn't work.
From your axes instance, call grid(False).
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, polar=True)
ax.grid(False)
r = np.arange(0,1,0.001)
theta = 2*2*np.pi*r
ax.plot(theta,r)
plt.show()

Categories

Resources