Plot legend does not display any lines

Hi,
I am plotting a simple graph from a matrix, but when I code as below, the legend does not show any lines, but shows legend names only.
plot(kmat,v(:,1))
title('Convergence of Value Functions')
xlabel('k')
ylabel('v(k)')
hold on
plot(kmat,v(:,5))
plot(kmat,v(:,50))
plot(kmat,v(:,1000))
legend('iter=1','iter=5','iter=50','iter=1000')
hold off
"kmat" is N x 1 matrix for x-axis value, and "v" is N x N matrix where i want to plot values on y-axis.
However, as you can see from the plot below, the legend displays its names but doesn't display any corresponding line.
legend_problem.png

13 件のコメント

dpb
dpb 2019 年 10 月 2 日
Also doesn't show outline box that is normal default and is opaque over the axes...looks like you may have set some default parameters or previous properties that are hanging around.
Have you tried closing and restarting MATLAB to see if symptoms go away? If not, I'd suggest giving that a go...
DoubleD
DoubleD 2019 年 10 月 2 日
編集済み: DoubleD 2019 年 10 月 2 日
I restarted the system but it still didn't work.
I also ran a very simple code below but the problem still persists.
clear all
A = [1,2,3;4,5,6;7,8,9];
B = [1,2,3];
plot(B,A(:,1));
hold on
plot(B,A(:,2));
plot(B,A(:,3));
legend('iter=2','iter=3','iter=10')
hold off
I also ran the code in another machine but still have the same problem, so I assume it is not a matter of local machine problem.
One reason I assume is version problem. I use R2019b in both my desktop and laptop and have the same problem. My colleague uses R2018b and she doesn't have the problem with the same code in this comment.
(Or maybe processor problem? I use AMD Ryzen in my machines)
Star Strider
Star Strider 2019 年 10 月 2 日
It’s not related to the Ryzen. I ran your code (with random data) on my Ryzen 7 1800X machine with R2019b and it plotted as I expected it to. I had no problems running your code.
I notice that the box around the legend is also absent. and I do not see anything in your code that would specifically turn it off.
After that plot, including the legend call, add these lines:
hleg = legend;
legprops = get(hleg)
and see if any specific properties are changed that would produce the result you are seeing. (Be sure to not put a semicolon after the get call, so you will see all the properties.) See Legend Properties for a list of them and their default settings.
DoubleD
DoubleD 2019 年 10 月 2 日
I added those two lines after "legend" call but it still did not work.
I also uninstalled Matlab (including manually deleting folder and deleting registries after uninstall) and re-installed it, but it did not work either. I did not change any Matlab settings at all (I do not even know how to do so)
I randomly selected a couple of Legend Properties and most of them (text color, position, etc...) worked well. (The only property didn't work was 'FontSize' option. Whenever I entered any font size manually, no legend showed up at all).
Star Strider
Star Strider 2019 年 10 月 2 日
At this point, I recommend that you click on the Contact Us telephone handset icon in the upper right corner of this page to request Tech Support. Include the URL of this page in your message so you don’t have to repeat everything you’ve already tried.
You have my sympathies!
Alessandro Lavelli
Alessandro Lavelli 2019 年 10 月 3 日
Hi, I have the same problem. I think it is a bug of 2019b version since I tried my script on both 2018b and 2019b and the legend behaves correctly on 2018b and no on 2019b.
darova
darova 2019 年 10 月 3 日
try
opengl software
DoubleD
DoubleD 2019 年 10 月 3 日
opengl software
This code worked!
Thank you @darova. Can you leave it as an answer so I can accept it?
Adam Danz
Adam Danz 2020 年 9 月 6 日
To make the change permanent for future sessions, opengl('save','software')
Star Strider
Star Strider 2020 年 11 月 17 日
This turned out to be a problem with certain AMD graphics drivers.
Updating to the newest AMD graphics driver solves the problem.
Paul Stansell
Paul Stansell 2020 年 11 月 17 日
Could you give more details?
Star Strider
Star Strider 2020 年 11 月 17 日
Paul Stansell —
I have no idea what the problem actually was. I updated the driver (using the ‘Auto-Detect and Install Radeon™ Graphics Drivers for Windows®’ app on the AMD Drivers and Support site) and after that, everything worked as it should, on both my desktop and laptop (both Ryzen+Radeon). There are several posts about this, and in every situation, upgrading the graphics driver solved the problem.
Matthew Fracalanza
Matthew Fracalanza 2021 年 2 月 22 日
編集済み: Matthew Fracalanza 2021 年 2 月 22 日
That worked, Star Strider. Thanks so much.

サインインしてコメントする。

 採用された回答

darova
darova 2019 年 10 月 3 日

2 投票

Please see THIS solution for colorbar error

その他の回答 (4 件)

Benoit Schiltz
Benoit Schiltz 2020 年 9 月 16 日

3 投票

Hi,
I have the same problem which was solved using
opengl software
But doing that makes the lines of the plot pixelized (this effect was not a problem before) displaying very little stairs between almost each step. Is there a way to fix this? I tried using
opengl hardware
Which solved the problem of the smoothness of the curves but then the legend colors and box disappeared.
Thanks

1 件のコメント

Antonio
Antonio 2023 年 5 月 12 日
This works well! I am using R2022b.

サインインしてコメントする。

Paul Stansell
Paul Stansell 2020 年 11 月 17 日
編集済み: Paul Stansell 2020 年 11 月 17 日

1 投票

I have recently had a similar problem. opengl('save','software') did not work for me. Specifying ...'LineWidth',1) did. No idea why! (Actually, a line width >= 0.8 worked)

5 件のコメント

Adam Danz
Adam Danz 2020 年 11 月 17 日
編集済み: Adam Danz 2020 年 11 月 17 日
A LineWidth of 0.601 worked for me in the past when set from the legend function.
Paul Stansell
Paul Stansell 2020 年 11 月 17 日
I've since found that this doesn't work on all plots.
Paul Stansell
Paul Stansell 2020 年 11 月 18 日
Things that make you go mmmm: last night there was a windows update, and this morning the legend lines worked without the linewidth workaround. Also the legend borders are back; I'd not noticed they were missing.
Thankyou microsoft!
christos d
christos d 2021 年 1 月 13 日
I can confirm that the issue is still there in matlab 2020b. But I can also confirm that using exactly the same laptop in other script it displays the dots of the legend the way it should. The script that I noticed it has the issue, happens to be a function file with nested function that provides results running it directly from the function file. I am not sure if it has to do with the matlab type of file or not, probably not.I do have an AMD processor by the way.
Siphiwo Mzekandaba
Siphiwo Mzekandaba 2023 年 10 月 1 日
I am using R2021a, specifying ...'LineWidth', 1) worked for me.

サインインしてコメントする。

Sergio Sanz
Sergio Sanz 2020 年 4 月 3 日

0 投票

I have the same problem and I've found that it only happens when lines are 0.5 width. If you change to 1 You can see both the edge of the legend and the lines of data inside the legend.
But I don't know what's the problem.
That's only a provisional solution.

2 件のコメント

parson fu
parson fu 2020 年 5 月 23 日
I see the same problem too,but in another computer,it works fine.
PAVEL
PAVEL 2022 年 9 月 10 日
It's a really solution, I just want to add it's neccessary to specify 'LineWidth' for each of plots for proper work.

サインインしてコメントする。

Aisha Alshamrani
Aisha Alshamrani 2021 年 3 月 24 日

0 投票

I have the same problem , i try to use opengl but it didn’t work

1 件のコメント

Adam Danz
Adam Danz 2021 年 3 月 24 日
編集済み: Adam Danz 2021 年 3 月 24 日
See Paul's answer on this page or Star Strider's answer in another thread.

サインインしてコメントする。

カテゴリ

製品

リリース

R2019b

質問済み:

2019 年 10 月 2 日

コメント済み:

2023 年 10 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by