Borrar filtros
Borrar filtros

How can i fill a marker with color?

1.780 visualizaciones (últimos 30 días)
Philipp Mueller
Philipp Mueller el 26 de Sept. de 2016
Respondida: MathWorks Support Team el 5 de Jun. de 2024
Hi,
I have two plots. How can I fill the marker in plot2? At the moment it is empty. With: plot(x(i),y(i),'s','MarkerFaceColor',colors(ID(i),:));???
Input_Matrix = textread('Rainflow_Input1.txt')
[zeilen,spalten]=size(Input_Matrix)
x = Input_Matrix(:,1)
y = Input_Matrix(:,2)
z = Input_Matrix(:,3)
colorbar('location','Manual', 'position', [0.93 0.1 0.02 0.81]);
a=30
scatter(x,y,a,z,'filled')
colorbar;
%http://stackoverflow.com/questions/15814068/pcolor-in-scatter-plot-matlab
[dummy ID]=sort(z);
colors=colormap(jet(length(z)));
figure
for i=1:length(z)
plot(x(i),y(i),'s','Color',colors(ID(i),:));
hold on
end
  1 comentario
Philipp Mueller
Philipp Mueller el 26 de Sept. de 2016
with -> plot(x(i),y(i),'s','MarkerFaceColor',colors(ID(i),:)); ???

Iniciar sesión para comentar.

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 5 de Jun. de 2024
If you are only plotting markers, and not any lines, you can create a plot with filled markers by calling the "scatter" function with the "filled" option. 
x = rand(1,50);
y = rand(1,50);
colors = jet(numel(x));
scatter(x,y,[],colors,'filled','s')
If you need to use the "plot" function, you can set the "MarkerFaceColor" property. If you want the edges of the markers to match, set the "MarkerEdgeColor" property to the same color values. 
figure
hold on
for i=1:50
plot(x(i),y(i),'s','MarkerFaceColor',colors(i,:),'MarkerEdgeColor',colors(i,:));
end
hold off
Also, you don’t need to call "hold on" at each iteration of the loop. You can call it once just before plotting. Call "hold off" when you’re all done. 
You can find more examples of setting marker colors in the documentation:
Specify Marker Colors in a Scatter Plot
https://www.mathworks.com/help/matlab/creating_plots/specify-plot-colors.html#mw_9ff0ca4d-33b0-4b96-b5b5-0f68092b0211

Más respuestas (3)

Walter Roberson
Walter Roberson el 26 de Sept. de 2016
Yes, you can use MarkerFaceColor to fill markers.
Note that only one 'MarkerFaceColor' will be paid attention to for any plot() call, even if you are requesting to plot multiple items.
As you are not drawing lines, you should consider instead using scatter(). You can specify the color of each point for scatter, and use the 'filled' option.

Forrest Mobley
Forrest Mobley el 19 de Ag. de 2018
It's also doable to just choose whatever predefined color as when choosing what your marker color is.
For example: plot(x,y,'or','MarkerFaceColor','r');
This will give your marker face and marker outline the same color, red.
  2 comentarios
KAE
KAE el 24 de En. de 2019
If you aren't picking the color yourself, but it's getting set by the plot color order, you can still fill it with the same color as the marker edges or line plot as follows,
x = rand(1,5); y = rand(1,5); % Example data
h1 = plot(x, y, '-o'); % Plot a line and circle markers
set(h1, 'markerfacecolor', get(h1, 'color')); % Use same color to fill in markers
Luis Humberto Niño Alvarez
Luis Humberto Niño Alvarez el 3 de Dic. de 2019
Thanks KAE!!!, very useful

Iniciar sesión para comentar.


MathWorks Support Team
MathWorks Support Team el 17 de Mzo. de 2021
If you are only plotting markers, and not any lines, you can create a plot with filled markers by calling the scatter function with the 'filled' option.
scatter(x,y,[],colors,'filled','s')
If you need to use the plot function, you can set the MarkerFaceColor property. If you want the edges of the markers to match, set the MarkerEdgeColor property to the same color values.
figure
hold on
for i=1:length(z)
plot(x(i),y(i),'s','MarkerFaceColor',colors(ID(i),:), ...
'MarkerEdgeColor',colors(ID(i),:));
end
hold off
If you want the markers to have the same color as the axes background color, set the MarkerFaceColor property to 'auto' to fill the markers with that color.
Also, you don’t need to call hold on at each iteration of the loop. You can call it once just before plotting. Call hold off when you’re all done.

Categorías

Más información sobre Scatter Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by