I want to plot a surface and zoom to a specific part of it.
I have:
r1 = np.arange(10)
r2 = np.arange(15)
x, y = np.meshgrid(r2,r1)
z = np.random.rand(10,15)
fig = plt.figure(figsize=(16,10))
ax = fig.gca(projection='3d')
surf = ax.plot_surface(x, y, z, cmap=cm.jet)
axes = plt.gca()
fig.tight_layout()
plt.show()
That gives:
Now, if I set axis limits, I have a problem
r1 = np.arange(10)
r2 = np.arange(15)
x, y = np.meshgrid(r2,r1)
z = np.random.rand(10,15)
fig = plt.figure(figsize=(16,10))
ax = fig.gca(projection='3d')
surf = ax.plot_surface(x, y, z, cmap=cm.jet)
axes = plt.gca()
axes.set_xlim([r1[2],r1[8]])
axes.set_ylim([r2[2],r2[8]])
fig.tight_layout()
plt.show()
gives:
What I was expecting was it to cut the original fig in the limits I asked, without putting the plot outside the box. How do I do that?
Related
I am trying to plot x vs y data and trying to see the variation of x and y with respect to z using a colorbar.
x = [1,2,3,4,5]
x_err = [0.1,0.2,0.3,0.4,0.5]
y = [5,6,7,8,9]
y_err = [0.5,0.6,0.7,0.8,0.9]
z = [3,4,5,6,7]
fig, ax = plt.subplots()
ax.errorbar(x, y, x_err, y_err, fmt='*', elinewidth = 0.9, ecolor='black')
scatter = ax.scatter(x, y, c=z, s=5)
cbar = fig.colorbar(scatter,cmap='viridis')
cbar.set_label('z')
I need the errorbar to have the same color as that of the datapoint.
You could compute the ecolor from the same cmap. Not sure there aren't any solution to do that for you, but it doesn't cost much
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
x = [1,2,3,4,5]
x_err = [0.1,0.2,0.3,0.4,0.5]
y = [5,6,7,8,9]
y_err = [0.5,0.6,0.7,0.8,0.9]
z = [3,4,5,6,7]
fig, ax = plt.subplots()
# Rest of your code is yours. Only this line is added (and next line modified to use this "col" as ecolor
col=cm.viridis((np.array(z)-min(z))/(max(z)-min(z))) # RGBA colors from z
ax.errorbar(x, y, x_err, y_err, ecolor=col, fmt='*', elinewidth = 0.9)
scatter = ax.scatter(x, y, c=z, s=5)
cbar = fig.colorbar(scatter,cmap='viridis')
cbar.set_label('z')
plt.show()
Result
I want to plot a curve over a background image with the x and y axis in logscale. However, when I try to do so, the image is stretched by the logscale. I obtain this figure
This is the code I wrote.
import numpy as np
import matplotlib.pyplot as plt
x = np.random.uniform(low=0, high=10**6, size=(100,))
y = np.random.uniform(low=10**(-14), high=10**(-10), size=(100,))
x.sort()
y.sort()
xm = len(x)
ym = len(y)
img = plt.imread("quiverplot.png")
fig, ax2 = plt.subplots()
plt.plot(x, y)
ax2.set_xscale('log')
ax2.set_yscale('log')
ax1 = ax2.twinx()
img = ax1.imshow(img, zorder=0, extent=[x[0], x[xm-1], y[0], y[ym-1]], aspect='auto')
fig.tight_layout()
plt.show()
Thanks for any advices you can give me.
Don't use twinx(), but create a new axes with matplotlib.pyplot.axes().
You can do like this controlling the frame(background), x/y axis, and z-order.
fig, ax2 = plt.subplots()
ax2.plot(x, y)
ax2.set_xscale('log')
ax2.set_yscale('log')
ax2.set_frame_on(False)
ax2.zorder = 1
ax1 = plt.axes(ax2.get_position(True))
ax1.set_frame_on(False)
ax1.set_axis_off()
ax1.imshow(img, extent=[x[0], x[xm-1], y[0], y[ym-1]], aspect='auto')
...
It will be simpler if you change the order of plotting like this.
fig, ax2 = plt.subplots()
ax2.imshow(img, extent=[x[0], x[xm-1], y[0], y[ym-1]], aspect='auto')
ax2.set_axis_off()
ax1 = plt.axes(ax2.get_position(True))
ax1.set_frame_on(False)
ax1.set_xscale('log')
ax1.set_yscale('log')
ax1.plot(x, y)
...
Hi I am trying to create:
horizontally stacked plots
Have secondary axis on both plots
Have different scales on axis - unfortunately, both my Y axis currently have the same scale per subplot... :(
Current code:
# Create axes
fig, (ax1, ax2) = plt.subplots(1, 2)
fig.suptitle("XYZ")
fig.set_figheight(5)
fig.set_figwidth(15)
# First graph
ax1.scatter(
df_PTA_clip_pstar["start_time"],
df_PTA_clip_pstar["pstar"],
s=5,
c="black",
label="P*",
)
plt.ylabel("P*")
ax1.scatter(df_PTA_clipkh["start_time"], df_PTA_clipkh["kh"], s=2, c="cyan", label="Kh")
ax1.secondary_yaxis("right")
plt.ylabel("Kh")
# Second graph - will add the correct data to this once first graph fixed
ax2.scatter(x, y, s=5, c="Red", label="P*")
ax2.scatter(x, z, s=5, c="Green", label="Kh")
ax2.secondary_yaxis("right")
plt.tight_layout()
plt.legend()
plt.show()
Current progress:
You can use .twinx() method on each ax object so you can have two plots on the same ax object sharing x-axis:
import matplotlib.pyplot as plt
import numpy as np
# Create axes
fig, (ax1, ax2) = plt.subplots(1, 2)
## First subplot
x = np.random.random_sample(100)
y = np.random.random_sample(100)
ax1.set_xlim(0, 2)
ax1.scatter(x, y,
s=5,
c="black")
ax11 = ax1.twinx()
x = 1 + x
y = 1 + np.random.random_sample(100)
ax11.scatter(x, y,
s=5,
c="red")
## Second subplot
x = 2 * np.random.random_sample(100) - 1
y = np.random.random_sample(100)
ax2.set_xlim(-1, 2)
ax2.scatter(x, y,
s=5,
c="blue")
ax21 = ax2.twinx()
x = 1 + x
y = 10 + np.random.random_sample(100)
ax21.scatter(x, y,
s=5,
c="orange")
plt.show()
I have timeseries data which I've segmented into hundreds of chunks. I solved the autocorrelation for each segment and plotted them:
# plot superimposed
fig = plt.figure()
color = iter(plt.cm.Set2(np.linspace(0,1,num_segs)))
seg_iterator = df.iterrows()
for index, seg in seg_iterator: # iterate over dataframe
c=next(color)
sns.plt.plot(seg, color=c)
Next, I plotted them as a 3D surface:
# plot as a surface
surfacefig = plt.figure()
surfaceax = surfacefig.gca(projection='3d')
X = np.arange(LAGS+1)
Y = np.arange(num_segs)
X, Y = np.meshgrid(X, Y)
surfaceax.plot_surface(X, Y, df, cmap=plt.cm.Set2)
plt.show()
How can I map colors to row index (rather than z-values)? I'd like to preserve the colors of the lines.
Update with result:
# updated lines. Make sure XX and YY are floats
surf = surfaceax.plot_surface(XX, YY, df, shade=False,
facecolors=plt.cm.Set2((YY-YY.min()) / (YY.max()-YY.min())),
cstride=1, rstride=5, alpha=0.7)
plt.draw() # you need this to get the edge color
line = np.array(surf.get_edgecolor())
surf.set_edgecolor(line*np.array([0,0,0,0])+1)
You can try this:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
X = np.linspace(-np.pi, np.pi, 200, endpoint=True)
Y = np.linspace(-np.pi, np.pi, 200, endpoint=True)
XX, YY = np.meshgrid(X,Y)
Z = np.cos(XX)*np.cos(YY)
fig = plt.figure()
ax1 = plt.subplot2grid((1,2), (0,0), projection='3d')
ax2 = plt.subplot2grid((1,2), (0,1), projection='3d')
surf = ax1.plot_surface(XX, YY, Z,
cmap=plt.cm.Set2)
surf2 = ax2.plot_surface(XX, YY, Z, shade=False,
facecolors=plt.cm.Set2((XX-XX.min())/(XX.max()-XX.min()))
)
Where on the second plot, you set the facecolors as being function of XX, instead of Z by default. You need to rescale your XX values between 0 and 1 or the colormap will be saturated outside 0 and 1. You also need to remove the shade which is removed when yous use cmap (in the first plot).
However, for some unknown reasons, the lines disappear.
You can add them back with:
plt.draw() # you need this to get the edge color
lines = np.array(surf2.get_edgecolor())
surf2.set_edgecolor(lines*np.array([0,0,0,0])+1) # make lines white, and keep alpha==1. It's an array of colors like this: [r,g,b,alpha]
It gives:
HTH
I have a surface plot, and I need this specific point of view that I have chosen. See the image below:
Now, as you can see, the very bottom part of my axis legend is missing, because matplotlib is cutting it off. Is there any way to programmatically zoom out of the plot so everything fits in the window?
This is my original code:
values_all = zip(*values_all)
x = range(len(values_all[0]))
y = range(len(values_all))
figure = plt.figure(1, figsize=(10, 7))
ax = Axes3D(figure, azim=-124, elev=40, zlim=(0, 0.4))
x, y = np.meshgrid(x, y)
surface = ax.plot_surface(x, y, values_all, linewidth=0, rstride=1, cstride=1, cmap=cm.jet)
plt.colorbar(surface, shrink=0.4, aspect=10)
plt.show()
Call
plt.tight_layout()
before
plt.show()