I'd like to give my graph background color based on a string value
17 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Anat Lazikin
le 17 Avr 2023
Réponse apportée : Image Analyst
le 17 Avr 2023
Hello,
I have a table, out of which I extracted three arrays: time, AvePressure and markers. I'd like to plot AvePressure over time, and i want to give the graph different background colors based on the value of markers (some are strings while others are empty. In total, there are 10 types of markers).
Thanks in advance!
0 commentaires
Réponse acceptée
DGM
le 17 Avr 2023
Modifié(e) : DGM
le 17 Avr 2023
Here's one way using basic colormapping and an image object as an underlay.
% fake data
x = linspace(0,1,100);
y = [x.^2; x.^1.5; x];
classes = [repmat({'a'},[1 25]),repmat({[]},[1 5]), ...
repmat({'b'},[1 25]),repmat({[]},[1 10]), ...
repmat({'c'},[1 25]),repmat({[]},[1 10])];
% define the mapping
classnames = {'a','b','c'};
classcolor = jet(numel(classnames)); % or any other color table
classcolor = [1 1 1; classcolor]; % one extra color is needed for the empty case
% find the classes and create color stripe
mask = cellfun(@isnumeric,classes); % "empty" might mean either [] or '' or ""
classes(mask) = {''}; % but in order to compare against strings/char, it can't be []
[~,idx] = ismember(classes,classnames); % create index array
bgpict = ind2rgb(uint8(idx),classcolor); % convert to an image stripe
% plot things
plot(x,y); hold on % plot the things
extents = [xlim; ylim]; % store the calculated axes extents
% draw the image underlay
hi = image(extents(1,:),extents(2,:),bgpict,'alphadata',0.2);
uistack(hi,'bottom') % move to background
xlim(extents(1,:)) % restore previous extents
ylim(extents(2,:))
0 commentaires
Plus de réponses (2)
Mandar
le 17 Avr 2023
Modifié(e) : Image Analyst
le 17 Avr 2023
I understand you want to change the background colour of the graph. Refer the link below to know more about changing the background colour of the graph.
0 commentaires
Image Analyst
le 17 Avr 2023
See this demo that let's you change virtually anything about the graph:
% Demo to make a black graph with red Y axis, green X axis, and yellow grid. Markers are magenta with green lines between them.
% Initialization steps:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 24;
% Create sample data.
X = 1 : 20;
Y = rand(1, 20);
% Plot green lines between the markers.
plot(X, Y, 'g-', 'LineWidth', 3);
hold on;
% Plot magenta markers.
plot(X, Y, 'ms', 'LineWidth', 3, 'MarkerSize', 15);
grid on;
title('Y vs. X, Font Size 20', 'FontSize', 20, 'Color', 'b', 'FontWeight', 'bold');
% Make labels for the two axes.
xlabel('X Axis, Font Size 18');
ylabel('Y axis, Font Size 24');
yticks(0 : 0.2 : 1);
% Get handle to current axes.
ax = gca
% Now let's have fun changing all kinds of things!
% This sets background color to black.
ax.Color = 'k'
ax.YColor = 'r';
% Make the x axis dark green.
darkGreen = [0, 0.6, 0];
ax.XColor = darkGreen;
% Make the grid color yellow.
ax.GridColor = 'y';
ax.GridAlpha = 0.9; % Set's transparency of the grid.
% Set x and y font sizes.
ax.XAxis.FontSize = 18;
ax.YAxis.FontSize = 24;
% Make the axes tick marks and bounding box be really thick.
ax.LineWidth = 3;
% Let's have the tick marks go outside the graph instead of poking inwards
ax.TickDir = 'out';
% The below would set everything: title, x axis, y axis, and tick mark label font sizes.
% ax.FontSize = 34;
% Bold all labels.
ax.FontWeight = 'bold';
hold off
% Now do stuff with the figure, as opposed to the axes control that is ON the figure.
% Maximize the figure
g = gcf; % Get handle to the current figure.
g.WindowState = 'maximized'; % Make it full screen.
g.Name = 'Demo by Image Analyst'; % Put a custom string into the titlebar.
g.NumberTitle = 'off'; % Don't have it put "Figure 1" before the name.
g.MenuBar = 'figure'; % or 'none'
g.ToolBar = 'figure'; % or 'none'
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1358838/image.png)
0 commentaires
Voir également
Catégories
En savoir plus sur Specifying Target for Graphics Output dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!