我已经在plotly上完成了这个图表
我想删除空白,仅显示有值的 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()
最佳答案
发生这种情况的原因是,它会将您的 x Axis 解释为日期,并为您制作一个时间线。您可以通过多种方式避免这种情况。一种可能性是将日期替换为日期的字符串表示形式。
在 x Axis 上绘制日期:
现在,只需将下面代码段中的 x=df.index
替换为 x=df.index.strftime("%Y/%m/%d")
得到这个 plotly :
在 x Axis 上绘制字符串:
代码:
# 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()
关于python - Plotly:如何消除x轴上的空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59021060/