如何绘制起始值非零的线图?

huangapple go评论87阅读模式
英文:

How to plot lineplot with x axis value starting from some non zero value

问题

我正在尝试生成一个季节性图,其中每条线对应一年(例如2018年、2019年...),x轴的值将是一年中的周数(从1到52)。
然而,我希望在图表上设置一些偏移,例如x轴的值为30、31、...、52、1、2、...、29。

我尝试将x轴转换为分类变量,但是在周52到周1之间始终会有一段间隔。

import pandas as pd
import numpy as np

def generate_sample_values(param1, param2, startendpoint):
    if startendpoint < 1 or startendpoint > 53:
        raise ValueError
    lst = [(x/param1)+np.random.randint(param2) for x in range(1,startendpoint)]
    lst2 = lst[:53-startendpoint][::-1]
    return lst + lst2

ls = generate_sample_values(1, 10, 30)

df = pd.DataFrame({"week":[x for x in range(1,53)], 
              "2019":generate_sample_values(1,10,30),
              "2020":generate_sample_values(0.5,10,30), 
              "2021":generate_sample_values(2,15,30),
              "2022":generate_sample_values(3,17,30)
             }).set_index("week")

px.line(df, x=df.index, y=df.columns)

在上述代码中,我希望x轴从30、31、...、52、1、2、...、29开始。

英文:

I am trying to generate a seasonality plot with each line corresponding to a year (i.e. lines for 2018, 2019, ...)
the x-axis values would be the week number of the year (ranging from 1 - 52).
However, I would like to start the plot at some offset e.g. x-axis values = 30, 31, ... 52, 1, 2, ..., 29

I tried to convert x axis to categorical but there would always be a gap in the lineplot between week 52 to week 1.

import pandas as pd
import numpy as np
def generate_sample_values(param1, param2, startendpoint):
    if startendpoint &lt; 1 or startendpoint &gt; 53:
        raise ValueError
    lst = [(x/param1)+np.random.randint(param2) for x in range(1,startendpoint)]
    lst2 = lst[:53-startendpoint][::-1]
    return lst + lst2

ls = generate_sample_values(1, 10, 30)

df = pd.DataFrame({&quot;week&quot;:[x for x in range(1,53)], 
              &quot;2019&quot;:generate_sample_values(1,10,30),
              &quot;2020&quot;:generate_sample_values(0.5,10,30), 
              &quot;2021&quot;:generate_sample_values(2,15,30),
              &quot;2022&quot;:generate_sample_values(3,17,30)
             }).set_index(&quot;week&quot;)
px.line(df, x =df.index, y= df.columns)

in the above, i would instead like to have x axis to start at 30, 31, ... 52, 1, 2, ..., 29

答案1

得分: 1

要完成这个任务,首先按照你的需求重新排列数据...从索引30开始直到结束,然后将前29个连接到数据框的末尾。然后,你需要将索引(周数)转换为字符串并绘制图表。下面是更新后的代码...

def generate_sample_values(param1, param2, startendpoint):
    if startendpoint < 1 or startendpoint > 53:
        raise ValueError
    lst = [(x/param1)+np.random.randint(param2) for x in range(1,startendpoint)]
    lst2 = lst[:53-startendpoint][::-1]
    return lst + lst2

ls = generate_sample_values(1, 10, 30)

df = pd.DataFrame({"week":[x for x in range(1,53)], 
              "2019":generate_sample_values(1,10,30),
              "2020":generate_sample_values(0.5,10,30), 
              "2021":generate_sample_values(2,15,30),
              "2022":generate_sample_values(3,17,30)
             }).set_index("week")

df=pd.concat([df.iloc[29:], df.iloc[:29]])
df.index = df.index.map(str)

fig=px.line(df, x =df.index, y= df.columns)
fig.update_xaxes(tickangle=0)

这将给你以下的图表...

如何绘制起始值非零的线图?

现在,如果你不喜欢标签并且认为它们太多,你可以通过在底部添加以下代码来调整它们的位置...

fig.update_layout(
    xaxis = dict(tickmode = 'linear', tick0 = 30, dtick = 5)
)

...这将给你以下的图表 - 从30开始,每5个刻度标签显示一个。

如何绘制起始值非零的线图?

英文:

To do this, first rearrange the data as per your need... start from index 30 till end and then concatenate the first 29 at the end of the dataframe. Then, you need to convert the index (week number) to string and plot it. The updated code is shown below...

def generate_sample_values(param1, param2, startendpoint):
    if startendpoint &lt; 1 or startendpoint &gt; 53:
        raise ValueError
    lst = [(x/param1)+np.random.randint(param2) for x in range(1,startendpoint)]
    lst2 = lst[:53-startendpoint][::-1]
    return lst + lst2

ls = generate_sample_values(1, 10, 30)

df = pd.DataFrame({&quot;week&quot;:[x for x in range(1,53)], 
              &quot;2019&quot;:generate_sample_values(1,10,30),
              &quot;2020&quot;:generate_sample_values(0.5,10,30), 
              &quot;2021&quot;:generate_sample_values(2,15,30),
              &quot;2022&quot;:generate_sample_values(3,17,30)
             }).set_index(&quot;week&quot;)

df=pd.concat([df.iloc[29:], df.iloc[:29]])
df.index = df.index.map(str)

fig=px.line(df, x =df.index, y= df.columns)
fig.update_xaxes(tickangle=0)

This will give you the below plot...

如何绘制起始值非零的线图?

Now, if you dont like the labels and find them as too many, you can adjust how far you want them by adding the code at the bottom...

fig.update_layout(
    xaxis = dict(tickmode = &#39;linear&#39;, tick0 = 30, dtick = 5)
)

...which will give you the plot below - start at 30 and show every 5th tick label.

如何绘制起始值非零的线图?

huangapple
  • 本文由 发表于 2023年8月9日 13:29:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76864834.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定