绘制x轴上的空白间隙(Python)

问题描述 投票:1回答:1

我已经在情节上完成了此图

Diagram

而且我想删除空白,只显示具有值的x,并在没有任何值的地方隐藏x]

我应该怎么做?

这是我的代码:

go.Bar(name=i,x=listeDepartement,y=listePPA))
fig = go.Figure(data=bar)
fig.update_layout(barmode='stack')
fig.write_html('histogram.html',auto_open=True)
fig.show()
python plotly axis diagram
1个回答
0
投票

发生这种情况的原因是,将您的x轴以图表方式解释为日期,并为您创建了时间表。您可以通过几种方式避免这种情况。一种可能性是用日期的字符串表示形式替换日期。

在x轴上标有日期的图:

enter image description here

现在,只需将下面的代码片段中的x=df.index替换为x=df.index.strftime("%Y/%m/%d"),即可得到该图:

在x轴上带有字符串的图:

enter image description here

代码:

# imports
from plotly.subplots import make_subplots
import plotly.graph_objs as go
import pandas as pd
import numpy as np

# data
np.random.seed(123)
frame_rows = 50
n_plots = 1
frame_columns = ['V_'+str(e) for e in list(range(n_plots+1))]
df = pd.DataFrame(np.random.uniform(-10,10,size=(frame_rows, len(frame_columns))),
                  index=pd.date_range('1/1/2020', periods=frame_rows),
                    columns=frame_columns)
df=abs(df)
df.iloc[21:-2]=np.nan
df=df.dropna()

# show figure
fig = go.Figure()
fig.add_traces(go.Bar(#x=df.index,
                       x=df.index.strftime("%Y/%m/%d"),
                         y=df['V_0']))

fig.show()
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.