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 件)

Les Beckham
Les Beckham 2023 年 3 月 22 日

0 投票

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)
Table1 = 2×4 table
xaxis A B Color _____ __ __ _________ 1 10 45 "#808080" 2 15 65 "#808080"
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
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
MattC 2023 年 3 月 24 日
So basically what I am trying to achieve is each row from that table will have its own x value plotted across x axis and A,B values that go on Y axis but the color for A,B should also be what I have in that row of the table. So in my above comment I expect 2 colors in the plot for each individual A,B pair
Les Beckham
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.
Les Beckham
Les Beckham 2023 年 3 月 25 日
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)
Table1 = 2×4 table
xaxis A B Color _____ __ __ _________ 1 10 45 "#808080" 2 15 65 "#023020"
Odds = [];
markers = 's+'
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
Les Beckham
Les Beckham 2023 年 3 月 25 日
編集済み: Les Beckham 2023 年 3 月 25 日
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)
Table1 = 2×4 table
xaxis A B Color _____ __ __ _________ 1 10 45 "#808080" 2 15 65 "#023020"
Odds = [];
markers = 'sd'
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
MattC 2023 年 3 月 25 日
  1. The error I am facing is because you're right 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.I am trying this with a different data sets having NaN values for A,B and that is issue
  2. This is the easiest way I can explain may be: The earlier code was correct in terms of value (A,B) placement only change needed is the how they are colored
Les Beckham
Les Beckham 2023 年 3 月 25 日
編集済み: Les Beckham 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
MattC 2023 年 3 月 25 日
編集済み: MattC 2023 年 3 月 25 日
I see you made an edit to the code but I believe that's still would not fix what I am trying to do here:
It is not about the markers being sd or s+. It is about how s and + are aligned in the plot.
Example: x =1 , A= 10, B = 45 (Consider this the first day) it should have 2 markers s for A and +/d for B and one color "#FF0000"
Similarly for x =2 , A= 15, B = 65 (Consider this the second day) it should have 2 markers s for A and +/d for B and one color "#0000FF"
Hope this makes sense now!
MattC
MattC 2023 年 3 月 25 日
The point is to plot the data from the table and I think I shared everything I had already. I know it is easier to just plot it directly but that is not what I am have to do
Les Beckham
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.

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

カテゴリ

製品

リリース

R2020b

タグ

質問済み:

2023 年 3 月 22 日

コメント済み:

2023 年 3 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by