How do I make a plot background black?

I have two vectors.
x=[1 2 3 4 5];
y=[10 20 30 40 50];
I plotted the vectors
plot(x,y)
The default background of the plot is white. How do I change the plot background to black?

 Respuesta aceptada

Star Strider
Star Strider el 15 de Dic. de 2014
This works:
plot(x,y)
set(gca,'Color','k')

4 comentarios

Walter Roberson
Walter Roberson el 29 de Abr. de 2018
Side note, because I just ran into this:
The axes 'color' property only has effect if the axes 'visible' property is set 'on'.
In particular if you set the axes visible 'off' in order to hide the box and ticks and tick labels, then the axes background color will not be drawn.
Markus
Markus el 20 de Dic. de 2023
Did you find a solution ? Maybe i can plot a square into the background, but i have no idea how to acomplish that :)
Voss
Voss el 20 de Dic. de 2023
Editada: Voss el 20 de Dic. de 2023
@Markus: Do you mean a solution where the axes is invisible? If so, then something like this will work:
x=[1 2 3 4 5];
y=[10 20 30 40 50];
h = plot(x,y);
xl = xlim();
yl = ylim();
p = patch(xl([1 2 2 1 1]),yl([1 1 2 2 1]),'k');
set(gca(),'Children',[h;p],'Visible','off')
On the other hand, if you want the axes to be visible, then you just have to set its color as in the Accepted Answer.
The original approach still works.
x = (0:9).';
y = [sin(2*pi*x/9) cos(2*pi*x/9)];
figure
plot(x, y, 'LineWidth',2.5)
Ax = gca;
Ax.Color = 'k';
.

Iniciar sesión para comentar.

Más respuestas (2)

Azzi Abdelmalek
Azzi Abdelmalek el 15 de Dic. de 2014
Editada: Azzi Abdelmalek el 15 de Dic. de 2014
x=[1 2 3 4 5];
y=[10 20 30 40 50];
plot(x,y)
set(gca,'color',[0 0 0])

2 comentarios

Andyzhuang
Andyzhuang el 30 de Ag. de 2018
how to set quiver background color black? Hi, I use quiver function to figure vector. I want to set the background black and vector in green. Please how to set the background color? I have tried to use set(gca,'color','k'), but it doesn't work. Any comments and advise are very appreciated.
Walter Roberson
Walter Roberson el 31 de Ag. de 2018
Using set(gca, 'color', 'k') worked fine in a test I just did.

Iniciar sesión para comentar.

See this demo to show you how you can change just about everything by referring to it properly.
% 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
ax =
Axes (Y vs. X, Font Size 20) with properties: XLim: [0 20] YLim: [0 0.9] XScale: 'linear' YScale: 'linear' GridLineStyle: '-' Position: [0.13 0.11 0.775 0.796812954621955] Units: 'normalized' Use GET to show all properties
% 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'

Categorías

Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.

Preguntada:

Bob
el 15 de Dic. de 2014

Editada:

el 20 de Dic. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by