繁体   English   中英

matplotlib:更改轴

[英]matplotlib: change axes

我有每分钟进行的一系列测量m。 我只是说说这些测量值与时间的关系

import pylab as pl
pl.plot(range(len(m)), m)

这在x轴上给了我分钟(只是因为我以分钟为单位,而range(len(m))为我提供了整数)。 如何快速将X轴的标签更改为小时? 我基本上需要使用标签mod 60,但我只希望使用整数小时值。

简而言之,我想在x轴上重新标记60的每一个倍数。

对不起,而不是xlabels,您需要创建一个轴并使用ax.set_xticklabels:

from matplotlib.pyplot import figure
fig = figure()
ax = fig.add_subplot(111)


def mk_labels(vals):
    labels = []
    for i in vals:
        if i % 60 == 0:
            labels.append("Some new special label")
        else:
            labels.append(i)


 ax.set_xticklabels(mk_labels(range(len(m))))
 ax.plot(range(len(m)), m)

或者简单地:

 ax.set_xticklabels(["{0}h".format(i) if i % 60 == 0 else i for i in range(len(m))])

如果您需要更复杂的格式,则这两种方法都可以使用第一种可能更容易。

pylab.plot(data) # pylab will automatically add an x-axis from 0 to len(data) - 1

# first argument is when ticks should appear on the x-axis
# the second argument is what the label for each tick should be
# same as -> pylab.xticks([0, 60, 120...], [0, 1, 2...])
pylab.xticks(range(0, len(data), 60), range(len(data)/60))

# let the reader know the units
pylab.xlabel("hours")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM