0

First Jupyter cell:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
%matplotlib notebook

plt.figure()
plt.plot((1,2,3))

labels = [item.get_text() for item in plt.gca().get_xticklabels()]
print("Labels are: {}".format(labels))

The ouput is (besides the chart):

Labels are: ['', '', '', '', '', '', '', '', '', '', '']

Now, in another cell, if I run the same code:

labels = [item.get_text() for item in plt.gca().get_xticklabels()]
print("Labels are: {}".format(labels))

The ouput is:

Labels are: ['', '0.00', '0.25', '0.50', '0.75', '1.00', '1.25', '1.50', '1.75', '2.00', '']

Why the same code, referencing the same Axes, returns different results?

3
  • Probably the plt object has changed when the plot is rendered. Some internal magic in matplotlib, perhaps. Commented Jul 3, 2017 at 20:04
  • Thanks, Haken, but it's not that. I checked the Axes ID with id(plt.gca()) and it is the same in both cells. Commented Jul 3, 2017 at 20:41
  • I was thinking about the internal state of the object. Matplotlib has many quirks. Commented Jul 3, 2017 at 22:46

2 Answers 2

0

It's an interesting problem, as far as I can tell it seems to be that while you are referencing the same object in both lots of code the actual plotting that jupyter is doing isn't side-effect free.

The plot itself is generated when the code in the cell has finished running (at least with the notebook back-end), and during the generation the details such as the xtick-labels are populated. Naïvely I assume that this is because you can add additional things to the plots that will change the x and y limits of the figure and it's more efficient to only do that once when we need to see it.

Note that you can manually set the xtick-labels prior to the generation of the plot if you know what they are going to be (or what you want them to be) ahead of time:

plt.gca().set_xticklabels(list_of_labels)

However, if you do actually need the label values prior to the plot being generated you can always use

plt.gca().get_xticks()

This will return a list of the locations of the ticks and from there it's pretty easy to make the labels from that

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
%matplotlib notebook

# generate the figure and axes objects
plt.figure()
plt.plot((1, 2, 3))  # plot the data

# now we can look at the xtick positions, and infer the labels
labels = ["{:.2f}".format(item) for item in plt.gca().get_xticks()]
print("Labels are: {}".format(labels))
Sign up to request clarification or add additional context in comments.

Comments

0

instead of:

%matplotlib notebook

Use:

%matplotlib inline

1 Comment

I actually want to use the nbAgg backend (%matplotlib notebook) to refer to the same figure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.