How to smooth out plotted points in a graph/figure?
3 次查看(过去 30 天)
显示 更早的评论
Hello everyone! I have a question concerning the plot. I'm working on a simple tool that plots automatically squares based on a MATLAB code that I wrote. The code is actually working perfectly, and it is able to determine on which coordinates each point should be. The code is very long, so I won't be posting it here (unless a segment of the code is required for my request of course). The thing is that I am plotting these points as "squares" and the code automatically knows exactly which color to choose in every case. It is a very long iteration and after around 10 minutes, I get a figure that looks like the one I just uploaded.
As you can see, each point that is plotted is a "square" with a certain "MarkerSize":
plot(i, j, 's', 'color', colorselected, 'MarkerSize', 11.5, 'MarkerFaceColor', colorselected);
This covers the figure, however it doesn't provide that smoothness that I am looking for. It looks something like an extremely pixelated figure. I am trying to figure out a way to have a sort of dividing diagonal between the parts that the colors start to change. The thing is that if I increase the number of iterations, it can take up to 1 hour of plotting, if not more, and honestly that's just far too long (plotting over 10000 individual points). I did try it out, and it did yield much better results, but obviously it isn't an optimal solution.
I have also tried using the "area" function in MATLAB, with their corresponding color, of course, but it backfired. It did not resemble the graphs that were plotted individually at all! I did follow through with it logically though. I created a method that calculates, checks the value received based on what I require as a value, and then put that value in an array that contains the xy coordinates (so a 2D array). There are a total of 8 colors, thus 8 distinct 2D arrays. What I was always getting, however, are huge square areas/figures completely filled with ONLY ONE COLOR!. Here is a simple example of what I meant:
if(yellowplot~=0)
area([min(yellowplot(1,:)), min(yellowplot(1,:)), max(yellowplot(1,:)), max(yellowplot(1,:))], [min(yellowplot(2,:)), max(yellowplot(2,:)), min(yellowplot(2,:)), max(yellowplot(2,:))], 'FaceColor', 'y');
end
hold on;
if(magentaplot~=0)
area([min(magentaplot(1,:)), min(magentaplot(1,:)), max(magentaplot(1,:)), max(magentaplot(1,:))], [min(magentaplot(2,:)), max(magentaplot(2,:)), min(magentaplot(2,:)), max(magentaplot(2,:))], 'FaceColor', 'm');
end
hold on;
if(cyanplot~=0)
area([min(cyanplot(1,:)), min(cyanplot(1,:)), max(cyanplot(1,:)), max(cyanplot(1,:))], [min(cyanplot(2,:)), max(cyanplot(2,:)), min(cyanplot(2,:)), max(cyanplot(2,:))], 'FaceColor', 'c')
end
hold on;
if(redplot~=0)
area([min(redplot(1,:)), min(redplot(1,:)), max(redplot(1,:)), max(redplot(1,:))], [min(redplot(2,:)), max(redplot(2,:)), min(redplot(2,:)), max(redplot(2,:))], 'FaceColor', 'r')
end
hold on;
if(greenplot~=0)
area([min(greenplot(1,:)), min(greenplot(1,:)), max(greenplot(1,:)), max(greenplot(1,:))], [min(greenplot(2,:)), max(greenplot(2,:)), min(greenplot(2,:)), max(greenplot(2,:))], 'FaceColor', 'g')
end
hold on;
if(blueplot~=0)
area([min(magentaplot(1,:)), min(blueplot(1,:)), max(blueplot(1,:)), max(blueplot(1,:))], [min(blueplot(2,:)), max(blueplot(2,:)), min(blueplot(2,:)), max(blueplot(2,:))], 'FaceColor', 'b')
end
hold on;
if(orangeplot~=0)
area([min(orangeplot(1,:)), min(orangeplot(1,:)), max(orangeplot(1,:)), max(orangeplot(1,:))], [min(orangeplot(2,:)), max(orangeplot(2,:)), min(orangeplot(2,:)), max(orangeplot(2,:))], 'FaceColor', [1 0.270588 0])
end
hold on;
if(blackplot~=0)
area([min(blackplot(1,:)), min(blackplot(1,:)), max(blackplot(1,:)), max(blackplot(1,:))], [min(blackplot(2,:)), max(blackplot(2,:)), min(blackplot(2,:)), max(blackplot(2,:))], 'FaceColor', 'k')
end
hold on;
I even removed the "Hold on" command in one instance to see what is actually being drawn in INDIVIDUAL figures (yes, I called the "Figure" function in every "if" case) and it still just plotted a huge square. I don't know why it didn't properly plot everything properly as it should have (like taking the minimum and maximum points of each array and then filling in the area using these 4 points).
Any help would be greately appreciated! Thank you!
0 个评论
回答(1 个)
Steven Lord
2021-1-27
Rather than creating one line per point I would consider using either scatter or one line per point color.
xy = rand(10, 2);
scatter(xy(:, 1), xy(:, 2), 49, rand(10, 3), 'Marker', 's')
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Line Plots 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!