Can you create string variables to use in plots that use LaTeX?

51 ビュー (過去 30 日間)
Daniel Neyers
Daniel Neyers 2024 年 1 月 15 日
コメント済み: Hassaan 2024 年 1 月 15 日
Hello,
I have a few graphs that I plot. I want to change the name of the legend entries / the DisplayName of the graph in one location.
So I set up string variables:
namePlot1 = "First measurement";
namePlot2 = "Second measurement";
Later during plotting I use that variable for the DisplayName.
scatter(plotData_X,plotData_Y,"DisplayName",namePlot1);
Now I want to use LaTeX expressions in some of the texts. More specificaly, the average x with a line above it.
'$\bar{x}$'
To display LaTeX text, you have to set 'Interpreter','latex'. But from what I understood so far, you use that inside a plot or legend command.
Is there a way to add Latex symbols to a variable and then use that variable to change the names of plot X/YLabel Strings and plot DisplayNames?

採用された回答

Hassaan
Hassaan 2024 年 1 月 15 日
% Define your LaTeX string variables for plot names
namePlot1 = '$\bar{x}$ First measurement';
namePlot2 = '$\bar{y}$ Second measurement';
% Some example data for the scatter plots
plotData_X1 = rand(10,1); % Random data for the first plot
plotData_Y1 = rand(10,1);
plotData_X2 = rand(10,1); % Random data for the second plot
plotData_Y2 = rand(10,1);
% Create the first scatter plot and use the LaTeX string for DisplayName
scatter(plotData_X1, plotData_Y1, 'DisplayName', namePlot1);
% Hold the current figure so that the next plot doesn't overwrite it
hold on;
% Create the second scatter plot and use the LaTeX string for DisplayName
scatter(plotData_X2, plotData_Y2, 'DisplayName', namePlot2);
% Add legend to the plot with LaTeX interpreter
legend('Interpreter', 'latex');
% Add labels to the axes with LaTeX interpreter
xlabel('X-axis Label', 'Interpreter', 'latex');
ylabel('Y-axis Label', 'Interpreter', 'latex');
% Set title with LaTeX interpreter
title('Comparison of Measurements', 'Interpreter', 'latex');
% Release the hold on the figure
hold off;
This script will create a figure with two scatter plots, each with a display name that includes a LaTeX-formatted bar over the variable names ˉxˉ and ˉyˉ​. The legend, axis labels, and title will also be able to render LaTeX formatted text since we have specified the 'Interpreter' as 'latex'.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
  8 件のコメント
Daniel Neyers
Daniel Neyers 2024 年 1 月 15 日
編集済み: Daniel Neyers 2024 年 1 月 15 日
@Muhammad Hassaan Shah Then may I refer you to my rather text heavy question over here?
Long story short:
I upload a high resolution background image which shows up fine in the Matlab view. Once I export it with
export_fig(export_figname, '-png','-a1', '-native');
the background itself turns into a blur. As if it got downscaled and then upscaled again.
Hassaan
Hassaan 2024 年 1 月 15 日
@Daniel Neyers That's indeed some serious stuff :-) :-). I will see what can be done. Thank you.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by