How to make 2D plot of points with different colors?

In summary, the conversation discusses the process of creating a 2D plot of points with different coordinates and colors based on a separate variable. The code involves creating column vectors for the coordinates and the variable, and using a for loop to assign colors to the points. However, the code does not work and the suggested modification is to use the scatter command instead of plot. This successfully creates a plot with different colored points.
  • #1
kelvin490
Gold Member
228
3
I want to make a 2D plot of points with different x, y coordinates and have colors depending on a separate variable. I have make column vectors for x and y coordinates and another column containing 1 or -1. I would like to represent the points with 1 as red and -1 as blue points. I have codes as follow:
Matlab:
x_dis=rho_rec(1:nDis,xCol,step);%x coordinates
y_dis=rho_rec(1:nDis,yCol,step); %y coordinates
bv=rho_rec(1:nDis,bvCol,step); % 1 or -1

for i=1:1:nDis
   if bv(i)==1
        dis_color(i,1:3)=[0 0 1]; %blue
    elseif bv(i)==-1
        dis_color(i,1:3)=[1 0 0]; %red
    end
end

plot(x_dis,y_dis,'.','Color',dis_color(1:nDis,:))

However it doesn't work. How should I modify the code? Thank you.
 
Last edited by a moderator:
Physics news on Phys.org
  • #3
I have changed to use scatter instead of plot, as suggested by other
So the command becomes:

scatter(x_dis,y_dis,[],dis_color(1:nDis,:),'.')

It works like a charm
 

1. How do I plot 2D points with different colors using a programming language like Python?

In order to plot 2D points with different colors using Python, you can use a library like matplotlib. First, import the library and then use the scatter function to plot your points. You can specify the colors using the c parameter and passing in a list of colors corresponding to each point.

2. Can I plot 2D points with different colors without using a programming language?

Yes, you can use a variety of online graphing tools or software such as Microsoft Excel to plot 2D points with different colors. These programs usually have options to customize the colors of your data points.

3. How do I determine what colors to use for my 2D plot?

There are a few different approaches you can take when choosing colors for your 2D plot. One option is to use a color palette generator, which can provide you with a range of colors that work well together. Another option is to use a color map, which assigns colors to your points based on a numerical value. You can also manually select colors based on their visual appeal or to represent different categories of data.

4. Is it possible to add a legend to my 2D plot with different colored points?

Yes, you can add a legend to your 2D plot by specifying a label for each set of points and then using the legend function. Make sure to also specify the location for the legend to appear. This can be helpful for identifying which colors correspond to which data points or categories.

5. Can I change the shape of my points in a 2D plot with different colors?

Yes, you can change the shape of your points by using the marker parameter in your plot function. You can choose from a variety of shapes such as circles, squares, triangles, and more. You can also customize the size of the markers to make them larger or smaller depending on your preference.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
6K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
575
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
960
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
Back
Top