0

I am trying to manually change the axis labels for a custom matplotlib plot. Basically, each label on the figure is an integer (-1, 0, 1, 2...) and I want to format them to each be 10 to the power of that integer.

What I've tried so far is:

newLabel = [r'$10^{0}$'.format(item.get_text()) for item in axes[ax].get_xticklabels()]

The curly braces are part of the latex formatting, but they also seem to get picked up by the .format function and I think this may be the problem. I tried multiple nested braces and it didn't seem to work. It breaks the whole plot and throws a bunch of consecutive errors, the last of which is a ValueError: 10^. I also tried to format the string by concatenating it to see if I can stop the suspected clash:

newLabel = [r'$10^{'+item.get_text()+'}$' for item in axes[ax].get_xticklabels()]

But it resulted in the same problem.

What would be the proper way to do this?

Edit: Here is the code I am using to test for the labels being populated. Note that this works if I just run it in python interactive with i = 0 (Pardon the messiness).

ax=0
# yvar is a list of integers.
for i in yvar:
    hist00, ext00, asp00 = histogram(np.log10(df[spn[xvar]]), np.log10(df[spn[i]]), 100, False) # My custom histogram function.
    axes[ax].imshow(hist00, norm = matplotlib.colors.LogNorm(), extent = ext00, aspect = asp00) # Inputting the resulting data as an image. This all works as far as I know.

    plt.draw()
    
    for item in axes[ax].get_xticklabels():
        print(item.get_text()) #Ideally this should show the string content of the labels, but since the labels are not populated, it only returns a blank string.

    axes[ax].set_xlabel(spl[xvar]+' (Log $cm^{-3}$)')
    axes[ax].set_ylabel(spl[i]+ '(Log $cm^{-3}$)')
    # This is just for labels.
    ax +=1
syzygy350
  • 23
  • 5
  • Does this answer your question? [Change x axes scale in matplotlib](https://stackoverflow.com/questions/11577665/change-x-axes-scale-in-matplotlib) – sehan2 Oct 18 '21 at 06:33
  • I don't think so. I'm creating a custom plot (2d histogram, but normalized in a particular way) and showing it with imshow(). So to make each bucket in the histogram a reasonable size, I took the log of the data along the necessary variables first. So the data itself already is the log of the previous values. So I need to make a custom label to make it show what the label would be for the un-logged data. The simplest way I've seen so far would be to change the array of xticklabels that matplotlib can give me. – syzygy350 Oct 18 '21 at 12:31
  • The label Text objects of `get_xticklabels` aren't populated until some sort of `draw` method is called ([docs](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.get_xticklabels.html)), so you're formatting to `r'$10^$'`. The docs [warn](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.set_xticklabels.html) against setting the xtick labels without explictly setting the positions anyway, so I'd set the xtick positions with `Axes.set_xticks` and *then* set the labels to whatever you want using `Axes.set_xticklabels`. – AJ Biffl Oct 19 '21 at 05:08
  • Also, you're right about `format` eating up the curly braces - you will need to double up to get both format and Latex working – AJ Biffl Oct 19 '21 at 05:08
  • Alright, I am even more confused now. You were right about the labels being empty until the draw method is called. However, in my code, the labels seem to remain empty even though I call pyplot.draw() for each plot. HOWEVER, if I run the code in python interactive by copy and pasting it bit by bit, it suddenly works if I do not loop over the code (I loop over this as it is a figure with 3 plots). TBH, I can't figure out what the problem is here. I tried rewriting it to see if some weird syntax was making a difference, but it doesn't look like it. I'll ad the code I was using to test it above. – syzygy350 Oct 21 '21 at 05:48
  • I found pretty finnicky behavior myself when I tried calling `draw` in various ways... only way I got populated labels was after the script was done, and I called `get_xticklabels` from the shell where I ran the script... I think the best thing is to just set the labels yourself – AJ Biffl Oct 26 '21 at 00:41

0 Answers0