How do I make a plot background black?

1,268 views (last 30 days)
Bob
Bob on 15 Dec 2014
Edited: Voss on 20 Dec 2023
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?

Accepted Answer

Star Strider
Star Strider on 15 Dec 2014
This works:
plot(x,y)
set(gca,'Color','k')
  4 Comments
Voss
Voss on 20 Dec 2023
Edited: Voss on 20 Dec 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.
Star Strider
Star Strider on 20 Dec 2023
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';
.

Sign in to comment.

More Answers (2)

Azzi Abdelmalek
Azzi Abdelmalek on 15 Dec 2014
Edited: Azzi Abdelmalek on 15 Dec 2014
x=[1 2 3 4 5];
y=[10 20 30 40 50];
plot(x,y)
set(gca,'color',[0 0 0])
  2 Comments
Andyzhuang
Andyzhuang on 30 Aug 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 on 31 Aug 2018
Using set(gca, 'color', 'k') worked fine in a test I just did.

Sign in to comment.


Image Analyst
Image Analyst on 20 Dec 2023
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'

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!