1

I'm running into a weird problem with matplotlib. Here is my code:

f,a=subplots(3,1,sharex='col')
f.set_figheight(3.)
f.set_figwidth(3.)
## Make plots, set labels for a[0], a[1], a[2]
a[2].set_xlim(-4.40,6)

[plt.setp(i.get_xticklabels(),fontsize=9) for i in a]
[plt.setp(i.get_yticklabels(),fontsize=9) for i in a]
[i.set_yscale('log') for i in a]
[i.set_ylim(1e-4,1.) for i in a]

for i in a:
##The following part is problematic
   labels=[j.get_text() for j in i.get_yticklabels()]
## end problematic part
   print labels
   labels[1] = u''; i.set_yticklabels(labels)

f.subplots_adjust(hspace=0)
plt.show()

The problem is that part of for loop that gets yticklabels works fine if I run it in the shell after making the plot but it returns an empty list if I run it as part of the above script.

If I run the code within ipython using:

#Code run inside IPython shell
run -i 'myscript.py'

I get the following output:

['', '', '', '', '', '', '']
['', '', '', '', '', '', '']
['', '', '', '', '', '', '']

This is not what I want. However, when I comment out the label modification in the script and run the following:

# Code run inside IPython shell
run -i 'myscript.py'
for i in a:
   labels=[j.get_text() for j in i.get_yticklabels()]
   print labels
   labels[1] = u''; i.set_yticklabels(labels)

I get the following output:

['', '$\\mathdefault{10^{-4}}$', '$\\mathdefault{10^{-3}}$', '$\\mathdefault{10^{-2}}$', '$\\mathdefault{10^{-1}}$', '$\\mathdefault{10^{0}}$', '']
['', '$\\mathdefault{10^{-4}}$', '$\\mathdefault{10^{-3}}$', '$\\mathdefault{10^{-2}}$', '$\\mathdefault{10^{-1}}$', '$\\mathdefault{10^{0}}$', '']
['', '$\\mathdefault{10^{-4}}$', '$\\mathdefault{10^{-3}}$', '$\\mathdefault{10^{-2}}$', '$\\mathdefault{10^{-1}}$', '$\\mathdefault{10^{0}}$', '']

which is the output I expect. I have no idea what might be happening here. Any help will be greatly appreciated. Also, is there a "pythonic" way of writing the for loop?

Thanks

NichtJens
  • 1,709
  • 19
  • 27
toylas
  • 437
  • 2
  • 6
  • 19
  • Could you explain what "run" is, that you are using? Is that supposed to be `ipython -i`? – NichtJens Jun 23 '16 at 20:10
  • I run the script interactively within the IPython shell this way: ---- In [1]: run -i 'myscript.py' – toylas Jun 23 '16 at 20:41
  • I see. Have you tried running it via the default interpreter? I guess this will produce the same result, but then you'd know it wasn't ipython doing weird things... – NichtJens Jun 23 '16 at 20:43
  • Possible duplicate of [Modify tick label text](http://stackoverflow.com/questions/11244514/modify-tick-label-text) – NichtJens Jul 07 '16 at 22:19

1 Answers1

3

Actually, the for-loop is more "pythonic" than your misuse of list comprehension. See also here.

I think you should move all those into the for-loop... That way you have a single outer loop. In your code, you are doing the same loop five times.

As the Zen of Python says:

Readability counts.


For your actual question: You are running into this problem?

Your version of Matplotlib is too new (matplotlib.__version__ = 1.3.1 as per your comment). Hence, you cannot use the code in the accepted answer there, as per the first paragraph of that answer.

There, an answer that should work for newer version of matplotlib is given (but not accepted), too. The main trick is using axes.get_xticks().tolist() instead of axes.get_xticklabels() ...

Community
  • 1
  • 1
NichtJens
  • 1,709
  • 19
  • 27
  • That is exactly what I get if I run the label editing part within the script. The axis range goes from 1e-4 to 1. and I want the ylabels to be from 1e-3 to 1. pylab automatically puts in all and I want to remove 1e-4. Please see my edit to see what is expected. – toylas Jun 23 '16 at 20:43
  • What version of matplotlib are you using? – NichtJens Jun 23 '16 at 20:45
  • matplotlib.__version__ = 1.3.1 and matplotlib.__version__numpy = 1.5 – toylas Jun 23 '16 at 20:48
  • Well then my guess is spot-on! Please read the answer to the [linked question](http://stackoverflow.com/a/11250884/655404) – NichtJens Jun 23 '16 at 20:50
  • Not exactly. My code actually does exactly what is suggested in that post (I belive that is where I might have learnt the trick from). However, in this particular case, I run into this weird problem that the code does not work as part of the script but does work outside the script. – toylas Jun 23 '16 at 20:53
  • Exactly! Read the first paragraph. Your version is too new for this to work! And you don't see weird behavior at all ... it's perfectly expected as by that answer. And, luckily, the question even has the solution [here](http://stackoverflow.com/a/18946103/655404)... – NichtJens Jun 23 '16 at 20:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/115451/discussion-between-toylas-and-nichtjens). – toylas Jun 23 '16 at 20:59