Saving Pcolor/ImageSC as vector graphics with editable colors

14 views (last 30 days)
Hello!
I am trying to output either a pcolor or imagesc plot as vector graphics, so that I can retroactively change the colors used. However, whenever I output either, the colors themselves come out as one large image (non-vector graphics) that can not be edited easily, rather than many small reactangles as I would have expected from a vector graphics output. In the case of pcolor specifically, the grid is made with vector graphics, but the colors are an independent layer that is one large image. When I place the output PDF into a program such as Adobe Illustrator, I am unable to edit the colors of the file as I would with a typical vector graphics output such as a "scatter" or "plot" figure.
Any help with this would be greatly appreciated!
I have listed a piece of example code generating and saving such plots below:
Count=50;
x=linspace(1,Count,Count);
y=linspace(1,Count,Count);
z=rand(Count);
mymap = [1 0 0; 0 1 0; 0 0 1; 0 0 0];
figure(1)
set(gcf,'renderer','painters');
colormap(mymap)
imagesc(x,y,z)
set(gca,'YDir','normal')
filepath = uigetdir(matlabroot,'Save Plot Location');
fullFilePathPDF=string(filepath)+'\TestPlot1.pdf';
exportgraphics(gcf,fullFilePathPDF,'ContentType','vector')
figure(2)
set(gcf,'renderer','painters');
colormap(mymap)
pcolor(z)
filepath = uigetdir(matlabroot,'Save Plot Location');
fullFilePathPDF=string(filepath)+'\TestPlot2.pdf';
exportgraphics(gcf,fullFilePathPDF,'ContentType','vector')

Accepted Answer

Abhinav Aravindan
Abhinav Aravindan on 22 Aug 2024
Hi Daniel
I understand that you are trying to export “pcolor” and “imagesc” plots as Vector Graphics file but facing trouble in doing so. As a workaround, you may try plotting each matrix value separately using “rectangle” and export it using “print” as follows:
% Parameters
Count = 10;
x = linspace(1, Count, Count);
y = linspace(1, Count, Count);
z = rand(Count);
mymap = [1 0 0; 0 1 0; 0 0 1; 0 0 0]; % Red, Green, Blue, Black
% Create a figure
figure;
hold on;
colormap(mymap);
% Get the number of colors in the colormap
numColors = size(mymap, 1);
% Determine the range of the matrix
minVal = min(z(:));
maxVal = max(z(:));
% Loop through each element of the matrix and draw a rectangle
for i = 1:Count
for j = 1:Count
% Define color based on matrix value
colorIndex = round((z(i, j) - minVal) / (maxVal - minVal) * (numColors - 1)) + 1;
colorIndex = min(max(colorIndex, 1), numColors);
color = mymap(colorIndex, :);
% Draw rectangle with the specified color
rectangle('Position', [j-1, i-1, 1, 1], 'FaceColor', color, 'EdgeColor', 'none');
end
end
% Set axis properties
axis equal;
axis tight;
axis off;
set(gca, 'YDir', 'reverse');
% Save the figure as a vector graphics file
print('-vector','-dsvg','TestPlot')
Please find below the relevant documentation:
I hope this helps resolve your issue!

More Answers (0)

Categories

Find more on Data Exploration in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!