Accessing new axis limits soon after we set it
When I set the limits of the axis using dpg.set_axis_limits, I would expect the dpg.get_axis_limits to fetch me the new values. Is my understanding correct?
If we check the output of print(dpg.get_axis_limits('x_axis')) in the code below. I would expect (9, 33) to be printed, but I get (0, 0)
What is the logic behind this? Is this because, the plot is not refreshed yet?
import dearpygui.dearpygui as dpg
dpg.create_context()
with dpg.window(label="Tutorial", width=400, height=400):
with dpg.group(horizontal=True):
dpg.add_button(label="fit y", callback=lambda: dpg.fit_axis_data("y_axis"))
dpg.add_button(label="unlock x limits", callback=lambda: dpg.set_axis_limits_auto("x_axis"))
dpg.add_button(label="unlock y limits", callback=lambda: dpg.set_axis_limits_auto("y_axis"))
dpg.add_button(label="print limits x", callback=lambda: print(dpg.get_axis_limits("x_axis")))
dpg.add_button(label="print limits y", callback=lambda: print(dpg.get_axis_limits("y_axis")))
with dpg.plot(label="Bar Series", height=-1, width=-1):
dpg.add_plot_legend()
# create x axis
dpg.add_plot_axis(dpg.mvXAxis, label="Student", no_gridlines=True, tag="x_axis")
dpg.set_axis_limits(dpg.last_item(), 9, 33)
print(dpg.get_axis_limits('x_axis'))
dpg.set_axis_ticks(dpg.last_item(), (("S1", 11), ("S2", 21), ("S3", 31)))
# create y axis
dpg.add_plot_axis(dpg.mvYAxis, label="Score", tag="y_axis")
dpg.set_axis_limits("y_axis", 0, 110)
# add series to y axis
dpg.add_bar_series([10, 20, 30], [100, 75, 90], label="Final Exam", weight=1, parent="y_axis")
dpg.add_bar_series([11, 21, 31], [83, 75, 72], label="Midterm Exam", weight=1, parent="y_axis")
dpg.add_bar_series([12, 22, 32], [42, 68, 23], label="Course Grade", weight=1, parent="y_axis")
dpg.create_viewport(title='Custom Title', width=800, height=600)
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()
@hoffstadt @v-ein Definitely appreciate your help here. Thank you
Is this because, the plot is not refreshed yet?
Exactly. get_axis_limits retrieves the "actual" limits (mvPlotAxisConfig::limits_actual), which only gets updated when the plot is rendered. Until the next frame, you'll be getting older "actual" limits. Add a split_frame if needed, or, better, store the limits that you passed to set_axis_limits and reuse your own values instead of getting them back from DPG (think of get_axis_limits as "tell me the current state" instead of a "property getter").
I almost haven't used plots, but it might be that get_axis_limits also reflects changes when the user zooms and pans the plot. Not sure.
Is this because, the plot is not refreshed yet?
Exactly.
get_axis_limitsretrieves the "actual" limits (mvPlotAxisConfig::limits_actual), which only gets updated when the plot is rendered. Until the next frame, you'll be getting older "actual" limits. Add asplit_frameif needed, or, better, store the limits that you passed toset_axis_limitsand reuse your own values instead of getting them back from DPG (think ofget_axis_limitsas "tell me the current state" instead of a "property getter").
Thank you for the reply @v-ein . I could see the same in deapygui commands header file.
I am curious to know why is this behaviour expected. Because like you mentioned in case of zoom and pan it gets updated in that instance but setting the axis limit do not update to the new values
I've no idea why :) but clearly it was a design decision at some point.
I've no idea why :) but clearly it was a design decision at some point.
Thanks you @v-ein for the quick response and yes it confused me for a bit. I could see it updated when I had put breakpoint to verify then I noticed this behaviour. May be worth to add it in the documentation. Will try to send a PR
@hoffstadt Im curious to understand this design choice, for a better mind map.