Saving Pcolor/ImageSC as vector graphics with editable colors
15 ビュー (過去 30 日間)
古いコメントを表示
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')
0 件のコメント
採用された回答
Abhinav Aravindan
2024 年 8 月 22 日
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!
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Annotations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!