Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

python - matplotlib plot set x_ticks

How would I set the x_axis labels at the indices 1,2,3....n to be something different.

lam_beta = [(lam1,beta1),(lam1,beta2),(lam1,beta3),....(lam_n,beta_n)]
chunks = [chunk1,chunk2,...chunk_n]
ht_values_per_chunk = {chunk1:[val1,val2,...],chunk2:[val1,val2,val3,.....]...}
color='rgbycmk'
     j=0
     for chunk in chunks:
        plt.plot([hr_values_per_chunk[chunk][i] for i,item in enumerate(lam_beta)],[i for i,item in enumerate(lam_beta)],color=j%len(color))
        j+=1

     plt.set_xticks([i for i,item in enumerate(lam_beta)])
     plt.set_xticklabels([item for item in lam_beta],rotation='vertical')
     plt.show()

Error:

AttributeError: 'module' object has no attribute 'set_xticks'

Here I am unable to set the values of the lambda_beta tuple to be the values of each of the ticks on the x-axis as it say plt has no such method. How would I be able to achieve this for plt? I used xticks because this is how I had done it while generating a histogram in matplotlib. Any help would be appreciated. Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

set_xticks and set_xticklabels are axes methods, not functions in the plt module namespace. This is the meaning of the error message, 'module' object has no attribute 'set_xticks'.

Moreover,

[i for i,item in enumerate(lam_beta)]

can be simplified to

range(len(lam_beta))

and

[item for item in lam_beta]

can be simplified to

lam_beta

A convenient way to get your hands on the axes is to call plt.subplots:

So:

fig, ax = plt.subplots()
...
ax.set_xticks(range(len(lam_beta)))
ax.set_xticklabels(lam_beta, rotation='vertical')

ax is an Axes object. Calling Axes methods is the object-oriented approach to using matplotlib.


Alternatively, you could use the Matlab-style pylab interface by calling plt.xticks. If we define

loc = range(len(lam_beta))
labels = lam_beta

then

plt.xticks(loc, labels, rotation='vertical')

is equivalent to

fig, ax = plt.subplots()
ax.set_xticks(loc)
ax.set_xticklabels(labels, rotation='vertical')

plt.xticks sets the tick locations and labels to the current axes.


The list comprehension

[hr_values_per_chunk[chunk][i] for i,item in enumerate(lam_beta)]

could be simplified to

hr_values_per_chunk[chunk][:len(lam_beta)]

And you could eschew setting the color parameter for each call to ax.plot by using ax.set_color_cycle:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
lam_beta = [(lam1,beta1),(lam1,beta2),(lam1,beta3),....(lam_n,beta_n)]
chunks = [chunk1,chunk2,...chunk_n]
ht_values_per_chunk = {chunk1:[val1,val2,...],chunk2:[val1,val2,val3,.....]...}
color='rgbycmk'
ax.set_color_cycle(colors)

for chunk in chunks:
    vals = hr_values_per_chunk[chunk][:len(lam_beta)]
    ax.plot(vals, range(len(lam_beta)))

ax.set_xticks(range(len(lam_beta)))
ax.set_xticklabels(lam_beta, rotation='vertical')
plt.show()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...