Я мог бы воспроизвести вашу проблему. Я использую plotly.express
, но он работает так же, как plotly.graph_objs
Данные
import pandas as pd
import plotly.express as px
df = pd.DataFrame({"SPName":["SAMOSA"]*3+ ["COFFEE"]*4,
"S2BillDate":["2020-02-10/2020-02-16",
"2020-02-17/2020-02-23",
"2020-02-24/2020-03-01",
"2020-02-24/2020-03-01",
"2020-02-17/2020-02-23",
"2020-02-10/2020-02-16",
"2020-02-03/2020-02-09"],
"totSale":[4000, 4500, 5000, 10_000, 12_000, 10_000, 2000]})
Это производит
fig = px.line(df, x="S2BillDate", y="totSale", color="SPName")
fig.update_traces(mode='markers+lines')
fig.show()
Здесь проблема в том, как сортируются даты. Если вы видите, что первая точка для COFFEE
равна 2020-02-24/2020-03-01
, вторая 2020-02-17/2020-02-23
и т. Д.
Быстрое исправление будет
df1 = df.sort_values("S2BillDate").reset_index(drop=True)
fig = px.line(df1, x="S2BillDate", y="totSale", color="SPName")
fig.update_traces(mode='markers+lines')
Лично я предпочитаю работать с датами, а не со строками xaxis
df["Date"] = df["S2BillDate"].str.split("/").str[1].astype("M8")
fig = px.line(df, x="Date", y="totSale", color="SPName")
fig.update_traces(mode='markers+lines')
```[![enter image description here][3]][3]
but in this case in order to show the ticktext in the format you asked for you still need to sort `df` and in this case there you need more coding.
```python
df = df.sort_values(["Date"]).reset_index(drop=True)
fig = px.line(df, x="Date", y="totSale", color="SPName")
fig.update_traces(mode='markers+lines')
fig.update_layout(
xaxis = dict(
type="category",
tickmode = 'array',
tickvals = df["Date"].tolist(),
ticktext = df["S2BillDate"].tolist()
)
)
fig.show()