The legend overlay is displayed when drawing

14 Comments

Attach a minimum working example that illustrates the problem; we can't fix what we don't know how was done...it doesn't do that as a general rule.
Pls post a working code - no one can work on just an image
This is my drawing statement, I don't think there are any errors in the statement;
The error seems to have occurred after I used the patch function, and all legends display this error.
figure
h1 = plot3(x, y, z, 'o', 'MarkerSize', 6, 'MarkerFaceColor', 'r');
hold on;
plot3(x_interp, y_interp, z_interp, 'b-', 'LineWidth', 2);
plot3(x_interp, y_interp, z_real, 'g-', 'LineWidth', 2);
grid on;
set(gca, 'YDir', 'reverse');
xlabel('X');ylabel('Y');zlabel('Z');
title('隧道反演结果三维示意图');
legend( '隧道中心反演结果', '反演的隧道三维分布', '模拟的隧道三维分布');
xlim([0 200]);
ylim([0 200]);
zlim([-25 0]);
hold off;
It seems to work with some test data.
N = 50;
x = sort(rand(1,N));
y = rand(1,N);
z = rand(1,N);
x_interp = linspace(0,1,N);
y_interp = interp1(x, y, x_interp);
z_interp = interp1(x, z, x_interp);
z_real = z_interp + randn(1,N);
figure
h1 = plot3(x, y, z, 'o', 'MarkerSize', 6, 'MarkerFaceColor', 'r');
hold on;
plot3(x_interp, y_interp, z_interp, 'b-', 'LineWidth', 2);
plot3(x_interp, y_interp, z_real, 'g-', 'LineWidth', 2);
grid on;
set(gca, 'YDir', 'reverse');
xlabel('X');ylabel('Y');zlabel('Z');
title('隧道反演结果三维示意图');
legend( '隧道中心反演结果', '反演的隧道三维分布', '模拟的隧道三维分布');
xlim([0 200]);
ylim([0 200]);
zlim([-25 0]);
hold off;
I have also tested it on other computers and it can display the legend correctly, but on this computer it will display an error, it seems to be a problem with the patch function
Your example code does not use the patch() function ?
I used patch() in other scripts, and the legend in the drawing kept showing errors afterwards
Please post a working example.
p1 = patch(x, y, z, 'FaceColor', 'blue');
legend([h1, p1], {'反演结果', '填充'});
patch() was used in the test, and it seems to have been used in this way. And then, the legend() error.
p1 = patch(x, y, z, 'FaceColor', 'blue');
Unrecognized function or variable 'x'.
legend([h1, p1], {'反演结果', '填充'});
That is not a working example...most particularly, h1 is undefined when get there and that's probably the key ingredient here.
Submit code and format it with the "CODE" button, then use the "RUN" button -- when your result shows the issue, then you'll have a working example. Easiest route would likely be to pare down your real use case to the barest of lines that you can that show up the problem on your local machine first, then paste that in...
Looks okay in my test.
N = 50;
x = sort(rand(1,N));
y = rand(1,N);
z = rand(1,N);
x_interp = linspace(0,1,N);
y_interp = interp1(x, y, x_interp);
z_interp = interp1(x, z, x_interp);
z_real = z_interp + randn(1,N);
figure
h1 = plot3(x, y, z, 'o', 'MarkerSize', 6, 'MarkerFaceColor', 'r');
hold on;
plot3(x_interp, y_interp, z_interp, 'b-', 'LineWidth', 2);
plot3(x_interp, y_interp, z_real, 'g-', 'LineWidth', 2);
grid on;
set(gca, 'YDir', 'reverse');
xlabel('X');ylabel('Y');zlabel('Z');
title('隧道反演结果三维示意图');
p1 = patch(x, y, z, 'FaceColor', 'blue');
legend([h1, p1], {'反演结果', '填充'});
xlim([0 200]);
ylim([0 200]);
zlim([-25 0]);
hold off;
"I used patch() in other scripts, and the legend in the drawing kept showing errors afterwards"
If the use of scripts in the above means what a script means in MATLAB, then you quite possibly have invalid handles hanging around since scripts run in the command workspace.
Execute
clear all % save hard to regenerated data first
and then retry the script and see if the problem doesn't go away.
If so, then encapsulate your scripts as functions so you don't leave temporary variables hanging around.
If not, you will at least have generated a test case that should demonstrate the problem that others can try.
If not, also then close and restart Matlab (be sure to save anything of value including the updated script/function files first) and try again.
If it then still repeats, it may be time to reinstall or check for updates.
clear all;
N = 50;
x = sort(rand(1,N));
y = rand(1,N);
z = rand(1,N);
x_interp = linspace(0,1,N);
y_interp = interp1(x, y, x_interp);
z_interp = interp1(x, z, x_interp);
z_real = z_interp + randn(1,N);
figure
h1 = plot3(x, y, z, 'o', 'MarkerSize', 6, 'MarkerFaceColor', 'r');
hold on;
plot3(x_interp, y_interp, z_interp, 'b-', 'LineWidth', 2);
plot3(x_interp, y_interp, z_real, 'g-', 'LineWidth', 2);
grid on;
set(gca, 'YDir', 'reverse');
xlabel('X');ylabel('Y');zlabel('Z');
title('隧道反演结果三维示意图');
p1 = patch(x, y, z, 'FaceColor', 'blue');
legend([h1, p1], {'反演结果', '填充'});
xlim([0 200]);
ylim([0 200]);
zlim([-25 0]);
hold off;
I tested the code on the webpage and there were no errors;
But on my local computer, I reinstalled Matlab and run the code, it still encountered errors. In the figure, there seems to be a shadow covering the top.
dpb
dpb on 2 Sep 2024
Edited: dpb on 2 Sep 2024
OK, that's interesting and informative. This leads to confriming the fact something is unique/different on the particular machine; now it's down to specifics that may matter. Which OS, release, is it updated? Same with MATLAB although with a reinstall if you downloaded afresh it should include updates but you didn't say which release with the question posting(*).
These kinds of graphical display issues often get into the specifics of the display hardware/chipset and drivers and the MATLAB renderer. Updating your graphics drivers from the chipset/card vendor can sometimes fix things.
However, first work through the suggestions at <resolving low level graphics issues> and you may be able to resolve it yourself. If switching to a software OpenGL renderer makes the issue go away, then it is pretty clearly something in the hardware/drivers.
(*) There's an alternative thing one could try albeit time-consuming; download a prior release and see if the symptom stays/goes away...one can have multiple releases of MATLAB installed at the same time so don't have to remove your current install; you just need patience and enough bandwidth it's not too much of a chore.

Sign in to comment.

Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Asked:

龙
on 29 Aug 2024

Edited:

dpb
on 2 Sep 2024

Community Treasure Hunt

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

Start Hunting!