How to create Double-Y plot where the right side is a "sticker" for where each curve "goes"

1 visualización (últimos 30 días)
So I have many curves (spectra, for that matter), and it is useful to view them in a plot similar to this: https://www.mathworks.com/matlabcentral/answers/uploaded_files/213411/image.jpeg. The key twist is that unlike the attached figure, the Y-axis "distance" between the curves matters and is different for each. So I'd like to have this graph be a double-y plot, where the right side controls where each spectrum "goes" on the plot.
I've tried stackedplot and waterfall as built-in Matlab functions, but I no longer believe those are tools for the job. Does anyone have any hint what I might need?
  1 comentario
dpb
dpb el 22 de Jun. de 2022
" ... the Y-axis "distance" between the curves matters and is different for each. So I'd like to have this graph be a double-y plot, where the right side controls where each spectrum "goes" on the plot."
I have no idea what the above is trying to describe -- how does the RH axis have anything to do with the offset you choose to add to a line drawn with some reference to another?
If the attachment doesn't illustrate what is wanted, specifically, then it's of little help; you may have a mental picture of what you think the result should be, but we don't have the benefit of the inbuilt knowldege.
Sketch out something that illustrates what you're thinking about if you can actually visualize it and post that...then there's a chance somebody might have a way to generate the result to match.

Iniciar sesión para comentar.

Respuestas (1)

Avni Agrawal
Avni Agrawal el 18 de Oct. de 2023
Hi,
I understand that you are trying to create multiple waves plot where left y-axis denotes values for each wave and right y-axis denotes distance between 2 consecutive waves. To achieve that you can use the yyaxisfunction in MATLAB.
Here is the code example for the same:
% Generate example data
x = 1:10; % X-axis values
waves = [sin(x); cos(x); tan(x)]; % Example waveforms (3 waves)
% Create a figure and axes
figure;
ax = gca;
% Plot the waves
for i = 1:size(waves, 1)
yyaxis left;
plot(x, waves(i, :), 'LineWidth', 2);
hold on;
ylabel(['Wave ' num2str(i)]);
% Adjust the position of the left Y-axis
ax.YAxis(1).Color = 'k';
ax.YAxis(1).Label.Color = 'k';
ax.YAxis(1).Limits = [-1.5, 1.5]; % Adjust the limits based on your data
% Calculate the distance between two consecutive waves
if i > 1
distance = abs(max(waves(i-1, :)) - min(waves(i, :)));
yyaxis right;
plot(x, distance * ones(size(x)), 'LineWidth', 1, 'LineStyle', '--', 'Color', 'k');
hold on;
ylabel('Distance');
% Adjust the position of the right Y-axis
ax_pos = ax.Position;
ax_pos(3) = 0.85 * ax_pos(3);
ax.Position = ax_pos;
end
end
In this code, the example waveforms are stored in the waves matrix, where each row represents a different wave. The waves are plotted using a loop, and the yyaxisfunction is used to switch between the left and right Y-axes. The position of each Y-axis is adjusted based on the wave index to achieve the desired layout. The distance between two consecutive waves is calculated and plotted on the right Y-axis.
You can refer to this MATLAB documentation for more information:
I hope this helps.

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by