【问题标题】:Matplotlib dates x axis wrong distanceMatplotlib 日期 x 轴错误距离
【发布时间】:2015-12-11 23:35:17
【问题描述】:

我无法正确安排绘图中 X 轴上的日期标签。我放了图表图像,以便您更好地理解我的问题。

这里有创建图表的代码:

plt.figure()
x_axis = []    # contains the date value
y_axis = []    # an integer value
x_values_str = []    # contains the date representation

for data in json.loads(trend.trend_values):
    date_obj = datetime.datetime.strptime(str(data[3]), '%m-%d-%Y %H:%M')

    x_axis.append(dates.date2num(date_obj))
    y_axis.append(str(data[2]))
    x_values_str.append(str(data[3]))

    plt.xticks(x_axis, x_values_str, rotation=45)
    plt.plot_date(x_axis, y_axis, tz=None, xdate=True, ydate=False, linestyle='-', marker='D',color='g')

    plt.title("Vowel: " + trend.vowel)
    plt.margins(0.1)
    plt.subplots_adjust(bottom=0.1)

所以,这里的问题是 X 值彼此之间的距离不相等。如何调整间隔? 谢谢

【问题讨论】:

  • 您的问题是标签重叠,还是标签的中点与刻度线对齐?重叠是由于数据点来自同一日期,并且对齐是默认值。您是否希望数据点具有相同的距离,但标有日期?
  • @Stefan 是的,我希望数据点具有相同的距离,但标有日期。我的意思是,我希望数据点沿 x 轴均匀分布。我希望你明白我的目标:)

标签: python datetime matplotlib intervals


【解决方案1】:

您的x_axis 应该是一个整数范围,而不是日期列表,那么您将获得距离为1 的所有数据点,但标有您来自x_values_str 的日期。

x_axis.append(range(1,len(data[3])+1))

【讨论】:

    猜你喜欢
    • 2021-08-18
    • 2016-02-23
    • 1970-01-01
    • 2020-12-07
    • 2020-10-01
    • 2015-05-02
    • 2020-06-09
    • 1970-01-01
    • 2017-08-01
    相关资源
    最近更新 更多