Python matplotlib 绘图集 x_ticks

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20335290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:14:05  来源:igfitidea点击:

matplotlib plot set x_ticks

pythonmatplotliblinegraph

提问by anonuser0428

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

我将如何将索引 1,2,3....n 处的 x_axis 标签设置为不同的东西。

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'

AttributeError: 'module' 对象没有属性 '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!

在这里,我无法将 lambda_beta 元组的值设置为 x 轴上每个刻度的值,因为它说 plt 没有这样的方法。我如何才能为 plt 实现这一目标?我使用 xticks 是因为这是我在 matplotlib 中生成直方图时的做法。任何帮助,将不胜感激。提前致谢!

回答by unutbu

set_xticksand set_xticklabelsare axes methods, not functions in the pltmodule namespace. This is the meaning of the error message, 'module' object has no attribute 'set_xticks'.

set_xticksset_xticklabels是轴方法,而不是plt模块命名空间中的函数。这就是错误消息的含义,'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 axesis to call plt.subplots:

一个方便的方法axes是调用 plt.subplots

So:

所以:

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

axis an Axesobject. Calling Axesmethods is the object-oriented approach to using matplotlib.

ax是一个Axes对象。调用Axes方法是使用 matplotlib 的面向对象的方法。



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

或者,您可以pylab通过调用plt.xticks. 如果我们定义

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.xtickssets the tick locations and labels to the current axes.

plt.xticks将刻度位置和标签设置为当前轴。



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 colorparameter for each call to ax.plotby using ax.set_color_cycle:

可以避免使用ax.set_color_cyclecolor为每个调用设置参数:ax.plot

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()