0

I need to plot data in Matlab, Excel or Python with a 3D bar graph. I need to account for 4 variables so three would be used in the x, y, and z-axis and the forth would ideally be color coded in the graph. A sample of the csv file is looks like:

"1, 0, 0.1"
"10, 0, 0.9"
"20, 0, 0.4"
"30, 0, 0.6"
"1, 3.6, 0.1"
"10, 1.1, 0.9"
"20, 1, 26.4"
"30, 1, 30.8"
"1, 3, 518.4"
"10, 3, 929.7"
"20, 3, 1099.2"
"30, 3, 1360.1"
"1, 5.1, 19.1"
"10, 5, 861.9"
"20, 5, 799.8"
"30, 5, 1328.7"
"1, 7, 805.2"
"10, 7, 1407.8"
"20, 7, 2645.1"
"30, 7, 1372.4"

Where the first value (x-axis) is Temperature, second value (y-axis) is Concentration, and third value (z-axis) is velocity.

My current code for Matlab is:

%# read file contents: temp,concentration,velocity
fid = fopen('data.csv','rt');
C = textscan('%f', '%f', '%f');
fclose('%f');

%# correctly reshape the data, and extract x/y labels
num = 5;
T = reshape(C{1},num,\[\]); T = T(1,:);
Con = reshape(C{2},num,\[\]); Con = Con(:,1);
V = reshape(C{3},num,\[\]);

%# plot 3D bars
bar3(Z)
xlabel('Temperature'), ylabel('Concentration'), zlabel('Velocity')
set(gca, 'XTickLabel',T, 'YTickLabel',Con)`

I am not sure how to best go about this. I wrote my code based on some other examples and I am sure there are several things wrong with this as I keep getting different errors when I try to address it. Does anyone have any suggestions for me?

I am trying to get my plot to look something like this: plot

If you know how to manipulate colors for individual values or how to account for a 4th variable in the data, it would be super helpful! The 4th variable just separates all the data above into 1 category. So the complete data set would have 4x the data as above with different values they are grouped into.

Wolfie
  • 27,562
  • 7
  • 28
  • 55
Kilara
  • 1
  • 3
    Your example is a bit misleading, since the plot's colours are just the y-axis not a 4th dimension, which seems to be what you're asking for? You can get an output like your image with MATLAB's `bar3`. Otherwise you'll need something like [this](https://uk.mathworks.com/matlabcentral/answers/5424-how-to-colorize-individual-bar-in-bar3) to colour individual bars. Worth saying that 3D bar charts are already an unclear way to display data - you're trying to add even more information by colouring each bar, perhaps try and think about a better way to display the data (e.g. multiple simpler plots) – Wolfie Nov 15 '22 at 08:46
  • 1
    as Wolfie said, I think the colour doesn't add much info. You say you have 4 dimesntions but only 3 variables (Temp,Conc, Vel). What is the 4th? Maybe then we can help :) – Bas Nov 15 '22 at 12:38
  • 1
    When you say you have 4 dimensions, do you mean you have a value for each 3 locations? like an scalar field?v Is something like this what you want? https://stackoverflow.com/questions/27659632/plotting-volumetric-data-in-matlab/27660039#27660039 – Ander Biguri Nov 15 '22 at 13:34

0 Answers0