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