1

I want to change the location of the axes spines of a matplotlib plot (e.g., 'left') to a specific data value. I am aware that I can use matplotlib.spines.set_position() to this end, and on a linear plot this works fine. However, on a log (or, semi-log) plot the axis is not showing for some reason. The following code and screenshot demonstrate the problem: demo spine position on log plot

# test axis location on log plot
from matplotlib import pyplot as plt

figure = plt.figure()
axes = figure.add_subplot(121)
handle, = axes.plot([0.2, 1], [1, 2], 'ro')
axes.xaxis.set_label_text('X')
axes.yaxis.set_label_text('Y')
axes.set_xscale('log')
axes.set_xlim(0.02, 2)
axes.spines['left'].set_position(('data', 0.1))
axes.spines['left'].set_linewidth(3)
axes.set_title('log')

axes2 = figure.add_subplot(122)
handle2, = axes2.plot([0.2, 1], [1, 2], 'ro')
axes2.xaxis.set_label_text('X')
axes2.yaxis.set_label_text('Y')
axes2.set_xlim(0, 2)
axes2.spines['left'].set_position(('data', 0.5))
axes2.spines['left'].set_linewidth(3)
axes2.set_title('linear')

plt.show()

In the left, semi-log plot, the 'left' spine is no longer showing at all, whereas it does show up at the correct location in the right-hand, linear plot. As a workaround, I can convert the data coordinates to axes coordinates and then use set_position(('axes', xxx)), where xxx refers to the converted coordinate. However, to allow dynamic zooming and rescaling of the plot, I'd much rather set the spine to a specific data value, rather than an axis value.

I am using matplotlib v 3.0.2 and Python 3.6 on a Windows platform. This question appears related to (yet distinct from): Positions of spines in matplotlib Setting spines in matplotlibrc

  • The spine is at position x0=-112399 in pixel coordinates, so it's way outside the figure. This looks like a bug in the spine transform setup. You can open an issue at https://github.com/matplotlib/matplotlib/issues – ImportanceOfBeingErnest May 05 '19 at 22:41
  • @ImportanceOfBeingErnest: thanks a lot for the swift and helpful reply! I kind of suspected that it might be a bug but was not sure. I guess I will wait a couple of days to see whether there are any more comments, and then open an issue on github, as you suggested. On a related note, how did you figure out the pixel coordinates of the spine? (this may be helpful to know in the future) – The Angulion May 06 '19 at 05:06
  • Not sure what other comments you expect; the sooner you open the issue, the sooner it can potentially be fixed. The position in pixel space can be found via `fig.canvas.draw(); artist.get_window_extent()`. – ImportanceOfBeingErnest May 06 '19 at 12:23
  • Thanks again, and you are right in that this appears to be a bug that needs fixing. I just opened an issue on github as advised: https://github.com/matplotlib/matplotlib/issues/14141 – The Angulion May 06 '19 at 13:14

0 Answers0