필터 지우기
필터 지우기

Plot line is thick in the middle on a log-log scale

조회 수: 2 (최근 30일)
Anshuman Pal
Anshuman Pal 2021년 7월 2일
댓글: Anshuman Pal 2021년 7월 3일
Hello,
I scatter() a bunch of points, on which I plot a black linear regression line calculated using polyfit() and polyeval(). On a linear scale, the line looks normal. But as soon as I turn on logscale using set(gca, 'xscale','log','yscale','log), the line becomes thick in the middle, as following:
What is going on? Could someone please help me? Closer inspection suggests that there is a smaller second line being drawn as well, which makes the main line look thick. Why?
  댓글 수: 3
Jonas
Jonas 2021년 7월 2일
which renderer did you choose for plotting? you max try the 'painters' renderer
Scott MacKenzie
Scott MacKenzie 2021년 7월 2일
I just tried to duplicate your plot and I didn't get a thick line. Perhaps provide code (and data) that can be executed to create the problem you note.

댓글을 달려면 로그인하십시오.

채택된 답변

Cris LaPierre
Cris LaPierre 2021년 7월 2일
편집: Cris LaPierre 2021년 7월 2일
I can reproduce what you see in the code below. Since you haven't shared your code, I can't be certain it is the same cause, but I suspect it is.
When you use plot, it connects the points in the order they are listed. If your values are not sorted, it will bounce up and down the line plotting the points. When you switch your xscale, it highlights the subtle incontinuities in plotting the line this way.
A possible confounding issue may be if your data is in a matrix. Since each column is treated as a separate series, you would actually have multiple lines, one for each column. Of course, when you fit a line to your points, you want to fit a single line to all the points. Be sure you are not calculating a fit for each column, which results in multiple fit lines being drawn.
You do have to be mindful that, when you use polyval on all the x values are sorted to avoid the first issue mentioned above.. It may be a good idea to just create your own x vector to be sure your values are what you need.
% Create data set
x = rand(4,3);
y = x+(rand(4,3)-.5)*.1;
% Create plot where lines look normal
scatter(x,y,'filled')
p = polyfit(x,y,1);
yp = polyval(p,x);
hold on
plot(x,yp)
hold off
% Now looking stacked
figure
scatter(x,y,'filled')
hold on
plot(x,yp)
hold off
set(gca, 'xscale','log','yscale','log')
Here's what the figure looks like if I use polyval on an x vector I create that is sorted in ascending order.
% fit a single line to all the points
p = polyfit(x(:),y(:),1);
% create a new vector of x values (ascending order)
xFit = 0:.1:1;
yp = polyval(p,xFit);
figure
scatter(x,y,'filled')
hold on
plot(xFit,yp)
hold off
set(gca, 'xscale','log','yscale','log')
  댓글 수: 2
Anshuman Pal
Anshuman Pal 2021년 7월 3일
Indeed, I think this is the right answer. Unfortunately, my Matlab crashed soon after I posted this question. I will try out your solution as soon as I get Matlab running.
Anshuman Pal
Anshuman Pal 2021년 7월 3일
This was indeed the perfect answer. All I needed to do was sort my data before fitting. Thank you very much! I learnt something useful today.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by