Why does MATLAB export the .png with a graph besides the right y-axis?

18 Ansichten (letzte 30 Tage)
When opening the exported .png file I see that MATLAB has added or extended the graph beyond the right y-axis. I want to plot several shorter time series of a long temperature history and use xlimit to achieve my wanted time window. When exporting the figure as a svg or eps it is gone but then other information go missing.
How can I eliminate the 'extra' graph on the right side for my .png?
  2 Kommentare
Vinayak Choyyan
Vinayak Choyyan am 11 Apr. 2023
Hi Tom,
This is not an expected behaviour. Please share your code and data with us so that we can investigate the issue.
Alternatively, you can create a technical service request at http://www.mathworks.com/support/servicerequests/index.html and send us the above details over the request.
Tom Kristian Hoffmann
Tom Kristian Hoffmann am 12 Apr. 2023
Hi,
you can find the code here and a hooksiel.mat file that includes all important data. The HOOKSIEL_201910_202204.csv is too large (85 MB)
clear all; close all; clear
% Source Info
% Aktualisiert: 10.05.2022 | Aufnahmestart: 10.07.1994 | Aufnahmeende: 01.04.2022
% Bemerkung: Aktuelle Messwerte und Stammdaten: {http://www.pegelonline.wsv.de/gast/stammdaten?pegelnr=9460040}
% Source: Wasserstand (Ganglinie) Mess-Station LEUCHTTURM ALTE WESER, Wasserstraßen-
% und Schifffahrtsverwaltung des Bundes, www.kuestendaten.de, 03.08.2023
% Load Data --> Water Level
cd('C:\Users\root\Desktop\UAV_paper\Results\wetter'); % Path
table_watLev = readtable('HOOKSIEL_201910_202204.csv'); % August 1997 - 30th of April 2022, Water Temperature
load('hooksiel.mat')
date_int.Format = 'yyyyMMddHHmm';
dt = datetime(date_int, 'InputFormat', 'dd-MM-yyyy HH:mm:ss');
dt.Format = 'yyyyMMddHHmm';
formatOut= 'yyyy mmmm';
MThw = 664 -504;
MTnw = 329 -504;
stormTide = MThw*1.5; % According to BSH (Gerber et al (2016))
rH_LUFI = -10.2;
rH_ceiling = 9.3;% 55% AIR EXPOSURE
rH_bottom = -111; % Lower boundary of oyster occurence
%% Weather Data
cd('C:\Users\root\Desktop\UAV_paper\Results\wetter'); % Path
airTemp_Table = readtable('Wangerland_Airtemp.txt'); % Air temperature between August 2017 April 2022
windSpeed = readtable('Bremerhaven_Windspeed_20190901_20220531'); % 1st September 2019 - 31st of May 2022. 6 hour
AirTemp = airTemp_Table.Wert;
air_dt = datetime(airTemp_Table.Zeitstempel, 'InputFormat', 'yyyy-MM-ddTHH:mm:ss');
air_dt.Format = 'yyyyMMddHHmm';
formatOut= 'yyyy mmmm';
%% Water temperature
watTemp = readtable('bhv_alte_weser_leuchtturm!Wassertemperatur'); % August 1997 - 30th of April 2022, Water Temperature
temp = watTemp.WassertemperaturIn__C_;
temp(end-1:end) = [];
date_int =(watTemp.Datum_Uhrzeit);
date_int.Format = 'yyyyMMddHHmm';
wat_dt = datetime(watTemp.Datum_Uhrzeit, 'InputFormat', 'yyyy-MM-dd HH:mm:ss');
wat_dt (end-1:end) = [];
wat_dt.Format = 'yyyyMMddHHmm';
%% Plotten
figure()
yyaxis right
hWater=line(wat_dt , temp, 'Color','#696969','LineWidth',2) % Water temperature
hWater=line(wat_dt , temp, 'Color','#696969','LineWidth',2) % Water temperature
% hWater.Color(4)=0.5; % Transparency
hWater.LineStyle ='-';
hold on
hAir=line(air_dt, AirTemp, 'Color','#000000','LineWidth',2); % Air Temperature
% hAir.Color(4)=0.5; % Transparency
% uistack(htemp)
ax = gca;%ax.YAxis;
ax.YColor = '#000000'
ax.FontSize = 20;
ylabel('Temperature [C°]','fontsize',34,'FontWeight','normal')%,'Color','k')
ylim([-40,40])
yticks([-40:10:40])%
xlabel('Time [yyyy mm]','fontsize',34)
monthStart=datestr(dt(1), formatOut);
monthEnd=datestr(dt(end), formatOut);
% title(['Air Temperature from ',monthStart,' to ',monthEnd,', Bremerhaven.'])
grid
xtickformat('yyyy MMM')
xticks(datetime('01-Aug-2017') : calmonths(6): datetime('30-June-2022')) %
minSpawningTemp =18; % Minimum temperature for spawning
coldWaterTemp = 2; % Cold Water limit
hxl = yline(minSpawningTemp,'LineStyle','--','Label','18°','Color','#696969','LabelHorizontalAlignment','right','LineWidth',4); % 'LabelVerticalAlignment','middle',
hxl.FontSize = 34;
% xlim([t(1)- calmonths(9) t(end)+calmonths(9)]) % adding space to left and right --> +/- calmonths
yyaxis left
pwatLev=plot(dt, watLev, 'Color','#295e8d','LineWidth',2) % Plot Water Level
hold on
pwatLev.Color(4)=0.4; % Transparency
pwatLev.LineStyle ='-';
ax = gca;%ax.YAxis;
ax.YColor = '#295e8d'
xlabel('Time [yyyy mm]','fontsize',34)
monthStart=datestr(dt(1), formatOut);
monthEnd=datestr(dt(end), formatOut);
grid
xtickformat('yyyy MMM')
% xticks(datetime('01-Jan-2020') : calmonths(6): datetime('31-July-2022')) %
xticks(datetime('01-Jan-2020') : calmonths(3): datetime('31-July-2022')) %
xticks; % for measurement events
xlim([datetime('01-Jan-2020') datetime('31-March-2022')])
ylim([-400,400])
yticks([-400:100:400])%
ytickformat('%,g');
ylabel('Water Level [m NHN]','fontsize',34,'FontWeight','normal')%,'Color')%,'k')
set(gca, 'TickDir', 'out');
set(gca, 'YGrid', 'on', 'XGrid','off');
box off
%% Inundation
% Bestimmung der Positionen des up- und down-crossings (externe Funktion)
lvl_airExp = rH_LUFI; %
lvl_airExp = -36.8;
zeroShift = watLev(:,1)-lvl_airExp;
[upcrossings, downcrossings]=zerocross_position(zeroShift); % Shifting the zero level down to lvl_airExp
time_Upcrossing= dt(upcrossings);
time_Downcrossing = dt(downcrossings);
duration.up = upcrossings;
duration.down = downcrossings;
i=1;
for i=1:length(upcrossings)
duration.diff(i,:)=diff([dt(upcrossings(i)), dt(downcrossings(i+1))]);
if i>=length(upcrossings)-1
break
end
end
[sortedDiff, ind]=sort(duration.diff, 'descend');
i=1;
for i=1:length(sortedDiff)
if hours(sortedDiff(i)) >= 10
inundation(i,1) = duration.up(ind(i)); % Index for upcrossing
inundation(i,2) = duration.down(ind(i)+1); % Index for downcrossing
inundationTime(i,1) = time_Upcrossing(ind(i)); % Start Time when inundated
inundationTime(i,2) = time_Downcrossing(ind(i)+1); % End Time Time when inundated
else
break
end
end
date_limit = datetime((['2021-01-01 00:00:01']), 'InputFormat', 'yyyy-MM-dd HH:mm:ss'); % Limiting the window of observation
k=1;
y=[0,0];
i=1;
for i=1:length(inundationTime(:,1))
x=[inundationTime(i,1), inundationTime(i,2)]';
% hline=line(x,y,'Color','#e54f4c','LineStyle','--','LineWidth',100)
hline=line(x,y,'Color','#f1a340','LineStyle','-','LineWidth',500);
if inundationTime(i,1) > date_limit
inun_Duration.start(k) = inundationTime(i,1);
inun_Duration.end(k) = inundationTime(i,2);
inun_Duration.time(k) = between(datetime(inundationTime(i,1),'Format', 'dd-MMM-yyyy HH:mm:ss'), datetime(inundationTime(i,2),'Format', 'dd-MMM-yyyy HH:mm:ss'), 'Time'); % Duration of the inundation after 01-Jan-2021
k=k+1;
end
end
%% Air exposure time
i=1;
for i=1:length(upcrossings)
duration.diffExp(i,:) = diff([dt(downcrossings(i)), dt(upcrossings(i))]);
if i>=length(upcrossings)-1
break
end
end
[sortedDiffExp, indExp]=sort(duration.diffExp, 'descend');
i=1;
for i=1:length(sortedDiffExp)
if hours(sortedDiffExp(i)) >= 10
inundationExp(i,1) = duration.down(indExp(i));
inundationExp(i,2) = duration.up(indExp(i));
inundationExpTime(i,1) = time_Downcrossing(indExp(i));
inundationExpTime(i,2) = time_Upcrossing(indExp(i));
else
break
end
end
k=1;
i=1;
for i=1:length(inundationExpTime(:,1))
% figure()
xExp=[inundationExpTime(i,1), inundationExpTime(i,2)]';
% hline=line(x,y,'Color','#e54f4c','LineStyle','--','LineWidth',100)
hline2=line(xExp,y,'Color','#925CA8','LineStyle','-','LineWidth',500);
if inundationExpTime(i,1) > date_limit
exp_Duration.start(k) = inundationExpTime(i,1);
exp_Duration.end(k) = inundationExpTime(i,2);
exp_Duration.time(k) = between(datetime(inundationExpTime(i,1),'Format', 'dd-MMM-yyyy HH:mm:ss'), datetime(inundationExpTime(i,2),'Format', 'dd-MMM-yyyy HH:mm:ss'), 'Time'); % Duration of the inundation after 01-Jan-2021
k=k+1;
end
end
%% Storm events
table_storm = readtable('StormNIce.txt');
table_storm.Day = num2str(table_storm.Day, '%02.0f');
table_storm.Month=num2str(table_storm.Month, '%02.0f');
table_storm.Year=num2str(table_storm.Year);
stormDate = str2num([table_storm.Year table_storm.Month table_storm.Day]);
tStorm = datetime(stormDate,'ConvertFrom','yyyymmdd');
% set(lgd,'AutoUpdate','off')
pStorm = plot(tStorm, 200, '.r', 'markersize', 28, 'MarkerEdgeColor', 'r', 'MarkerFaceColor',[0.5,0.5,0.5]);
fp = 0;% Freezin point
hxl = yline(fp,'LineStyle','-','Label','0°','Color','#000000','FontSize',20,'LabelHorizontalAlignment','left','LineWidth',2,'LabelVerticalAlignment','bottom')
%% Title and legend
% title('Stressors','fontsize',34)
lgd = legend([hAir hWater pwatLev hline hline2 pStorm(1)], ...
'Air temperature [C°]','Water temperature [C°]' , 'Water level','Inundation > 10 h','Air exposure > 10 h', 'Storm event','', 'Location','southeast');
lgd.FontSize =20;
%% Plots of seasons
xlabel('Time [yyyy mm dd]','fontsize',34);
xtickformat('yyyy MMM dd');
% Winter Season 2020/21
xticks(datetime('01-Jan-2021') : calweeks(2): datetime('30-June-2021'));
xlim([datetime('15-Jan-2021') datetime('25-March-2021')]);
% Winter Season 2021/22
xticks(datetime('01-Jan-2022') : calweeks(2): datetime('30-June-2022'));
xlim([datetime('01-Jan-2022') datetime('25-March-2022')]);
hold off

Melden Sie sich an, um zu kommentieren.

Antworten (1)

David Wilkinson
David Wilkinson am 22 Apr. 2023
Bearbeitet: David Wilkinson am 22 Apr. 2023
Hello,
I just experienced a similar issue using R2023a.
Similar to yourself, I was plotting a figure with a left and right y-axis, using yyaxis left and yyaxis right.
The issue arose when I exported using exportgraphics.
There was a small part of the graph showing outside the main axes, past the right y-axis label.
To remedy this, I did the following:
  • Set axis padding to be consistent for both the yyaxis left and yyaxis right plots (I used "axis tight")
  • Set the x limit accordingly after the above line, for each plot
yyaxis left
plot(your_data)
axis tight
xlim([0 1])
yyaxis right
plot(your_other_data)
axis tight
xlim([-4 12])
exportgraphics(gcf,"test.png")
I hope this fixes your issue.
All the best,
David
Edit: Whilst I thought this fixed it, it appears having any xlim on a dual y-axis plot causes the issue...
  3 Kommentare
David Wilkinson
David Wilkinson am 26 Apr. 2023
Interestingly, I am seeing the issue with just
exportgraphics(gcf,"test.png")
There doesn't seem to be an explainable pattern here...
David Wilkinson
David Wilkinson am 26 Apr. 2023
This seems to be a recurrent issue. The linked forum states there's no practical workaround (without decreasing resolution).

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by