简体   繁体   English

plotly 水平堆叠条形图不适用于日期中的 x 轴

[英]plotly horizontal stacked bar chart not working with x-axis in dates

I am trying with one of the example provided at https://plotly.com/python/horizontal-bar-charts/ under the section Colored Horizontal Bar Chart .我正在尝试使用https://plotly.com/python/horizontal-bar-charts/Colored Horizontal Bar Chart部分下提供的示例之一。 But instead of number I am using dates但是我使用日期而不是数字

Code代码

import plotly.graph_objects as go

fig = go.Figure()
fig.add_trace(go.Bar(
    y=['giraffes', 'orangutans', 'monkeys'],
    x=['2012-02-02', '2012-02-01', '2012-02-01'],
    name='SF Zoo',
    orientation='h',
    marker=dict(
        color='rgba(246, 78, 139, 0.6)',
        line=dict(color='rgba(246, 78, 139, 1.0)', width=3)
    )
))
fig.add_trace(go.Bar(
    y=['giraffes', 'orangutans', 'monkeys'],
    x=['2012-02-10', '2012-02-06', '2012-02-28'],
    name='LA Zoo',
    orientation='h',
    marker=dict(
        color='rgba(58, 71, 80, 0.6)',
        line=dict(color='rgba(58, 71, 80, 1.0)', width=3)
    )
))

fig.update_layout(barmode='stack')
fig.show()

I tried with datetime.datetime(2012, 2, 2, 0, 0), still give me a completely wrong graph.我尝试使用 datetime.datetime(2012, 2, 2, 0, 0),仍然给我一个完全错误的图表。 I tried playing with the layout format but still couldn't fix this我尝试使用布局格式,但仍然无法解决这个问题

Plot: Plot:

在此处输入图像描述

One solution is to use a Gantt Chart which may handle dates better (especially defining where it starts and ends).一种解决方案是使用甘特图,它可以更好地处理日期(特别是定义它的开始和结束位置)。

Here's a working example:这是一个工作示例:

import plotly.express as px
import pandas as pd

df = pd.DataFrame([
    dict(Task="LA Zoo", Start='2012-02-02', Finish='2012-02-10', Resource="giraffes"),
    dict(Task="LA Zoo", Start='2012-02-01', Finish='2012-02-6', Resource="orangutans"),
    dict(Task="LA Zoo", Start='2012-02-01', Finish='2012-02-28', Resource="monkeys"),
    dict(Task="SF Zoo", Start='2012-02-10', Finish='2012-02-17', Resource="giraffes"),
    dict(Task="SF Zoo", Start='2012-02-6', Finish='2012-02-22', Resource="orangutans"),
    dict(Task="SF Zoo", Start='2012-02-28', Finish='2012-03-07', Resource="monkeys")
])

fig = px.timeline(df, x_start="Start", x_end="Finish", y="Resource", color="Task")
fig.show()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM