【问题标题】:X axis invisible for large dataset大型数据集的 X 轴不可见
【发布时间】:2020-07-03 14:12:19
【问题描述】:

我是 python 新手,我正在尝试绘制日期和时间在 X 轴上的数据。这些数据是关于几天内几小时内的推文数量。由于数据庞大,X 轴刻度变得不可见。下面是主要数据的sn-p(我要绘制的数据)

> Date       Hour 
> 2017-06-01  0        9922287
>             1        8518504
>             2       11329880
>             3        8917199
>             4        2561618
>             5        5356574
>             6        9094935
>             7        5668480
>             8       10685864
>             9        4817401
>             10      13737030
>             11      13102746
>             12      36891729
>             13      28093150
>             14      13071736
>             15      26999175
>             16      25637322
>             17      24140113
>             18      12172451
>             19      27828496
>             20      14746762
>             21      30112348
>             22      25418125
>             23      15357580 
> 2017-06-02  0       11392671
>             1        5044931
>             2        4476793
>             3        2218296
>             4        1736378
>             5         838815
>                       ...    
> 2017-06-03  22      10569552
>             23       9315997

我的情节使用了以下代码。

df.plot(marker='*')

plt.legend().set_visible(False)

plt.title("Number of tweets on hourly basis")

在调整大小时,我使用了 plt.figure(figsize=(20,10)),我得到下图。但是我的 x 轴数字仍然是不可见的。

【问题讨论】:

    标签: python pandas matplotlib large-data


    【解决方案1】:

    可能没有安装最新的 pandas 版本。在我的带有 pandas 1.0.3 的系统上,x-ticks 显示为[2017-06-01 00:00:00, 0]。使用df.plot(marker='*', rot=30) 设置标签旋转使它们不会重叠。

    但无论如何,这不是一个非常令人满意的输出。 (我假设 'Date' 列是 pandas 日期格式。如果它是字符串格式,结果会相似,没有00:00:00。)

    无论如何,要走的路是将日期和小时列合并为一个日期时间列。这是一种可能的方法:

    from matplotlib import pyplot as plt
    import pandas as pd
    import numpy as np
    
    # first create a dataframe similar to the example
    days = pd.date_range('2017-06-01', '2017-06-03', freq='D')
    df = pd.DataFrame({'Date': np.repeat(days, 24),
                       'Hour': np.tile(np.arange(0, 24), len(days)),
                       'NumTweets': np.random.binomial(10000, 0.2, 24 * len(days))})
    df.set_index(['Date', 'Hour'], drop=True, inplace=True)
    
    # df.plot(marker='*', rot=30)  # this would be the plot from the question
    
    df.reset_index(inplace=True) # remove the index, making 'Date' and 'Hour' regular columns
    # create a new column combining 'Date' and 'Hour'
    df['Time'] = pd.to_datetime(df['Date'].dt.strftime('%Y-%m-%d') + ' ' + df['Hour'].astype(str).str.zfill(2))
    # use the new column as index
    df.set_index('Time', drop=True, inplace=True)
    
    # as the 'Date' and 'Hour' columns are still there, indicate we only want to plot the 'NumTweets' column
    df.plot(y='NumTweets', marker='*', rot=20) # rot=0 would also work, depending on the figure width
    plt.tight_layout() # make space to show the labels
    
    plt.show()
    

    请注意,pandas 会根据显示的天数调整您的 x 轴。只有 3 天,在 00:00 时会有“主要”刻度,在 12:00 时会有“次要”刻度。随着天数的增加,小时数将不会出现刻度。

    【讨论】:

    • 您好,感谢您的快速周转。即使尝试了您的步骤,我仍然得到与以前相同的图表。
    • 如果你运行我的测试代码,你得到相同的输出吗?你有安装最新的熊猫吗?请注意,如果您不使用 pandas 日期时间格式,事情可能会变得混乱。 Pandas 对日期时间的外观与标准 matplotlib 有不同的想法。
    • 您尝试过最新的 pandas 版本(1.0.3)吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    • 2020-11-24
    • 2017-11-10
    • 2013-09-07
    相关资源
    最近更新 更多