changing background color for plot

28 visualizzazioni (ultimi 30 giorni)
TZ
TZ il 14 Gen 2016
Modificato: Star Strider il 14 Gen 2016
hi, i am plotting a graph of machine parameters over time. the machine has 5 working modes and i want the background color to change according to a parameter i will define that will express the temporary machine state. for example for time vector from 1 to 100 white, then from 101 to 153 blue etc. is this possible on matlab? thanks.

Risposte (1)

Star Strider
Star Strider il 14 Gen 2016
Modificato: Star Strider il 14 Gen 2016
The patch function will do what you want:
x = linspace(1, 300, 300); % Create Data
y = randi([20 80], 1, 300);
figure(1)
patch([1 100 100 1], [0 0 99 99], 'w')
hold on
p2 = patch([100 153 153 100], [0 0 99 99], 'b')
plot(x, y, '-k')
hold off
axis([1 300 0 99])
set(p2, 'FaceAlpha', 0.5) % Adjust Patch Transparency
The one caveat is to plot the patches first regardless of the transparency or other settings, and plot the data last. This plots the data on top of the patches:

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by