Color in plot from Table
10 次查看(过去 30 天)
显示 更早的评论
Hi All, I have a table in matlab which has some columns which I trying to plot. The table also has a column for the color which I want to use as color point but when I try and use this column directly to plot the data it gives me an error. Can someone help how to plot this?
xaxis = [1,2];
A = [10,15];
B = [45,65];
Color = ["#808080","#808080"];
Table1 = table (xaxis,A,B,Color);
Trying to plot it like:
Odds = [];
Odds(end+1) = plot(Table1.xaxis,Table1.A,'s','MarkerFaceColor','none','MarkerEdgeColor',sprintf('%s',Table1.Color));
hold on
Odds(end+1) = plot(Table1.xaxis,Table1.B,'+','MarkerFaceColor','none','MarkerEdgeColor',sprintf('%s',Table1.Color));
% This is the error I receive
% Error using plot
% Invalid color name or hexadecimal color code. Valid names include: 'red', 'green', 'blue', 'cyan',
% 'magenta', 'yellow', 'black', 'white', and 'none'. Valid hexadecimal color codes consist of '#' followed
% by three or six hexadecimal digits.
0 个评论
回答(1 个)
Les Beckham
2023-3-22
Just a couple of issues. You need column vectors to insert into the table, and you can only have a scalar string or char vector for specifying the MarkerEdgeColor using a hex color code.
xaxis = [1,2]'; % <<< transpose these to get column vectors to use in creating the table
A = [10,15]';
B = [45,65]';
Color = ["#808080","#808080"]';
Table1 = table (xaxis,A,B,Color)
Odds = [];
Odds(end+1) = plot(Table1.xaxis,Table1.A,'s','MarkerFaceColor','none','MarkerEdgeColor',Table1.Color(1));
hold on
Odds(end+1) = plot(Table1.xaxis,Table1.B,'+','MarkerFaceColor','none','MarkerEdgeColor',Table1.Color(1));
xlim([0 3])
ylim([5 70])
grid on
10 个评论
Les Beckham
2023-3-26
I'm pretty sure that I have shown you all of the available options for using the plot command to specify markers and colors, and how to extract data from a table. If you won't share your actual data and the code that you are using (and why, specifically, it doesn't do what you want), I can't help you any further.
Experiment with changing the code that you have (using the suggestions I gave you) until you get what you want. That is the thing that is great about Matlab, it is easy to try things and see what happens, and then change it until it does what you want.
另请参阅
类别
在 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!