New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Colorbar label position different when executing a block of code #6471
Comments
Some further digging: def mycolorbar(ax,cbar):
fig = ax.get_figure()
cax = cbar.ax
inv = ax.transAxes.inverted()
R = fig.canvas.get_renderer()
caxY = cax.yaxis
ylab = caxY.get_label()
ticklabelbbox = inv.transform( caxY.get_ticklabel_extents(R)[1].get_points() )
ylab.set_verticalalignment('center')
ylab.set_horizontalalignment('center')
ylab.set_rotation(0)
xpos, ypos = np.max(ticklabelbbox[:,0]), (3/4)
caxY.set_label_coords( xpos , ypos , transform = ax.transAxes )
return xpos |
The source of the problem is that you are capturing screen-space position which are figure size / dpi dependent. When the dpi changes (and the default save dpi is currently different than the default screen dpi) your coordinates do not get updated and are wrong. What exactly are you trying to do that the provide colorbar label does not do? You probably should be using |
Thanks for the response.
If you were to download and import import matplotlib.pyplot as plt
import numpy as np
import figfun as f
x,y,z = [np.random.rand(1000) for i in range(3)]
fig = plt.figure(facecolor='w')
ax = fig.add_axes([.1,.1,.8,.8])
plt.tricontourf(x,y,z)
plt.xlabel('xlab')
plt.ylabel('ylab')
cbar = plt.colorbar(label='clab')
f.myax( fig )
f.colorbar( ax, cbar)
plt.show() |
Without looking at this in detail, I suspect the problem is related to your use of the colorbar convenience functionality: its ability to resize an Axes and then put itself in the liberated space. This is great for quick plotting and interactive use, but for any finely-tuned plots for publication, it is often better to explicitly size and position all of your Axes, including one that you make to hold the colorbar (via the cax kwarg). def axes_inches(fig, rect, **kw):
"""
Wrapper for Figure.add_axes in which *rect* is given in inches.
The translation to normalized coordinates is done immediately
based on the present figsize.
*rect* is left, bottom, width, height in inches
*kw* are passed to Figure.add_axes
"""
fw = fig.get_figwidth()
fh = fig.get_figheight()
l, b, w, h = rect
relrect = [l / fw, b / fh, w / fw, h / fh]
ax = fig.add_axes(relrect, **kw)
return ax |
On 2016/05/26 8:06 AM, Russle wrote:
Annotate can make FancyArrow instances. See |
I think what you want to do there is set the |
Sincerely appreciate everyone's feedback!
Does that mean you were able to duplicate what I was seeing? I was under the impression that the @QuLogic fig2 = plt.figure(2)
ax2 = fig2.add_subplot(111)
ylab = plt.ylabel('ylabel')
ylab.set_rotation(0)
ylab.set_va('bottom')
ylab.set_ha('right')
ylab.set_y(3/4)
plt.draw() But what I couldn't make sense of after this was that Ultimately I thought that the most robust (thought perhaps not the smartest or most convenient) method for positioning all these different pieces was to just find their bboxes and work within one coordinate system. In any case, I thank you all for the assistance and consideration. I moved all of my research codes over to Python about a year ago, and working with Matplotlib is awesome. |
On 2016/05/26 5:30 PM, Russle wrote:
No, it meant I was lazy and just thought about how things might go
You are correct. I was missing the distinction between FancyArrow and |
I'm closing this, as the discussion has died down and I don't think it leads to a clear specification of something we can fix in matplotlib. If this is incorrect--if there is a bug, or a hole in the documentation, or a needed feature--please open a new issue or (better) a PR with a fix. Or, if I have misunderstood the situation, reopen the present issue. |
Matplotlib version 1.5.1
Python 3.4.4 on Windows via Anaconda 2.2.0 (64-bit)
Also occured on a Python 3.5.1 install on Lubuntu via Anaconda 4.0
Jupyter QtConsole 4.1.0; IPython verion 4.0.0
I made some functions for positioning and formatting x and y labels and to draw arrows pointing towards these labels. Today I wrote a function that similarly formats the colorbar label. However, the behavior is erroneous if the function is called within a block of code or in a script executed on the command line. I defined a condensed version of the function here as
mycolorbar()
.If I run the following in a script from the command line, or if I paste everything into a single code block in the Jupyter QtConsole, I get the figure shown below the code. In the QtConsole this occurs regardless of whether I've called
%matplotlib
Observe how far out to the right the colorbar label are. This is "incorrect".
Now, when working interactively in the QtConsole, if I run all of the above code _except_ the last line, let the figure window render, then execute that last line
mycolorbar( ax, cbar)
on its own, I get the "correct" position for the colorbar label.I'm asking anyone to debug my code. Just trying to bring a potential issue to light. Thank you.
The text was updated successfully, but these errors were encountered: