How to save the scatter plot with and without transparency

Hi,
I need to save the scatter graph plotted with the same data but one with transparency applied and the other without transparency. Although the below code saves both the figures, however, gca styles are only applied on the second plot (i.e. the one with transparency)
a=figure;
scatter(F2(:,2),F3(:,3),0.1,F4(:,5));
b=figure;
scatter(F2(:,2),F3(:,3),0.1,F4(:,5),'MarkerFaceAlpha',0.2,'MarkerEdgeAlpha',0.2);
set(gca, 'FontSize', 10)
set(gca,'fontname','Arial')
set(gca, 'fontweight', 'bold')
saveas(a,'X_1.emf')
saveas(b,'X_2.emf')

 採用された回答

Star Strider
Star Strider 2023 年 3 月 4 日

0 投票

The gca function (of course) is ‘get current axes’, and it does exactly that. One option is to save an axes reference in each scatter call, and the other is to refer to the plot Parent. Either will work.
One approach —
F2 = randi(9,50,5); % Create Data
F3 = randi(9,50,5); % Create Data
F4 = randi(9,50,5); % Create Data
a=figure;
scatter(F2(:,2),F3(:,3),10,F4(:,5));
Axa = gca;
b=figure;
scatter(F2(:,2),F3(:,3),10,F4(:,5),'MarkerFaceAlpha',0.2,'MarkerEdgeAlpha',0.2);
Axb = gca;
set([Axa,Axb], 'FontSize', 10)
set([Axa,Axb],'fontname','Arial')
set([Axa,Axb], 'fontweight', 'bold')
% saveas(a,'X_1.emf')
% saveas(b,'X_2.emf')
And with that change, the set calls apply to both of them.
The saveas calls threw an error because .emf is not a recognised extension, and they threw an error that prevented the code from running, so I commented them out so it would.
.

5 件のコメント

Turbulence Analysis
Turbulence Analysis 2023 年 3 月 4 日
Thank you very much. May I know how to add colorbar to both the figures.
colormap(hot)
c1 = colorbar('FontSize',12);
clim ([1 10]);
c1.Title.FontSize = 16;
As always, my pleasure!
This looks like it should work —
F2 = randi(9,50,5); % Create Data
F3 = randi(9,50,5); % Create Data
F4 = randi(9,50,5); % Create Data
a=figure;
scatter(F2(:,2),F3(:,3),10,F4(:,5));
colormap(hot)
c1 = colorbar('FontSize',12);
clim ([1 10]);
c1.Title.FontSize = 16;
Axa = gca;
b=figure;
scatter(F2(:,2),F3(:,3),10,F4(:,5),'MarkerFaceAlpha',0.2,'MarkerEdgeAlpha',0.2);
colormap(hot)
c2 = colorbar('FontSize',12);
clim ([1 10]);
c2.Title.FontSize = 16;
Axb = gca;
set([Axa,Axb], 'FontSize', 10)
set([Axa,Axb],'fontname','Arial')
set([Axa,Axb], 'fontweight', 'bold')
% saveas(a,'X_1.emf')
% saveas(b,'X_2.emf')
I gave the colorbar objects different designations in each figure to keep them separate.
The colorbar object is one of the Children of each of the figure objects. The other is the scatter axes. (I checked to be sure, in the event you need that information.)
.
Turbulence Analysis
Turbulence Analysis 2023 年 3 月 4 日
Thanks.
I already defined in this way. I thought there may a way to define it in common, rather than defining it individually for each scatter plots. Anyways, that's fine, it's just two scatter plots.
Star Strider
Star Strider 2023 年 3 月 4 日
The same colorbar could work for multiple axes, however it would have to be defined separately for each of them, according to the target argument documentation. Having separate handles to each of them makes it easier to change both of their properties with a single set call. Otherwise, the set call would likely have to be repeated for each colorbar separately in each figure. It likely does not matter unless you want to do somethng like that.
Turbulence Analysis
Turbulence Analysis 2023 年 3 月 4 日
Yes, got it..

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

その他の回答 (1 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023 年 3 月 4 日
編集済み: Sulaymon Eshkabilov 2023 年 3 月 4 日

0 投票

Use exportgraphics() and also, note where the set() command for figure a and b is used and where exportgraphics() is used, e.g.:
F2 = repmat((1:10)', 1, 5);
F3 = F2.^2+2*F2-5;
F4 = sqrt(F3);
a = figure;
surf(F3)
set(gca, 'FontSize', 10)
set(gca,'fontname','Courier')
set(gca, 'fontweight', 'bold')
aX=gca;
aX.XColor='k';
aX.YColor='g';
aX.ZColor='m';
exportgraphics(a, 'a.emf', 'ContentType', 'image', 'BackGroundColor', 'white'); % White background
b=figure;
surfc(abs(F4))
set(gca, 'FontSize', 10)
set(gca,'fontname','Arial')
set(gca, 'fontweight', 'bold')
bX=gca;
bX.XColor='r';
bX.YColor='g';
bX.ZColor='b';
xlabel('x', 'Color','r')
ylabel('y', 'Color','g')
zlabel('z', 'Color','b')
exportgraphics(b, 'b.emf', 'ContentType', 'image', 'BackgroundColor', 'yellow'); % yellow background

カテゴリ

ヘルプ センター および File ExchangeColor and Styling についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by