Setting matplotlib ticks - matplotlib

I'm having some issues in setting the ticks in my plots using matplotlib. What I need is to set the ticks inwards, so inside the figure (but the labels must stay outside), and at the same time I need to have ticks on all four boundaries. Do you know a simple way to do this?
Thanks in advance!

Just because it can be useful for other people, I post here the simple solution I found after looking for some time in the documentation, as suggested by cfort:
ax.xaxis.set_ticks_position('both')
ax.xaxis.set_tick_params(direction='in', which='both', labelsize=16)
ax.yaxis.set_ticks_position('both')
ax.yaxis.set_tick_params(direction='in', which='both', labelsize=16)
ax.minorticks_on()
these lines of code set the tick inwards on both sides (e.g. for the x axis they are placed at top and bottom of the figure), set the label size and add the minorticks, which will point in the inward direction as well.

Related

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)

How to control the specific size of plot in matplotlib?

Let us suppose that I am plotting a few plots with pyplot/matplotlib. Now, the first has to have tick marks and tick labels, and only the first. The last has to have a colorbar and some marks for scale. If I do a script specifying the figure size, the plot proper in the last and first plots is drawn with smaller sizes, as the figure has to make room for the extra markings. And I seem to be not able to control that, in an automatic way, like making the other plots at the same scale inside a larger figure or something like that.
Example code (it looks a little non-pythonic because I am using PyPlot inside Julia):
using PyPlot
SomeData=randn(64,64,3)
for t=1:3
figure(figsize=(3.0,3.0))
imagen=imshow(SomeData[:,:,t], origin="lower")
if t!=3
xticks([])
yticks([])
else
tick_params(labelsize=8, direction="out")
end
if t==1
cbx=colorbar(imagen, fraction=0.045, ticks=[])
cbx[:set_label]("Some proper English Label", fontsize=8)
end
savefig("CSD-$t.svg",dpi=92)
end
Thanks in advance-

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.

How can I get and set the position of a draggable legend in matplotlib

I'm trying to get and set the position of a draggable legend in matplotlib. My application consists of an interactive GUI, which has a redraw/plot function that should perform the follow steps:
save the position of the current legend.
clear the current axes and perform various plotting operations, which may or may add labels to their plots.
build a new draggable legend (ax.legend().draggable()) and restore the old position of the legend.
In between these steps the user is free to drag the legend around, and the goal is to persist the legend position when the plots are redrawn.
My first approach was to use oldpos = legend.get_bbox_to_anchor() and legend.set_bbox_to_anchor(oldpos) in steps 1 and 3. However this causes to move the legend completely off the visible area.
Note that I have to use ax.legend() and cannot use fig.legend(lines, labels), since step 2 is completely decoupled, i.e., I don't know anything about lines and labels in step 3. According to answers to the question How to position and align a matplotlib figure legend? there seems to be a difference between these two possibilities regarding axes or figure coordinates. Obviously my problem calls for figure coordinates, but I haven't fully understood how to convert the bbox to a "bbox in figure coordinates".
The even more severe problem I just realized is that apparently legend.get_bbox_to_anchor() always seems to return the same values irrespective of the drag position. So maybe the anchor can only be (ab-)used to manipulate the position of static legends? Is there another/proper way to save and restore the position of a draggable legend?
By looking at the implementation of Legend I found out that there is an undocumented property _loc, which exactly does what I want. My solution now looks astonishingly simple:
oldLegPos = ax.get_legend()._loc
# perform all plotting operations...
legend = ax.legend().draggable()
legend._loc = oldLegPos
It looks like _loc automatically stores figure coordinates, since I do not have to convert the coordinates in any way (eg. when the plotting operations completely change the axes ranges/coordinates).

Weird graphics in matplotlib when changing the scale

I get a histogram picture in matplotlib which looks great. Now I realize I need a log scale on the y-axis, so I just add to the code:
ax.set_yscale('log')
but then, the histogram bars dissapear and I only get some sparse points, do you know waht could be the reason?
Thanks
Use hist's log=True keyword argument instead. This is a FAQ in matplotlib-user list :)