这是我的回调函数代码,它是使用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)
But when I don't include this portion of the code, the complete plot gets displayed:
有没有人可以帮助显示完整的曲线图,即使在包含了设置x轴范围的代码部分之后?