【问题标题】:How to set xticks for the index of string with hvplot如何使用 hvplot 为字符串的索引设置 xticks
【发布时间】:2022-09-27 11:16:35
【问题描述】:

我有一个数据框region_cumulative_df_sel,如下所示:

Month-Day  regions  RAIN_PERCENTILE_25  RAIN_PERCENTILE_50  RAIN_PERCENTILE_75  RAIN_MEAN   RAIN_MEDIAN
07-01      1        0.0611691028        0.2811064720        1.9487996101    1.4330813885    0.2873695195
07-02      1        0.0945720226        0.8130480051        4.5959815979    2.9420840740    1.0614821911
07-03      1        0.2845511734        1.1912839413        5.5803232193    3.7756001949    1.1988518238
07-04      1        0.3402922750        3.2274529934        7.4262523651    5.2195668221    3.2781836987
07-05      1        0.4680584669        5.2418060303        8.6639881134    6.9092760086    5.3968687057
07-06      1        2.4329853058        7.3453550339        10.8091869354   8.7898645401    7.5020875931
... ...
... ...
... ...
06-27      1        382.7809448242      440.1162109375      512.6233520508  466.4956665039  445.0971069336
06-28      1        383.8329162598      446.2222900391      513.2116699219  467.9851379395  451.1973266602
06-29      1        385.7786254883      449.5384826660      513.4027099609  469.5671691895  451.2281188965
06-30      1        386.7952270508      450.6524658203      514.0201416016  471.2863159180  451.2484741211

索引\"Month-Day\" 是String 类型,表示日历年的第一天和最后一天,而不是日期时间类型。

我需要使用 hvplot 来开发交互式绘图。

region_cumulative_df_sel.hvplot(width=900)

很难查看 x 轴上的标签。如何将 xticks 更改为仅显示每个月的第一天,例如\"07-01\"、\"08-01\"、\"09-01\"、……、\"06-01\"?


我尝试了@Redox 代码如下:

region_cumulative_df_sel[\'Month-Day\'] = pd.to_datetime(region_cumulative_df_sel[\'Month-Day\'],format=\"%m-%d\") ##Convert to datetime

from bokeh.models.formatters import DatetimeTickFormatter
## Set format for showing x-axis ... you only need days, but in case counts change
formatter = DatetimeTickFormatter(days=[\"%m-%d\"], months=[\"%m-%d\"], years=[\"%m-%d\"])

region_cumulative_df_sel.plot(x=\'Month-Day\', xformatter=formatter, y=[\'RAIN_PERCENTILE_25\',\'RAIN_PERCENTILE_50\',\'RAIN_PERCENTILE_75\',\'RAIN_MEAN\',\'RAIN_MEDIAN\'], width=900, ylabel=\"Rainfall (mm)\",
                        rot=90, title=\"Cumulative Rainfall\")

这就是我生成的。

如何移动 x 轴上的 xticks 以与月日值对齐。此外,弹出窗口将“1900”显示为“月-日”列的年份。可以去掉年段吗?

    标签: pandas holoviews hvplot holoviz-panel


    【解决方案1】:

    x 轴数据为字符串格式。因此,holoviews 认为这是分类的并且绘制每一行。您需要将其转换为日期时间,这将允许绘图采用您需要的格式。我举一个简单的例子并展示如何做到这一点......应该也适用于你的情况......

    ##My month-day column is string -  07-01    07-02   07-03   07-04 ... 12-31
    
    df['Month-Day']=pd.to_datetime(df['Month-Day'],format="%m-%d") ##Convert to datetime
    df['myY']=np.random.randint(100, size=(len(df)))  ##Random Y data
    
    from bokeh.models.formatters import DatetimeTickFormatter
    ## Set format for showing x-axis ... you only need days, but in case counts change
    formatter = DatetimeTickFormatter(days=["%m-%d"], months=["%m-%d"], years=["%m-%d"])
    
    ##Plot graph
    df.plot(x='Month-Day',xformatter=formatter)#.opts(xticks=4, xrotation=90)
    

    【讨论】:

    • 感谢您的及时回复。我认为它越来越接近我想要实现的目标。请注意月-日是从 07-01、07-02、07-03、... ..、12-29、12-30、12-31、01-01、01-02、01-03、 ……,06-29、06-30。这是从7月1日到次年6月30日。当我尝试您的代码时,x 轴是从 01-01 到 12-31。请参阅我编辑的问题。
    【解决方案2】:

    @Redox 在这里是正确的。问题在于 Month-Day 列转换为日期时间的方式; pandas 假设每一行的年份是 1900 年。

    本质上,您需要以某种方式将年份附加到 Month-Day。

    请参见下面的示例,它采用第一个月份-日期字符串,添加“2022-”并为每一行生成连续的每日值(但有几种方法可以做到这一点)。

    代码:

    import pandas as pd
    import numpy as np
    import hvplot.pandas
    from bokeh.models.formatters import DatetimeTickFormatter
    
    dates = pd.date_range("2021-07-01", "2022-06-30", freq="D")
    
    
    df = pd.DataFrame({
        "md": dates.strftime("%m-%d"),
        "ign": np.cumsum(np.random.normal(10, 5, len(dates))),
        "sup": np.cumsum(np.random.normal(20, 10, len(dates))),
        "imp": np.cumsum(np.random.normal(30, 15, len(dates))),
    })
    
    df["time"] = pd.date_range("2021-" + df.md[0], periods=len(df.index), freq="D")
    
    formatter = DatetimeTickFormatter(
        days=["%m-%d"], months=["%m-%d"], years=["%m-%d"])
    
    df.hvplot(x='time', xformatter=formatter, y=['ign', 'sup', 'imp'],
     width=900, ylabel="Index", rot=90, title="Cumulative ISI")
    
    

    【讨论】:

      猜你喜欢
      • 2017-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-05
      • 2023-03-18
      • 2011-04-10
      • 1970-01-01
      • 2013-08-17
      相关资源
      最近更新 更多