2

I am trying to modify the mpl example with sharing x per column and y per row and I'd like to put the y tick labels to the right hand side. I tried a solution similar to this, i.e.

import matplotlib.pyplot as plt
import numpy as np

# Simple data to display in various forms
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)

plt.close('all')

# row and column sharing
f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex='col', sharey='row')
ax1.plot(x, y)
ax1.set_title('Sharing x per column, y per row')
ax2.scatter(x, y)
ax3.scatter(x, 2 * y ** 2 - 1, color='r')
ax4.plot(x, 2 * y ** 2 - 1, color='r')

#my "contribution" to the code:

for ax in [ax1,ax3]:
    ax.yaxis.set_ticks_position('both')
    ax.set_yticklabels([])
for ax in [ax2,ax4]:
    ax.yaxis.set_ticks_position('both')
    ax.yaxis.tick_right()#right hand side tickslabels

plt.show()

Which works for a single plot, but not here: no y tick labels at all. Suggestions? Same question for putting x-labels to the top.

If I only use the second loop I get the example output in the first column and at least right hand side y tick labels in the second column, but the left hand side ticks disappear in the second column. Why would that happen?

Community
  • 1
  • 1
mikuszefski
  • 3,943
  • 1
  • 25
  • 38
  • Please provide a complete minimal example. This will make it easier to helpp. – hitzg Feb 24 '15 at 15:12
  • @hitzg here you go, but as I linked above, it is just copy paste from the [matplotlib demos](http://matplotlib.org/examples/pylab_examples/subplots_demo.html) plus my 2 for-loops. – mikuszefski Feb 25 '15 at 13:44

1 Answers1

0

When you keep the first loop, the call to ax.set_yticklabels([]) kills all of your ticklabels, because when you share axes the tick label text becomes linked. The demo here using axes_grid1 demonstrates some ways to do it if you want different ticks/labels.

However, for this simple example, you can instead set the ticklabels specifically to the right, and pull the relevant other label objects individually to turn off their visibility. If you replace your modified section with the following:

for ax in [ax1,ax3]:
    for label in ax.get_yticklabels():
        label.set_visible(False)
for ax in [ax2,ax4]:
    # Change the labelling only
    ax.yaxis.set_tick_params(labelright='on', labelleft='off')

You get your tick labels on the right side. I'm not sure if this is the most efficient way, but it works.

example with ticks on the right side

Ajean
  • 5,528
  • 14
  • 46
  • 69
  • I see. However, you notice that you don't have ticks on the left hand side. You hide this a little by putting a grid. Why are the left hand side ticks disappearing? How to get them back? – mikuszefski Feb 26 '15 at 09:00
  • I'm sorry, I thought you *wanted* the left ticks to disappear. Why were you setting your ticklabels to null there if you wanted to keep them? Perhaps explain more precisely and I'll fix it up. – Ajean Feb 26 '15 at 14:17
  • Hi, to clarify, I distinguish beween `set_yticks()` and `set_yticklabels()`. I have used both of them in a simple plot where they do as I expect them to work, i.e.: The first on defines where the ticks are, the second which label they get. I want to remove the label only, or in this case keep all the ticks but put the labels right hand side. – mikuszefski Feb 26 '15 at 15:27
  • Maybe it is the *get linked* that I can make the label only vanish if I make the tick itself vanish, too? If so, I would almost call that a bug. – mikuszefski Feb 26 '15 at 15:31
  • 1
    Ah, I see. Sorry, I guess I hadn't read carefully enough. I've updated the answer. (Also the grid in the old picture was just because I use the 'bmh' style by default as I think it looks nicer, but for the update I reverted back to matplotlib defaults) – Ajean Feb 26 '15 at 15:46
  • Ok, that is the one! Whether or not a grid is a good idea, depends on the data I guess and whether it is for printing or projector presentation. I just meant that the presence of the grid almost made me miss the fact that the ticks are actually not there. I will play a little with this solution. Every time I run into problems like this, I learn something new. (Well not every time, I still have some open matplotlib questions). – mikuszefski Feb 27 '15 at 14:48