Matplotlib tick labels - matplotlib

Is there a way to render the tick labels just right inside the axes, i.e, something like the direction property there is on the ticks themself?
Right now I'm setting the x property to a positive value on the ticklabels to draw them inside of the axis, i.e.,
ax2.set_yticklabels(['0', '2500', '5000', '7500'], minor=False, x=0.05)
But this doesn't really work on resizable plots, as the 0.05 figure is absolute (and too big on big plots).
Any ideas?

I'm assuming that ax2 is constructed as ax2 = ax.twinx(), which is to say that it is on the right side of the axes.
You could do something like the following:
ax2.set_yticklabels(['0', '2500', '5000', '7500'], minor=False, horizontalalignment='right')
for tick in ax2.yaxis.get_major_ticks():
tick.set_pad(-8)
If you want the left side axis on the inside too, then you'd simply switch the horizontal alignment to 'left' and change the pad from -8 to -25.
The two numbers might not be exact and could depend on other matplotlib settings you might have (e.g. length of major ticks) so you may want to increase or decrease those values slightly.

Related

Does changing the y-axis and x-axis label changes anything in the image using imshow?

I want to show values as pixels. But my x-axis and y-axis show different range.I don't know what these values are telling us. But I read somewhere that we have to read the values as intensity values. They will not be visualized on the axis. Then what these axis are telling us? Secondly if I use xticks and yticks to label x and y axis does it has any effect on the image? Does it flip anything in the image?
`# a is an array of shape(500,2)
plt.xticks(np.arange(0,2))
plt.yticks(np.arange(0,500))
plt.imshow(a,aspect='auto',cmap=plt.cm.gray)`

matplotlib xticks misplaced after using ax.set_xlim()

When I set xlim with ax.set_xlim() my xtick labels are all shifted by one space to the left.
fig,axes=plt.subplots(2,1,sharey=True)
x=list(range(0,9))
y=list(range(1,10))
df=pd.DataFrame({'x':x,'y':y})
ax1=df.plot('x','y',ax=axes[0])
xticklabels=x
ax1.set_xticklabels(x)
after I add this line to the code
ax1.set_xlim(-0.2,8.2)
xticks are wrongly placed:
While you set the ticklabels to be the elements of a list, you do not specify the actual tick positions. So you leave it to the automatic AutoLocator to place the tick positions, but then set some custom labels to those ticks.
This will in general not give reasonable results.
As a rule of thumb: If you fix the labels, you need to fix the positions as well.
ax.set_xticks(x)
ax.set_xticklabels(x)

Matplotlib: How to place x-axis and y-axis TITLES at edges of frame, even if origin is not at bottom left?

I'm making plots where the (0,0) position is not necessarily in the bottom left corner of the plot frame, meaning that the x and y axes and their ticks cross within the plot frame. It's fine to have the lines, ticks and their values within the frame, but having the axis titles so near to their lines interferes with visualization of certain data points.
I'd simply like to force the x and y axis titles to be at the bottom and far left of the plot frame, respectively; it doesn't work to simply 'pad' the xlabel or ylabel titles because the padding I'd need would vary between plots.
How to get the titles always in these consistent locations of the plot frame, even if the corresponding axis lines, ticks/values may vary in the plot frame space?
Thank you.
I'd like to add a note here. It seemed to me that it could be possible to define the xlabel and ylabel 'labelpad' values directly from the data range:
For example, one could find the minimum x-value of the minimum y-value (this would set the position of the x-axis label), and the minimum x-value of the minimum x-value (this would set the position of the y-axis label):
xpad = ax.get_ylim()[0]
ypad = ax.get_xlim()[0]
Then use these values in xlabel and ylabel parameters as labelpads:
pylab.xlabel("X Title", fontsize=12, labelpad=abs(xpad))
pylab.ylabel("Y Title", fontsize=12, labelpad=abs(ypad))
Unfortunately, it doesn't look like labelpad can accept variables. Any suggestions that might allow a work-around?

Seaborn Heatmap Colorbar Location

The cbar_kws argument of seaborn.heatmap accepts the parameters that fig.colobar accepts.
Is there a way to adjust the placement of the colorbar, simply to adjust the location to the left (especially when the correlation matrix is adjusted to have only a lower triangle).
I can adjust the labels by overriding the tick labels. As of now I still have to adjust the upper-right borders in post-processing, but it would make things much easier if I didn't have to edit the color bar as well.
heatmap accepts a cbar_ax argument; if you want to specify the position of the colorbar, the best thing to do is to set up the figure how you want it and then pass the specific axes.
You can also move axes around after plotting through normal matplotlib commands.

Change y_axis to begin 0 on top

By default, when I add axes to an image in matplotlib, the x axis begins at 0 and increases from left to right and the y-axis begins at 0, increasing from bottom to top. I would like to have the y-axis beginning at zero, but from top to bottom (that is, 0 on the top, and the maximum value on the bottom) How could I accomplish this?
If I understand correctly, you're asking how to reverse the y-axis. This can be done with
plt.gca().invert_yaxis()
which takes the current axis plt.gca() and calls its method invert_yaxis() to invert the y-axis.
You can also simply call plt.ylim() and put the coordinates in reverse order. I know I always fine-tune the range of all plots by hand anyway, so this is easier in that situation. So let's say you have a plot that runs from 0 to 10, you would just call
plt.ylim(10,0)
and it will flip the y-axis.