matplotlib图集x_ticks [英] matplotlib plot set x_ticks
问题描述
如何将索引1,2,3 .... n的x_axis标签设置为不同的内容.
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()
错误:
AttributeError:模块"对象没有属性"set_xticks"
AttributeError: 'module' object has no attribute 'set_xticks'
在这里,我无法将lambda_beta元组的值设置为x轴上每个刻度的值,因为它说plt没有这种方法.我将如何实现这一目标?我使用xticks是因为这是我在matplotlib中生成直方图时所做的.任何帮助,将不胜感激.提前致谢!
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!
推荐答案
set_xticks 和 set_xticklabels 是轴方法,而不是函数在plt
模块名称空间中.这就是错误消息'module' object has no attribute 'set_xticks'
的含义.
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'
.
此外
[i for i,item in enumerate(lam_beta)]
可以简化为
range(len(lam_beta))
和
[item for item in lam_beta]
可以简化为
lam_beta
方便使用axes
的方法是致电
plt.subplots :
A convenient way to get your hands on the axes
is to call
plt.subplots:
所以:
fig, ax = plt.subplots()
...
ax.set_xticks(range(len(lam_beta)))
ax.set_xticklabels(lam_beta, rotation='vertical')
ax
是Axes
对象.调用Axes
方法是使用matplotlib的面向对象方法.
ax
is an Axes
object. Calling Axes
methods is the object-oriented approach to using matplotlib.
或者,您可以通过调用 plt.xticks
.如果我们定义
Alternatively, you could use the Matlab-style pylab
interface by calling plt.xticks
. If we define
loc = range(len(lam_beta))
labels = lam_beta
然后
plt.xticks(loc, labels, rotation='vertical')
等同于
fig, ax = plt.subplots()
ax.set_xticks(loc)
ax.set_xticklabels(labels, rotation='vertical')
plt.xticks
将刻度位置和标签设置为当前轴.
plt.xticks
sets the tick locations and labels to the current axes.
列表理解
[hr_values_per_chunk[chunk][i] for i,item in enumerate(lam_beta)]
可以简化为
hr_values_per_chunk[chunk][:len(lam_beta)]
并且您可以避免使用ax.plot的调用设置color
参数. ="noreferrer"> ax.set_color_cycle :
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()
这篇关于matplotlib图集x_ticks的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!