Plot command changing the graph background and grid line colours in GUIDE

51 views (last 30 days)
Hi.
Im using GUIDE and have several axes components that I use to display graphs. One of them i want to have a dark background. I noticed when I set the background colour to my choice in the property inspector, when the GUI ran, it was correct. But as soon as I plotted the background went white.
so I tried to put the following in the opening function:
ax=axes(handles.axes1)
set(ax,'Color',[0.15 0.15 0.15])
grid on
ax.GridColor=[1 1 1];
ax.GridAlpha=0.5;
drawnow;
But again, when I plot my grapg (from a push button callback), it again turns back to default white.
Is there a way to stop the plot command changing the graph background and grid line colours?Is there a way to stop the plot command changing the graph background and grid line colours?

Accepted Answer

Walter Roberson
Walter Roberson on 7 Jan 2020
In MATLAB, when you use any "high-level" plotting command, MATLAB checks to see if you have specifically said that you want to add new graphics on top of existing graphics. If you have not indicated that, then if there is an existing axes, MATLAB clears the axes properties -- including properties such as background colors. For example if you do
ax=axes(handles.axes1);
plot(ax, [1 2 3], [3 2 1]);
pause(3)
plot(ax, 2, 2, 'r*');
then this would draw a diagonal line, wait three seconds, then clear the axes and then draw a single point at 2,2 . The above code does not draw a diagonal line, wait three seconds, and then add a red star to the existing plot.
In order to tell MATLAB that you want new graphics to go on top of existing graphics (that is, that you do not want the entire axes cleared), use hold on. For example,
ax=axes(handles.axes1);
set(ax,'Color',[0.15 0.15 0.15])
hold(ax, 'on')
plot(ax, [1 2 3], [3 2 1]);
pause(3)
plot(ax, 2, 2, 'r*');
hold(ax, 'off')
This sets the background color, then tells MATLAB that you want later drawing to go on top of existing drawing. Then it draws the diagonal line, waits three seconds, does not clear the axes, and adds a red * on top of the line. Then it tells MATLAB that unless it is told otherwise, it is okay for the next high level graphic operation to clear the axes.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!