How do I make a plot background black?

조회 수: 1,317 (최근 30일)
Bob
Bob 2014년 12월 15일
편집: Voss 2023년 12월 20일
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?

채택된 답변

Star Strider
Star Strider 2014년 12월 15일
This works:
plot(x,y)
set(gca,'Color','k')
  댓글 수: 4
Voss
Voss 2023년 12월 20일
편집: Voss 2023년 12월 20일
@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 2023년 12월 20일
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';
.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 12월 15일
편집: Azzi Abdelmalek 2014년 12월 15일
x=[1 2 3 4 5];
y=[10 20 30 40 50];
plot(x,y)
set(gca,'color',[0 0 0])
  댓글 수: 2
Andyzhuang
Andyzhuang 2018년 8월 30일
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 2018년 8월 31일
Using set(gca, 'color', 'k') worked fine in a test I just did.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2023년 12월 20일
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'

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by