简体   繁体   English

Python x标签平均分布

[英]Python x-labels evenly spreaded

I am trying to get my labels turned up correctly. 我正在尝试正确设置标签。 I want it to show 10 values on the list, but I have no idea how. 我希望它在列表上显示10个值,但我不知道如何。 The usual way to show it doesn't work and when there are a lot of values, it is hard to read, see the picture. 显示它的通常方法不起作用,并且当有很多值时,很难看清图片。 在此处输入图片说明

Here you see that it is impossible to read the data. 在这里您将看到无法读取数据。 Do you have an idea to make it work properly? 您有想法使其正常工作吗?

I have tried autoDateLocator, but that didn't work. 我已经尝试过autoDateLocator,但是没有用。 The axis values were wrong. 轴值错误。 I also tried to do that manually, but the same result happened. 我也尝试手动执行此操作,但是发生了相同的结果。

Thanks in advance! 提前致谢!

I have kinda 'solved this issue'. 我有点“解决了这个问题”。 I asked around and didn't really find an answer that suited my needs, as every tick always gets shown. 我四处询问,但并没有真正找到适合我需要的答案,因为每次打勾总是显示出来。 However, when I keep the xtick_labels in the datetime-format, it gets sorted out by matplotlib itself, if there are too many values to show. 但是,当我将xtick_labels保留为datetime格式时,如果要显示的值太多,它将由matplotlib本身进行排序。 This according to the dataset Armamatita provided 这根据Armamatita提供的数据集

import matplotlib.pyplot as plt
import datetime

import numpy as np

x = np.array([datetime.datetime(i,1,1) for i in range(1700,2017)])
y = np.random.randint(0,100,len(x))
fig, ax = plt.subplots()
ax.plot(x,y)

plt.show()

I when the days I want to see is more than 14, I just let matplotlib pick te xtick_labels. 当我想看到的日子超过14天时,我只是让matplotlib选择xtick_labels。 When equal or less than 14, I add it myself using: 当等于或小于14时,我自己使用以下方法添加:

alldays = DayLocator() weekFormatter = DateFormatter('%a %b %d %Y')

and this right before returning the fig: 这是在返回无花果之前的权利:

ax.xaxis.set_major_locator(alldays) ax.xaxis.set_major_formatter(weekFormatter)

It isn't the most neat pythonic code, but it does the trick. 它不是最简洁的pythonic代码,但是可以解决问题。

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

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