Color in plot from Table
古いコメントを表示
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.
回答 (1 件)
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 月 24 日
Yes. As I said, "you can only have a scalar string or char vector for specifying the MarkerEdgeColor using a hex color code" in a plot command.
Perhaps your example is a too oversimplified to show what you are really trying to do.
Please try to explain what you are trying to do in more detail. Do you want a unique color for each marker in each of the two calls to plot?
MattC
2023 年 3 月 24 日
Les Beckham
2023 年 3 月 25 日
I'm sorry but that doesn't make any sense. Each row in your table only has one x axis value and one color.
I think you may have oversimplified your example. Please explain more clearly what your are trying to do and give a better example of the data and the expected plot result.
xaxis = [1,2]'; % <<< transpose these to get column vectors to use in creating the table
A = [10,15]';
B = [45,65]';
Color = ["#808080","#023020"]';
Table1 = table (xaxis,A,B,Color)
Odds = [];
markers = 's+'
for i = 1:numel(Table1.xaxis)
Odds(end+1) = plot(Table1.xaxis,[Table1.A(i) Table1.B(i)], ...
markers(i), 'MarkerFaceColor', 'none', 'MarkerEdgeColor', ...
Table1.Color(i));
hold on
end
xlim([0 3])
ylim([5 70])
grid on
If you have issues with your code, then you need to show your code. As you can see above, the code that I posted here has no such error.
You have chosen a really strange way to organize your data, which makes it more difficult to achieve what you seem to want than it needs to be.
For a plot, you need a vector for x that is the same size as the vector of y values that you want to plot for each "line" on the plot.
Perhaps this will do what you want.
xaxis = [1,2]'; % <<< transpose these to get column vectors to use in creating the table
A = [10,15]';
B = [45,65]';
Color = ["#808080","#023020"]';
Table1 = table (xaxis,A,B,Color)
Odds = [];
markers = 'sd'
for i = 1:numel(Table1.xaxis)
Odds(end+1) = plot(repelem(Table1.xaxis(i), 2),[Table1.A(i) Table1.B(i)], ...
markers(i), 'MarkerFaceColor', 'none', 'MarkerEdgeColor', ...
Table1.Color(i));
hold on
end
xlim([0 3])
ylim([5 70])
grid on
MattC
2023 年 3 月 25 日
You really should organize your data better. This shouldn't be so hard. You can't use different markers in the same plot command. Nor can you use different colors (easily).
If you really only want to plot these 4 points with the colors and markers you specify that isn't hard but you have made it difficult with the structure of the (unnecessary) table.
As I said before, if this example isn't really representative of what you are really trying to do, provide the real data and the real reason for how the data is organized.
line(1, 10, 'Marker', 's', 'Color', 'r'); %'#808080') %<< using colors that are
line(1, 15, 'Marker', '+', 'Color', 'r'); %'#808080') %<< easier to see
line(2, 45, 'Marker', 's', 'Color', 'b'); %'#023020')
line(2, 65, 'Marker', '+', 'Color', 'b'); %'#023020')
xlim([0 3])
ylim([5 70])
grid on
MattC
2023 年 3 月 25 日
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.
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





