Aligning and linking an axis with different scale #649
Replies: 2 comments
-
|
Hello hello! Thank you for providing the reproducible example! I think the problem is that you needed to update the I wrote the working example below. Note that in my implementation, the X-axis center will always be the same on both plots. Depending on the // Data
const float x[4] = {10.0f, 10.0f, 90.0f, 90.0f};
const float y[4] = {0.0f, 0.05f, 0.05f, 0.0f};
// Linked axes values
static double x_min = 0.0;
static double x_max = 100.0;
static double y_min = 0.0;
static double y_max = 0.1;
if (ImPlot::BeginPlot("Original axis")) {
ImPlot::SetupAxisLinks(ImAxis_X1, &x_min, &x_max);
ImPlot::SetupAxisLinks(ImAxis_Y1, &y_min, &y_max);
ImPlot::PlotLine("Line", x, y, 4);
ImPlot::EndPlot();
}
// Convert limits to new scale (x_min_scaled/x_max_scaled)
const double x_factor = 2; // Range will be twice the original range
const double x_range = x_max - x_min;
const double x_center = (x_min + x_max) / 2.0;
double x_range_scaled = x_range * x_factor;
double x_min_scaled = x_center - x_range_scaled / 2.0;
double x_max_scaled = x_center + x_range_scaled / 2.0;
if (ImPlot::BeginPlot("Scaled axis")) {
ImPlot::SetupAxisLinks(ImAxis_X1, &x_min_scaled, &x_max_scaled);
ImPlot::SetupAxisLinks(ImAxis_Y1, &y_min, &y_max);
ImPlot::PlotLine("Line", x, y, 4);
ImPlot::EndPlot();
}
// --- Undo Scaling to compute x_min/x_max ---
// 1. Find the new scaled range and its center
double x_range_scaled_after = x_max_scaled - x_min_scaled; // Get updated range after user interaction
double x_center_after = (x_min_scaled + x_max_scaled) / 2.0; // Get updated center after user interaction
// 2. Calculate the original range
double x_range_after = x_range_scaled_after / x_factor;
// 3. Re-calculate the original min/max based on the center and original range
x_min = x_center_after - x_range_after / 2.0;
x_max = x_center_after + x_range_after / 2.0;
// --- Print values ---
ImGui::Text("Min %.3f, Max %.3f, Center %.3f, Range %.3f", x_min, x_max, x_center, x_range);
ImGui::Text("MinScaled %.3f, MaxScaled %.3f, CenterScaled %.3f, RangeScaled %.3f", x_min_scaled, x_max_scaled, x_center_after,
x_range_scaled);Video: Peek.2025-09-29.17-31.mp4Obs: I think this question would be a better fit in the Q&Adiscussion instead of issues. |
Beta Was this translation helpful? Give feedback.
-
|
Hello thank you for the reply, you're right i posted in the wrong location. Sorry about that, Im not too sure how to move it back to Q&A. I see that your example provided further insight on using the SetupAxisLinks, I think i'll be able to further modify it to meet my intended behavior! thank you very much for your time!
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello, I am trying to visualize the relation of velocity, acceleration, time and position of my parameters.
But i got stuck trying to achieve the intedend behavior.
my intended behavior,
Moving/panning/zooming X axis or Y axis in one of the plot would also perform the same changes in the other plot
the current behavior
Moving/panning/zooming X axis in the position plot, can update the X axis of timing plot.
Y axis is sort of stuck, unable to pan/zoom.
What other things i've tried
I tried using subplots to align x axis, but as the scale for position and timing are different which makes the allignment not correct.
Other thing to achieve: Ideally, the 2 plots graph should be identical , the only difference is the unit scale of X axis, so i would like them to be drawn in the same plot and have use axisx1 and axisx2 with the pan/zoom features for all the axis, and all the allignment should be correct.
Hopefully the idea is understandable, i just need some pointers / would appreciate a code that can do the intended behavior.
VelocityProfile_Axis_Alignment.mp4
Edit: ( forgot to attach the code)
Beta Was this translation helpful? Give feedback.
All reactions