python中plotly.express中线条图,Plotly在Python中表达的意外行

aQMaH.png

Plotly draws an extra diagonal line from the start to the endpoint of the original line graph.

Other data, other graphs work fine.

Only this data adds the line.

Why does this happen?

How can I fix this?

Below is the code

temp = pd.DataFrame(df[{KEY_WORD}])

temp['date'] = temp.index

fig=px.line(temp.melt(id_vars="date"), x='date', y='value', color='variable')

fig.show()

plotly.offline.plot(fig,filename='Fig_en1')

解决方案

A similar question has been asked and answered in the post How to disable trendline in plotly.express.line?, but in your case I'm pretty sure the problem lies in temp.melt(id_vars="date"), x='date', y='value', color='variable'. It seems you're transfomring your data from a wide to a long format. You're using color='variable' without specifying that in temp.melt(id_vars="date"). And when the color specification does not properly correspond to the structure of your dataset, an extra line like yours can occur. Just take a look at this:

Command 1:

fig = px.line(data_frame=df_long, x='Timestamp', y='value', color='stacked_values')

Plot 1:

w4oFk.png

Command 2:

fig = px.line(data_frame=df_long, x='Timestamp', y='value')

Plot 2:

JCsmw.png

See the difference? That's why I think there's a mis-specification in your fig=px.line(temp.melt(id_vars="date"), x='date', y='value', color='variable').

So please share your data, or a sample of your data that reproduces the problem, and I'll have a better chance of verifying your problem.

Plotly Express (px) 是 Plotly 库的一个高级接口,它提供了一套简洁的方式来创建交互式图表,包括甘特图。甘特图是一种常用的时间管理工具,用于展示项目进度和各个任务之间的依赖关系。以下是使用 `plotly.express.timeline` 函数创建甘特图的基本步骤: 1. 首先,你需要安装 plotlyplotly.express,如果你还没有安装,可以用 pip 安装: ``` pip install plotly plotly_express ``` 2. 导入必要的库: ```python import plotly.express as px import pandas as pd ``` 3. 准备数据。甘特图通常需要包含以下几个列: - `start_date`:任务开始日期。 - `end_date`:任务结束日期。 - `task_name`:任务名称。 - 可选的:`description` 或 `labels` 列以添加额外信息。 4. 创建一个 DataFrame,并按照上述列名填充数据: ```python data = { "task_name": ["任务A", "任务B", "任务C"], "start_date": pd.to_datetime(["2023-01-01", "2023-01-05", "2023-01-10"]), "end_date": pd.to_datetime(["2023-01-10", "2023-01-15", "2023-01-20"]) } df = pd.DataFrame(data) ``` 5. 使用 `px.timeline` 函数创建甘特图: ```python fig = px.timeline(df, x_start="start_date", x_end="end_date", labels={"start_date": "开始时间", "end_date": "结束时间"}) ``` 这里 `x_start` 和 `x_end` 指定时间轴的起点和终点,`labels` 参数用于设置坐标轴标签。 6. 显示图表: ```python fig.show() ``` 7. 如果需要自定义样式,可以调整 `fig.update_layout()` 的选项,比如更改线条颜色、宽度,添加网格线等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值