I have a 3D projection plot which is really a collection of 2D slices of some value. I also shade the underlying area with:
ax.add_collection3d(plt.fill_between())
I have a couple of related questions:
1) The plot looks fine but the axis labels look messed up - they are rendered on top of the tick labels. How can I space them out a bit?
2) How can I choose what camera angle is rendered? (I'm using Jupiter notebook).
3) The background of the plot (behind the grid, not the grid color itself) is this light blueish grey. how can make that white?
Thanks!
for number 1) ax.set_xlabel() has a labelpad parameters that can be set to any number of pixels.
Related
I'm trying to make a plot with multiple violin plots on one axis. The amount of violin plots is variable, and I would like to make plot 1 a very light blue, plot 2 a little darker blue, all the way to the last plot, which I would like to be black. So depending on the amount of plots, the steps in the color gradient should be higher or lower. Is there a (simple) way to do this with matplotlib itself (i.e. no extra module)? I referenced matplotlib's documentation on color maps, but this did not bring me much further. Anyone who could help? Thanks in advance!
# Current code:
for i, l in enumerate(self.H):
vplot = ax3.violinplot(
l, positions=[i + 1], widths=0.5, showmeans=True, showmedians=False)
for pc in vplot['bodies']:
pc.set_facecolor(colors[i])
pc.set_label(labels[i])
I want to plot images (in the 1st row) along with some diagrams (the 2nd and 3rd rows) using subplots from matplotlib.pyplot. However, imshow fucntion adds some additional white space around images I can't get rid of. Here is my code and the plot I'm getting:
rcParams['figure.figsize'] = (16, 14)
_, axes = plt.subplots(3, 3)
axes[0][0].imshow(image)
axes[0][0].set_title('title')
axes[0][0].set_xticklabels(list())
axes[0][0].set_yticklabels(list())
axes[0][0].grid(False)
axes[0][1].imshow(image)
axes[0][1].set_title('title')
axes[0][1].set_xticklabels(list())
axes[0][1].set_yticklabels(list())
axes[0][1].grid(False)
axes[0][2].imshow(image)
axes[0][2].set_title('title')
axes[0][2].set_xticklabels(list())
axes[0][2].set_yticklabels(list())
axes[0][2].grid(False)
plt.savefig(file_name, bbox_inches='tight')
in the plot below you can clearly see that there is significantly more space between the 1st and 2nd rows:
I would like to have an equal space between all subplots. What would be the easiest way to do this?
Thanks in advance for any advice!
Best,
Alexey
This is because imshow is showing the image with square pixels. If the image as a ratio of e.g. 16:9, the subplot will be reshaped to fit the image size. They will therefore have a different shape from the other subplots (see the imshow documentation for more info).
From here, you have two solutions:
decrease the figure height in order to reduce manually the vertical space between subplots
prevent imshow to resize the axes based on the images. For this you can set the aspect ratio to automatic aspect="auto", and the image will fit the existing axes
I am trying to rotate the following figure around the vertical green axis drawn:
However, I'm running into trouble trying to set the correct elevation and azimuthal values in order to correctly rotate my figure.
For example,
for i in range(0,360):
axU.view_init(100-i,-90+i)
plt.draw()
plt.savefig('./gif1/rot%i.jpg'%i,dpi=100)
gives me a figure like
http://imgur.com/b26d0V2
and
for i in range(0,360):
axU.view_init(100,-90+i)
plt.draw()
plt.savefig('./gif1/rot%i.jpg'%i,dpi=100)
looks something like:
http://imgur.com/3wdN8zT
both give me too much rotations around unwanted axes, where as I really just want to pan around the green axis drawn above. Is there any way to do this?
for i in range(0,360):
axU.view_init(100-i,-90)
plt.draw()
plt.savefig('./gif1/rot%i.jpg'%i,dpi=100)
This give me something similar to what I want, where the rotation is uniform around one axis, but this rotates around the horizontal green axis whereas I would like it to rotate around the vertical green axis.
rotation
http://imgur.com/b4zeUiI
This one is a quick and easy one for the matplotlib community. I was looking to plot an L-shaped gridspec layout, which I have done:
Ignoring a few layout issues I have for the moment, what I have is that the x-axis in the gs[0] plot (top left) shares the x-axis with the gs[2] plot (bottom left) and the gs[2] shares its y axis with the gs[3] plot. Now, what I was hoping to do was update the w-space and h-space to be tighter. So that the axes are almost touching, so perhaps wspace=0.02, hspace=0.02 or something similar.
I was also hoping that the bottom right hand plot was to be longer in the horizontal orientation, keeping the two left hand plots square in shape. Or as close to square as possible. If someone could run through all of the parameters I would be very appreciative. I can tinker then in my own time.
To change the spacings of the plot with grid spec:
gs = gridspec.GridSpec(2, 2,width_ratios=[1,1.5],height_ratios=[1,1])
This changes the relative size of plot gs[0] and gs[2] to gs1 and gs[3], whereas something like:
gs = gridspec.GridSpec(2, 2,width_ratios=[1,1],height_ratios=[1,2])
will change the relative sizes of plot gs[0] and gs1 to gs[2] and gs[3].
The following will tighten up the plots:
gs.update(hspace=0.01, wspace=0.01)
This gave me the following plot:
I also used the following to remove the axis labels where needed:
nullfmt = plt.NullFormatter()
ax.yaxis.set_major_formatter(nullfmt)
ax.xaxis.set_major_formatter(nullfmt)
I have a matrix I have plotted in matplotlib using self.ax.imshow(arr,cmap=plt.cm.Greys_r, interpolation = 'none') As you can see the plots are all the same color.
How can I make the plots, different colours and not just black
The correct link to the color maps is: http://www.loria.fr/~rougier/teaching/matplotlib/#colormaps
You assign the spring color map like this:
self.ax.imshow(arr, cmap = plt.cm.spring, interpolation = 'none').
#tcaswell is of course correct that if your data is binary the color will be binary as well. The color map gives different colors to different z-values. If you want to give the right bottom part a different color from the left bottom part (or whatever), you'll need a different solution. Something with a scatterplot I guess.