这是我的回调函数代码,它是使用Dash包的更大代码的一部分.

@app.callback(
    Output("graph-container", "children"),
    Input("type-dropdown", "value"),
)
def update_graph(serial_number):
    
    serial_data = pd.read_csv("C:/Users/Enigma/data.csv")
    # Taking subset
    serial_data = serial_data[serial_data["serial_num"] == serial_number]

    # Plot the data
    fig = px.scatter(serial_data, x="date_time", y="voltage")
    fig.add_trace(
        go.Scatter(
            x=serial_data["date_time"], 
            y=serial_data["voltage"], 
            mode="markers",
            marker=dict(
                color=np.where(serial_data["after_red"], "orange", np.where(serial_data["voltage"] < 2.1, "red", "blue")),
                size=8
            )
        )
    )  
    
    # Set the x-axis range
    x_range = [np.min(serial_data["date_time"]), np.max(serial_data["date_time"])]
    
    # Set the x-axis range for the scatter plot and the line plot
    fig.update_xaxes(range=x_range)

    # Get the minimum and maximum y-axis values with a gap before the lowest value
    y_min = np.floor(serial_data["voltage"].min()) - 0.01
    
    fig.update_layout(
        title={
            'text': "Voltage Variation",
            'y':0.98,
            'x':0.5,
            'xanchor': 'center',
            'yanchor': 'top'
        },
        xaxis_title="Time", 
        yaxis_title="Voltage", 
        showlegend=False,
        # Set the y-axis range with a gap before the lowest value
        yaxis_range=[y_min, None],
        # Add some margin to the bottom of the plot
        margin=dict(l=50, r=50, b=50, t=50, pad=20),
        height=500
    )
    
    # Add a horizontal line at y = 2.1
    fig.add_shape(
        type="line",
        x0=serial_data["date_time"].min(),
        x1=serial_data["date_time"].max(),
        y0=2.100,
        y1=2.100,
        line=dict(color="black", dash="dot")
    )
    
    return html.Div(
        [
            dcc.Graph(
                id="first-graph",
                figure=fig
            )
        ],
        style={"width": "100%"},
    )

问题是,只要我包含设置x轴范围的代码的以下部分(如上所示),我的绘图就会在最右端被截断.

    # Set the x-axis range
    x_range = [np.min(serial_data["date_time"]), np.max(serial_data["date_time"])]
    
    # Set the x-axis range for the scatter plot and the line plot
    fig.update_xaxes(range=x_range)

The chopped-off plot: enter image description here

But when I don't include this portion of the code, the complete plot gets displayed: enter image description here

有没有人可以帮助显示完整的曲线图,即使在包含了设置x轴范围的代码部分之后?

推荐答案

您可能希望在X轴范围的两边都有一些填充.默认情况下,以打印方式将轴范围设置为[min - padding, max + padding]-padding = (max-min)/16.

因此,如果您想要一个更接近最小和最大日期时间的范围,可以try 将填充设置为小于默认值:

x_min, x_max = min(serial_data["date_time"]), max(serial_data["date_time"])
padding = (x_max - x_min) / 32
x_range = [x_min - padding, x_max + padding]

fig.update_xaxes(range=x_range)

Python相关问答推荐

如果我已经使用了time,如何要求Python在12秒后执行另一个操作.sleep

使用多个性能指标执行循环特征消除

Python -根据另一个数据框中的列编辑和替换数据框中的列值

如何根据条件在多指标框架上进行groupby

当密钥是复合且唯一时,Pandas合并抱怨标签不唯一

Pythind 11无法弄清楚如何访问tuple元素

如何在BeautifulSoup中链接Find()方法并处理无?

ModuleNotFound错误:没有名为Crypto Windows 11、Python 3.11.6的模块

追溯(最近最后一次调用):文件C:\Users\Diplom/PycharmProject\Yolo01\Roboflow-4.py,第4行,在模块导入roboflow中

将数据框架与导入的Excel文件一起使用

移动条情节旁边的半小提琴情节在海运

在Python中,从给定范围内的数组中提取索引组列表的更有效方法

用砂箱开发Web统计分析

在Python中计算连续天数

为什么常规操作不以其就地对应操作为基础?

提高算法效率的策略?

如何从pandas DataFrame中获取. groupby()和. agg()之后的子列?

30个非DATETIME天内的累计金额

将链中的矩阵乘法应用于多组值

当输入是字典时,`pandas. concat`如何工作?