python - Plotly:如何消除x轴上的空白

标签 python plotly axis diagram

我已经在plotly上完成了这个图表

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()

最佳答案

发生这种情况的原因是,它会将您的 x Axis 解释为日期,并为您制作一个时间线。您可以通过多种方式避免这种情况。一种可能性是将日期替换为日期的字符串表示形式。

在 x Axis 上绘制日期:

enter image description here

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

在 x Axis 上绘制字符串:

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()

关于python - Plotly:如何消除x轴上的空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59021060/

相关文章:

python - 如何根据 Plotly 美国航类 map 示例中的航空公司更改颜色

r - 在 Gadfly 中使用对数刻度调整刻度间距

java - 如何更改javafx图表的 Axis 颜色?

python - 将 Pandas 对象与 Plotly ValueError 一起使用

javascript - d3.scale.ordinal 的问题

python - Python 中的前向欧拉 N 体模拟未返回正确结果

python - Pyspark 无法从 pathlib 对象加载

python - Python中的位列表到整数

python - Beautifulsoup 使用下一个按钮分页

r - 在 Shiny 应用程序中创建动态选项卡集会在 ggploty 上出现错误