[HELP] Axis limit never update
cloudy-sfu opened this issue · comments
Minimum example:
import time
from datetime import datetime
from threading import Thread
import dearpygui.dearpygui as dpg
import pytz
dpg.create_context()
def update_figure_wrapper(hostname):
selected_axis = f"stem_{hostname}"
def update_figure():
while dpg.does_item_exist(selected_axis):
t2 = int(datetime.now(tz=pytz.utc).timestamp())
t1 = t2 - 65
print("Expected X-Axis range is", t1, t2)
dpg.set_axis_limits(axis=x_axis, ymin=t1, ymax=t2)
print("Actual X-Axis range is", dpg.get_axis_limits(x_axis))
time.sleep(1)
return update_figure
def display_connection(connection):
selected_axis = f"stem_{connection}"
if dpg.does_item_exist(selected_axis):
dpg.delete_item(selected_axis)
dpg.add_stem_series(x=[], y=[], label=connection, parent=y_axis, tag=selected_axis)
thread = Thread(target=update_figure_wrapper(connection))
thread.start()
with dpg.window(tag="main_window"):
with dpg.plot(tag="tcping_plot", width=600, height=400):
dpg.add_plot_legend(parent="tcping_plot")
x_axis = dpg.add_plot_axis(
dpg.mvXAxis, label="Time", parent="tcping_plot", time=True,
)
y_axis = dpg.add_plot_axis(
dpg.mvYAxis, label="Delay", parent="tcping_plot", lock_min=True,
)
dpg.add_button(
label="Start", callback=lambda: display_connection("test")
)
dpg.create_viewport(title="Title")
dpg.setup_dearpygui()
dpg.maximize_viewport()
dpg.show_viewport()
dpg.set_primary_window("main_window", True)
dpg.start_dearpygui()
dpg.destroy_context()
I want to dynamically adjust the range of X-Axis with update_figure
function (the expected behavior is like resource manager), but the range of X-Axis never update.
GPT keeps suggesting changing ymin, ymax
to xmin, xmax
, but the function set_axis_limits
only accept ymin, ymax
arguments. It's mainly implemented by function update_figure
. When user click "start" button in GUI, update_figure
will update per second. I have removed the data query part and the multiple series as a simplified example, so don't feel strange that I wrap update_figure
function.
How to solve this problem? Thank you!
Get rid of ChatGPT, it's of no help here. Whatever little it knows about DPG, it learned from docs of a very old version of DPG, which has since changed a lot.
Now, back to your code. You're saying the range never updates, but I've just tried your code and the range does update:
Expected X-Axis range is 1710591945 1710592010
Actual X-Axis range is [0.0, 1.0]
Expected X-Axis range is 1710591946 1710592011
Actual X-Axis range is [1710592000.0, 1710592000.0]
Expected X-Axis range is 1710591947 1710592012
Actual X-Axis range is [1710592000.0, 1710592000.0]
The reason you're seeing different numbers than what you specified in the set_axis_limits
call is loss of precision on floating-point numbers. There are a number of tickets opened on this; you can take a look at #2157 for example (other tickets are #2067, #1847, #386, maybe something else I missed).
Hi v-ein, I try to always subtract 1710632700 from t1
, and it works as expected. It seems that #2157 is the root cause. Thanks a lot.