3

I am a new user in python and matplotlib so be gentle.

I have a plot with changed x and y labels so they correspond to chemical compounds.

However as make a 10x10 plot for instance (I think the limit is 8x8) not all of my labels are showing.

Of course I want that changed so that all the labels are present in a 8 x 12 plot for instance.

Below are the code that I am using so far just with random numbers.

import numpy as np
import matplotlib.pyplot as plt

HOST = ['RuO2', 'IrO2', 'TiO2', 'MnO2','SnO2','MoO2','NbO2','Dummy','Dummy','Dummy']

GUEST = ['Ru','Ir','Ti','Mn','Sn','Fe','Ni','Zn','Mg','D']

data = np.random.random((10,10))

fig = plt.figure()
ax = fig.add_subplot(111)
cax = ax.matshow(data)
fig.colorbar(cax)

ax.set_xticklabels(['']+HOST,rotation=90)
ax.set_yticklabels(['']+GUEST)

plt.show()

This will in time of course be modified to my data etc.

1

1 Answer 1

4

Have a look at the matplotlib.pyplot.xticks function, which sets or gets the x-axis tick locations and labels. If I call this

locs, labels = plt.xticks()

in your example code I get the the xticks are locatedat

>>> locs
[ -2.,   0.,   2.,   4.,   6.,   8.,  10.]

Noting that the two points -2 and 10 have empty labels, this corresponds to 5 x-axis tick locations, so you won't get all of your labels plotted since the number of x-axis tick labels exceeds the number of x-axis tick locations. Try replacing

ax.set_xticklabels(['']+HOST, rotation=90)

with

ax.xticks(range(len(HOST), HOST, rotation=90)

Here we are setting both the xtick locations and labels. Of course a similar replacement is required for the y-axis tick marks.

Sign up to request clarification or add additional context in comments.

2 Comments

Did not work as intended. First of all the rotation is not possible but that is unimportant. I now get a plot with labels for every second row and it is numbers not the strings that I have specified.
When I make the modification I suggest I get 10 labels on the x-axis with labels RuO2, IrO2, ... Dummy etc. Is not what you want? Also, what do you mean "rotation is not possible"? It works for me. Can you be more specific in your question. Perhaps posting screenshots of what you are trying to achieve.

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.