如何修复 Plotly Graph 对象中的 x 轴值?

How to fix the x-axis value in the Plotly Graph Object?

我正在创建一个图表,该图表使用很少的汇总值显示在绘图上。我需要我的 x 轴从 1 到 30 保持不变,修复它。所以当水平条出现时,它总是小于或等于 30。

import plotly.graph_objects as go
import pandas as pd
df_prev_weather_final_sum = pd.read_csv("/content/sample_data/weather_sum_sample.csv")
print(df_prev_weather_final_sum.head())
weather_fig = go.Figure(go.Bar(
                x = df_prev_weather_final_sum["values"],
                y = df_prev_weather_final_sum["index"],
                orientation = 'h',
                marker_color='indianred'
            ))
weather_fig.update_layout(
    title={
        'text': "Weather",
        'y':0.9,
        'x':0.5,
        'xanchor': 'center',
        'yanchor': 'top'},
    yaxis_title="Port-Shipper",
    xaxis_title="Number of Days",
    template = "plotly_dark")
weather_fig.show()

如何使 'number of days' 轴固定?

您可以使用 update_xaxes 配置 xaxis 范围:

weather_fig.update_xaxes(range=[1,30], dtick=1)