Skip to content

Instantly share code, notes, and snippets.

@yichao0319
Last active April 4, 2022 14:42
Show Gist options
  • Save yichao0319/8140972 to your computer and use it in GitHub Desktop.
Save yichao0319/8140972 to your computer and use it in GitHub Desktop.
matlab:plot
clf;
fh = figure;
fontSize = 28;
%colors = {'r', 'b', [0 0.8 0], 'm', [1 0.85 0], [0 0 0.47], [0.45 0.17 0.48], 'k'};
colors = {[228 26 28]/255, [55 126 184]/255, [77,175,74]/255, [152,78,163]/255, [255,127,0]/255, [255,255,51]/255, [166,86,40]/255, [247,129,191]/255, [153,153,153]/255};
lines = {'-', '--', '-.', ':'};
markers = {'+', 'o', '*', '.', 'x', 's', 'd', '^', '>', '<', 'p', 'h'};
%% line
lh1 = plot(ranks, mses);
%% color : r|g|b|c|m|y|k|w|[.49 1 .63]
set(lh1, 'Color', colors{mod(lc-1,length(colors))+1});
%% line : -|--|:|-.
set(lh1, 'LineStyle', char(lines{mod(lc-1,length(lines))+1}));
set(lh1, 'LineWidth', 4);
%% marker: +|o|*|.|x|s|d|^|>|<|p|h
set(lh1, 'marker', markers{mod(lc-1,length(markers))+1});
set(lh1, 'MarkerEdgeColor', 'none');
set(lh1, 'MarkerFaceColor', [0.8 0 0]);
set(lh1, 'MarkerSize', 10);
hold on;
%% bar
bh1 = bar(ranks, ratios);
set(bh1, 'BarWidth', 0.6);
set(bh1, 'EdgeColor', 'none'); %% color : r|g|b|c|m|y|k|w|[.49 1 .63]
set(bh1, 'FaceColor', 'g');
set(bh1, 'LineStyle', '-'); %% line : -|--|:|-.
set(bh1, 'LineWidth', 2);
hold on;
%% figure
set(fh, 'PaperUnits', 'points');
set(fh, 'PaperPosition', [0 0 1024 768]);
%% title
set(gca, 'FontSize', fontSize);
title('TITLE', 'Color', 'r');
xlabel('XLABEL', 'FontSize', fontSize)
ylabel('YLABEL', 'FontSize', fontSize)
%% axis
%% -Inf and Inf for automatic value
set(gca, 'XLim', [XMIN XMAX]);
set(gca, 'YLim', [YMIN YMAX]);
set(gca, 'XTick', [TICKS]);
set(gca, 'YTick', [TICKS]);
set(gca, 'XTickLabel', 'LABEL1|LABEL2|...'); %% labels which map to TICKS, separated by |
set(gca, 'YTickLabel', 'LABEL1|LABEL2|...');
%% rotate x tick
XTickLabel = get(gca, 'XTickLabel');
set(gca, 'XTickLabel', ' ');
XTick = get(gca, 'XTick');
y = repmat(-0.1, length(XTick), 1) + 0.03;
fs = get(gca, 'fontsize');
hText = text(XTick, y, XTickLabel, 'fontsize', fs);
set(hText, 'Rotation', -45, 'HorizontalAlignment', 'left');
%% format
% xtics = get(gca,'XTick')
% set(gca,'XTickLabel',sprintf('%3.4f|',xtics))
xtickformat('$%,.0f')
xtickformat('%g GHz')
xtickformat('MM-dd')
xtickformat('mm:ss')
ax = gca;
ax.YAxis.Exponent = 2;
set(gca, 'OuterPosition', [LEFT BOTTOM WIDTH HEIGHT]); %% normalized value, [0 0 1 1] in default
set(gca, 'Position', [LEFT BOTTOM WIDTH HEIGHT]);
set(gca, 'XScale', 'linear|log')
%% North | NorthEast | NorthOutside | Best | BestOutside
h = legend(legends, 'Location','EastOutside');
h = legend([lh1, lh2, lh3], {'line1', 'line2', 'line3'});
h = legend('boxoff')
set(h, 'Interpreter', 'latex');
leg_pos = get(h, 'position');
set(h, 'position',[leg_pos(1)*0.95,leg_pos(2),...
leg_pos(3)*1.1,leg_pos(4)]);
legendmarkeradjust(marksize, pos_shift)
columnlegend(3, legends, 'Location', 'NorthOutside', 'Orientation', 'Horizontal');
pos = [0.1 0.1 0.8 0.7];
set(gca, 'Position', pos);
waitforbuttonpress
%% save as a file
% print(fh, '-dpsc', 'name.ps');
print(fh, '-dpng', 'name.png');
function legendmarkeradjust(marksize, pos_shift)
s=get(legend);
s1=s.Children;
s2=[];
s2=findobj(s1,{'type','patch','-or','type','line'});
for m=1:length(s2)
set(s2(m),'markersize',marksize);
if length(get(s2(m),'XData')) == 1
set(s2(m),'XData', pos_shift/2);
end
end
s2 = findobj(s1,{'type','text'});
for m=1:length(s2)
pos = get(s2(m),'Position');
set(s2(m), 'Position', pos + [pos_shift 0, 0]);
% set(s2(m),'markersize',marksize);
% if length(get(s2(m),'XData')) == 1
% set(s2(m),'XData',0.1);
% end
end
s2=findobj(s1,{'type','line'});
for m=1:length(s2)
if length(get(s2(m),'XData')) == 2
set(s2(m),'XData', [0 pos_shift]);
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment