密谋:如何消除x轴上的空白

我已经在图中绘制了该图

密谋:如何消除x轴上的空白

我想删除空白,仅显示具有值的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()
foxconn_fy 回答:密谋:如何消除x轴上的空白

发生这种情况的原因是,将您的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()
,

万一有人在这里玩股票数据,下面是隐藏非交易时间周末的代码。

    fig = go.Figure(data=[go.Candlestick(x=df['date'],open=df['Open'],high=df['High'],low=df['Low'],close=df['Close'])])
    fig.update_xaxes(
        rangeslider_visible=True,rangebreaks=[
            # NOTE: Below values are bound (not single values),ie. hide x to y
            dict(bounds=["sat","mon"]),# hide weekends,eg. hide sat to before mon
            dict(bounds=[16,9.5],pattern="hour"),# hide hours outside of 9.30am-4pm
            # dict(values=["2020-12-25","2021-01-01"])  # hide holidays (Christmas and New Year's,etc)
        ]
    )
    fig.update_layout(
        title='Stock Analysis',yaxis_title=f'{symbol} Stock'
    )

    fig.show()

这里是Plotly's doc

本文链接:https://www.f2er.com/3040643.html

大家都在问