Effacer les filtres
Effacer les filtres

How do I make a plot background black?

1 166 vues (au cours des 30 derniers jours)
Bob
Bob le 15 Déc 2014
Modifié(e) : Voss le 20 Déc 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?

Réponse acceptée

Star Strider
Star Strider le 15 Déc 2014
This works:
plot(x,y)
set(gca,'Color','k')
  4 commentaires
Voss
Voss le 20 Déc 2023
Modifié(e) : Voss le 20 Déc 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 le 20 Déc 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';
.

Connectez-vous pour commenter.

Plus de réponses (2)

Azzi Abdelmalek
Azzi Abdelmalek le 15 Déc 2014
Modifié(e) : Azzi Abdelmalek le 15 Déc 2014
x=[1 2 3 4 5];
y=[10 20 30 40 50];
plot(x,y)
set(gca,'color',[0 0 0])
  2 commentaires
Andyzhuang
Andyzhuang le 30 Août 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 le 31 Août 2018
Using set(gca, 'color', 'k') worked fine in a test I just did.

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 20 Déc 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'

Community Treasure Hunt

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

Start Hunting!

Translated by