英文:
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,32,...,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 < 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)
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 < 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)
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 = 'linear', tick0 = 30, dtick = 5)
)
...which will give you the plot below - start at 30 and show every 5th tick label.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论