简体   繁体   English

仅显示x轴图的第一个和最后一个刻度标签

[英]Show only first and last ticks label of x-axis plot

I'm parsing a log file and creating a plot. 我正在解析日志文件并创建图。

I don't need all labels on X axis. 我不需要X轴上的所有标签。 I want to display only a first one and a last one or a few of them with particular step let's say every 100. 我只想显示第一个,最后一个或几个,并按特定步骤显示,例如每100个。

How I can do this? 我该怎么做? I can display only first one or only last one but not both together. 我只能显示第一个或最后一个,但不能一起显示。

My code: 我的代码:

import numpy as np
import pylab as pl
import matplotlib.pyplot as plt

with open('file.log') as f:
    lines = f.readlines()
    x = [int(line.split(',')[0]) for line in lines]
    my_xticks = [line.split(',')[1] for line in lines]
    y = [int(line.split(',')[2]) for line in lines]
    z = [int(line.split(',')[3]) for line in lines]

plt.xticks(x, my_xticks[0], visible=True, rotation="horizontal")
plt.xticks(x, my_xticks[-1], visible=True, rotation="horizontal")

plt.plot (x,z)
plt.plot (x,z)
plt.plot(x, y)


plt.show()

Thank you! 谢谢!

with x-ticks, you can provide a list. 带有X-ticks,您可以提供一个列表。 So you can do: 因此,您可以执行以下操作:

plt.xticks([my_xticks[0], my_xticks[-1]], visible=True, rotation="horizontal")

Incidentally, you can get the original ticks using: 顺便说一句,您可以使用以下方法获得原始刻度:

my_xticks = ax.get_xticks()

where ax is your your Axes instance. 其中, ax是您的Axes实例。 You can even supply your own values: 您甚至可以提供自己的值:

plt.xticks(
          [my_xticks[0], my_xticks[-1]], 
          ['{:.2}'.format(my_xticks[0]), '{:.2}'.format(my_xticks[-1])]
           visible=True, rotation="horizontal")

etc. You can see how easily this can be generalized ... 等等。您可以看到将其概括的难易程度...

Just remember that the tick labels refer to specific Axes within the figure. 只要记住刻度线标签是指图中的特定Axes So ideally you should do: 因此,理想情况下,您应该这样做:

`ax.set_xticks(..)`

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

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