How to a reach solid surface in 3d surface in matplotlib? - python-3.x

How to reach a solid surface in 3d surface in matplotlib, please? I tried to apply plot_surface more times, but the refinement is limited and not enough. The transparency is still obvious. More sample also did not help.
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
# Coordinate for the cylinder
def data_for_cylinder_along_z(center_x,center_y,radius,height_z):
z = np.linspace(0, height_z, 200)
theta = np.linspace(0, 2*np.pi, 200)
theta_grid, z_grid=np.meshgrid(theta, z)
x_grid = radius*np.cos(theta_grid) + center_x
y_grid = radius*np.sin(theta_grid) + center_y
return x_grid,y_grid,z_grid
fig = plt.figure(figsize = (5,10))
ax = fig.add_subplot(111, projection='3d')
ax.set_xlim(-2, 2)
ax.set_ylim(-2, 2)
ax.set_zlim(0, 10)
ax.get_proj = lambda: np.dot(Axes3D.get_proj(ax), np.diag([0.4, 0.4, 1.6, 1])) # (x, y, z) ration of sides
theta1 = np.linspace(0, 2*np.pi, 100)
r1 = np.linspace(-2, 0, 100)
t1, R1 = np.meshgrid(theta1, r1)
X1 = R1*np.cos(t1)
Y1 = R1*np.sin(t1)
Z1 = 3+R1*1.5+4
ax.set_xlabel('x axis')
ax.set_ylabel('y axis')
ax.set_zlabel('z axis')
ax.plot_surface(X1, Y1, Z1, color="dimgray")
ax.plot_surface(X1, Y1, Z1, color="dimgray")
# Cylinder
Xc,Yc,Zc = data_for_cylinder_along_z(0,0,2,4)
rep=10
for i in range(rep):
ax.plot_surface(Xc, Yc, Zc, color = 'palegoldenrod')
plt.show()

you need to set antialiased to True (found by trial and error, couldn't find description in the docs):
# Cylinder
Xc,Yc,Zc = data_for_cylinder_along_z(0,0,2,4)
ax.plot_surface(Xc, Yc, Zc, color='palegoldenrod', antialiased=False)

Related

Using an image as a matplotlib marker and drawing a circle around that marker

I was using an image as a matplotlib marker , I have attached the code below
import matplotlib.pyplot as plt
from matplotlib import patches
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
import random
plt.rcParams["figure.figsize"] = [10.0,10.0]
plt.rcParams["figure.autolayout"] = True
def getImage(path):
return OffsetImage(plt.imread(path, format="png"), zoom=.1)
paths = ['BS.png']
for i in range(49):
paths.append('BS.png',)
#print(paths)
x = []
for i in range(50):
x.append(random.randint(0,10))
print(x)
y = []
for i in range(50):
y.append(random.randint(0,10))
print(y)
fig, ax = plt.subplots()
circle1 = patches.Circle((5, 5), radius=20, fill = False ,edgecolor = 'black')
for x0,y0,path in zip(x, y, paths):
ab = AnnotationBbox(getImage(path), (x0, y0), frameon=False)
ax.add_artist(ab)
plt.xticks(range(10))
plt.yticks(range(10))
ax.axis('off')
plt.show()
The output is as follows
Now how do I draw a circle around these markers in the graph? A dashed circle is preferrable.
I tried to use circle1 = patches.Circle((x0, y0), radius=20, fill = False ,edgecolor = 'black')
So that a circle is drawn around every marker but it does not draw anything.
You cannot see the circle because the set radius is too large to be displayed inside the canvas (radius = 20, while the axis range is (0, 10)).
These lines of code
fig, ax = plt.subplots()
for x0,y0,path in zip(x, y, paths):
ab = AnnotationBbox(getImage(path), (x0, y0), frameon=False)
ax.add_artist(ab)
circle = plt.Circle((x0, y0), radius=0.5, color='black', fill=False, linestyle='--')
ax.add_artist(circle)
plt.xticks(range(-1, 12))
plt.yticks(range(-1, 12))
ax.axis('off')
plt.show()
produces

Heat map for Irregularly Spaced Data with No Interpolation

I would like to plot a heatmap where the input data is not in the typical rectangularly spaced grid. Here is some sample data:
import numpy as np
xmin = 6
xmax= 12
ymin = 0
x = np.linspace(xmin, xmax, 100)
ymax = x**2
final = []
for i in range(len(ymax)):
yrange = np.linspace(0, ymax[i], 100)
for j in range(len(yrange)):
intensity = np.random.rand()
final.append([x[i], yrange[j], intensity])
data_for_plotting = np.asarray(final) # (10000, 3) shaped array
I would like to plot intensity (in the colorbar) as a function of (x,y) which represents the position and I would like to do this without interpolation.
Here is my solution which uses matplotlib's griddata and linear interpolation.
import matplotlib.pyplot as plt
from matplotlib.mlab import griddata
total_length = 100
x1 = np.linspace(min(data_for_plotting[:,0]), max(data_for_plotting[:,0]), total_length)
y1 = np.linspace(min(data_for_plotting[:,1]), max(data_for_plotting[:,1]), total_length)
z1 = griddata(data_for_plotting[:,0], data_for_plotting[:,1], data_for_plotting[:,2], x1, y1, interp='linear')
p=plt.pcolormesh(x1, y1, z1, vmin = 0. , vmax=1.0, cmap='viridis')
clb = plt.colorbar(p)
plt.show()
I am looking for an alternate solution without interpolation as I would like to see the smallest unit of measurement in my x and y position (pixel size/rectangle). Based on the sample data given above I expect the height of the pixel to increase for large values of x.
I'm unsure what matplotlib.mlab.griddata is about. Maybe some very old version?
You could use scipy.interpolate.griddata which needs its parameters in a slightly different format. method='nearest' switches off the interpolation (default method='linear').
Here is how it could look with your test data (see griddata's documentation for more explanation and examples):
import matplotlib.pyplot as plt
from scipy.interpolate import griddata
import numpy as np
xmin = 6
xmax = 12
ymin = 0
x = np.linspace(xmin, xmax, 100)
ymax = x ** 2
final = []
for i in range(len(ymax)):
yrange = np.linspace(0, ymax[i], 100)
for j in range(len(yrange)):
intensity = np.random.rand()
final.append([x[i], yrange[j], intensity])
data_for_plotting = np.asarray(final) # (10000, 3) shaped array
total_length = 100
x1 = np.linspace(min(data_for_plotting[:, 0]), max(data_for_plotting[:, 0]), total_length)
y1 = np.linspace(min(data_for_plotting[:, 1]), max(data_for_plotting[:, 1]), total_length)
grid_x, grid_y = np.meshgrid(x1, y1)
z1 = griddata(data_for_plotting[:, :2], data_for_plotting[:, 2], (grid_x, grid_y), method='nearest')
img = plt.imshow(z1, extent=[x1[0], x1[-1], y1[0], y1[-1]], origin='lower',
vmin=0, vmax=1, cmap='inferno', aspect='auto')
cbar = plt.colorbar(img)
plt.show()
An alernative, is to create one rectangle for each of the prolonged pixels. Beware that this can be a rather slow operation. If really needed, one could create a pcolormesh for each column.
import matplotlib.pyplot as plt
from matplotlib.cm import ScalarMappable
import numpy as np
# ... create x and data_for_plotting as before
fig, ax = plt.subplots()
cmap = plt.get_cmap('inferno')
norm = plt.Normalize(0, 1)
x_step = x[1] - x[0]
y_step = 0
for i, (xi, yi, intensity_i) in enumerate(data_for_plotting):
if i + 1 < len(data_for_plotting) and data_for_plotting[i + 1, 0] == xi: # when False, the last y_step is reused
y_step = data_for_plotting[i + 1, 1] - yi
ax.add_artist(plt.Rectangle((xi, yi), x_step, y_step, color=cmap(norm(intensity_i))))
cbar = plt.colorbar(ScalarMappable(cmap=cmap, norm=norm))
ax.set_xlim(x[0], x[-1])
ax.set_ylim(0, data_for_plotting[:, 1].max())
plt.tight_layout()
plt.show()

How to generate the plot of custom shape using numpy or other python libraries

I am interested in generating some random plots for data-based classification problems. These plots are generated inside the x-y plane. The maximum value of x and y is one. The main purpose of this is to generate a dummy dataset for a classification algorithm. The below figure is an example of the expected plot. Other than this I am also written a small code.
import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(0.15, 0.95, 100, endpoint = True)
x= x.reshape(100, 1)
noise =np.random.normal(0,0.1, [100,1])*0.25
x=x+noise;
s=(100,1);
y=0.5*(np.ones(s));
xy=np.hstack((x,y));
plt.plot(x,y)
x1 = np.linspace(0.15*np.pi, 0.95*np.pi, 100)
x1max=max(x1)
x1=x1/x1max;
y1 = 2*np.cosh(x1/2)
y1max=max(y1)
y1=y1/y1max;
plt.plot(x1, y1)
x2 = np.linspace(0.15*np.pi, 2*np.pi, 100)
x2max=max(x2)
x2=x2/x2max;
y2 = np.sin(x2)
y2max=max(y2)
y2=y2/y2max;
plt.plot(x2, y2)
def cart2pol(a, b):
rho = np.sqrt(a**2 +b**2)
theta = np.arctan2(b,a)
return(rho, theta)
def pol2cart(rho, phi):
a = rho * np.cos(theta)
b = rho * np.sin(theta)
return(a, b)
[x3,y3]=cart2pol(x,y)
x3max=max(x3)
x3=x3/x2max;
y3max=max(y3)
y3=y3/y3max;
plt.plot(x3, y3)
[x4,y4]=cart2pol(x1,y1)
x4max=max(x4)
x4=x4/x4max;
y4max=max(y4)
y4=y4/y4max;
plt.plot(x4, y4)

How to color space between two lines with a colour transition?

How to colour space between two lines with a colour transition? For instance, with grey colour - the dark grey from the upper line should become lighter as proceeding to the lower line. Thank you
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
plt.rcParams["figure.figsize"] = [8, 8]
x = np.linspace(0, 1, 100)
y = 0.3*x
ax.set_ylim(-0.2, 0.6)
ax.plot(x, y)
width_l = ax.get_ylim()[1] - ax.get_ylim()[0]
ax.plot(x, y - 0.1*width_l)
plt.show()
Edit
And this, please? How to make the width of the coloured part the same?
import matplotlib.pyplot as plt
import numpy as np
import pywt
fig, ax = plt.subplots()
plt.rcParams["figure.figsize"] = [8, 8]
wavelet = pywt.ContinuousWavelet('morl')
psi, x = wavelet.wavefun(level=2)
cmap = plt.get_cmap('Greys_r')
ax.plot(x, psi)
ax.set_xlim(ax.get_xlim()[0], ax.get_xlim()[1])
y_a = ax.get_ylim()
ax.set_ylim(y_a[0],y_a[1]*1.3)
width_l = ax.get_ylim()[1] - ax.get_ylim()[0]
x_range = ax.get_xlim()[1] - ax.get_xlim()[0]
x_shift = x_range * 0.1
ax.plot([x[0]+x_shift, x[1]+x_shift], [psi[0], psi[1]])
ax.plot([x[2]-x_shift, x[3]-x_shift], [psi[2], psi[3]])
ax.plot([x[1], x[2]], [psi[1]-width_l*0.1, psi[2]-width_l*0.1])
for t in np.linspace(0, 1, 40):
ax.plot(x, psi - t * 0.1 * width_l, color=cmap(t/2 + 0.25))
plt.show()
You could draw a lot of parallel lines (or curves) using a color from a gray-scale colormap. The example code below uses a transformation u = t/2 + 0.25, so when t goes from 0 to 1, u would just go between 0.25 and 0.75 to select of specific range from the colormap, avoiding the very dark and very light parts.
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
plt.rcParams["figure.figsize"] = [8, 8]
x = np.linspace(0, 1, 100)
y = 0.3 * x
width_l = ax.get_ylim()[1] - ax.get_ylim()[0]
ax.set_ylim(-0.2, 0.6)
cmap = plt.get_cmap('Greys_r')
for t in np.linspace(0, 1, 40):
u = t/2 + 0.25
ax.plot(x, y - t * 0.1 * width_l, color=cmap(u))
ax.plot(x, y)
ax.plot(x, y - 0.1 * width_l)
plt.show()

How to set linewidth of axis in 3d plot in python?

How to set linewidth of axis in 3d plot in python? Is it somehow possible with mpl.rcParams?
Code:
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
mpl.rcParams['legend.fontsize'] = 10
fig = plt.figure()
ax = fig.gca(projection='3d')
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
ax.plot(x, y, z, label='parametric curve')
ax.legend()
plt.show()
Try this:
ax.plot_surface(X, Y, Z, linewidth=1)

Resources