user2840470
user2840470

Reputation: 969

Matlab fast plotting of points with corresponding color array

I have an array of X/Y data of length N, and a corresponding color array that is Nx3. I am hoping to color the plot of the X/Y data based on the corresponding color in the color array.

A single XY array is >100000 in size and I have lots of the arrays to plot so hoping to find a fast solution for this.

An example of how I am currently doing it in a loop

xy_data = zeros(100000, 2);
colors = zeros(100000, 3);
figure;
hold on
for i = 1:length(xy_data)
    plot(xy_data(i, 1), xy_data(i, 2), '.', 'color', colors(i, :));
end

This technically works but it can be quite slow, especially when there are lots of data points, and lots of xy arrays to plot. I am wondering if

  1. is there a faster way?
  2. Is there a way to plot it as a multi color line, and not markers ('.')

Upvotes: 2

Views: 552

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112659

You can use scatter to plot all points in one go, with a different color for each point. The color values are passed as a fourth argument. The third argument is marker size (which can be fixed, or it can also have a different value for each point):

xy_data = rand(1000, 2);
colors = rand(1000, 3);
scatter(xy_data(:, 1), xy_data(:, 2), 30, colors, '.')

enter image description here

Upvotes: 5

Related Questions