# plotting 4 variables graph from csv file

I have a csv file with 4 columns like this:

P Q R cost

6, 3, 1, SC

6, 2, 2, SC

6, 1, 3, SC

5, 4, 1,

5, 3, 2,

5, 2, 3, DR

5, 1, 4, DR

4, 5, 1, DR

4, 4, 2, BR

4, 3, 3, BR

The first three columns are showing all the possible partitions of a number (n) between three variables (P, Q, R). In this example (n=10) and I didn't show all the possible combination. The last column shows that for each ratio which case is best (SC, BR or DR). My idea to plot is to use Q on the x-axis and R on the y-axis and plot area where BR or SC or DR is best. Can any expert please help me? I provided the sample csv file data for the minimum example.

• So you want to have either P, Q or R as x-values, and min(S,B,C) as y-values? By the way, if you could add those ten data lines as text in the question instead, it makes it easier to make an example. Feb 26, 2021 at 20:59
• I have edited the post with the data line as the text. Yes! that is what I am thinking to use R on x-value and min(S,B,C) as y value, Kind of overlapped area plot if any S,B,C have the same minimum value. Feb 26, 2021 at 21:28
• I realize I don't actually understand what you're after. I don't know if anyone else understood, but perhaps a sketch of what kind of plot you expect from the sample data file would help. For example, just \documentclass{standalone} \usepackage{pgfplots} \begin{document} \begin{tikzpicture} \begin{axis} \addplot table[col sep=comma, x=R, y expr={min(\thisrow{S}, min(\thisrow{B}, \thisrow{C}))}] {datafile.txt}; \end{axis} \end{tikzpicture} just gets you a straight line at y=1, which I guess you don't want? \end{document} Mar 1, 2021 at 15:59
• @TorbjørnT. I tried to update my question. Can you please look into it. Does it make sense now? Mar 3, 2021 at 16:49

I still don't know if this is anything like what you had in mind. If not, do consider making a sketch.

\documentclass[border=5mm]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.17}
% Note commas in the header as well!
P, Q, R, cost
6, 3, 1, SC
6, 2, 2, SC
6, 1, 3, SC
5, 4, 1, DR
5, 3, 2, BR
5, 2, 3, DR
5, 1, 4, DR
4, 5, 1, DR
4, 4, 2, BR
4, 3, 3, BR
}\MyData

% you'll have the data in a file, so do
\begin{document}
\begin{tikzpicture}
% define a few macros for convenience
\newcommand\AxisW{6cm} % width/height of axis
\newcommand\AxisXMin{0.5} % xmin/ymin
\newcommand\AxisRange{10} % range of axis, both x and y
\begin{axis}[
% define how the different cases are drawn
% all should have same mark and draw opacity,
% but change the color as you prefer
scatter/classes={
SC={mark=square*, draw opacity=0, fill=blue},
DR={mark=square*, draw opacity=0, fill=red},
BR={mark=square*, draw opacity=0, fill=green}
},
% add labels for the axes
xlabel=Q,
ylabel=R,
% set up the size of the axis
scale only axis, % width/height applies to axis box alone, without labels
width=\AxisW, height=\AxisW,
% set up the range of the axes
xmin=\AxisXMin, xmax=\AxisXMin+\AxisRange,
ymin=\AxisXMin, ymax=\AxisXMin+\AxisRange,
% calculate the mark size - divide by 2 because the size is half the width
mark size={\AxisW/\AxisRange/2},
% set legend entries in one row
legend columns=-1,
% move legend outside top of axis
legend style={at={(0.5,1.02)}, anchor=south},
% have ticks every 1 unit distance
xtick distance=1,ytick distance=1,
]

scatter, % make a scatter plot
only marks, % for some reason have to say that we only want markers
scatter src=explicit symbolic % say that the variable that declares which scatter class a point belongs to is explicitly given in the data stream, and is not a number, so should not be parsed as a number
]
table[x=Q, % use Q-column for x-values
y=R, % use R-column for y-values
meta=cost % use cost-column to define which scatter class should be used
] \MyData;

\legend{SC, DR, BR}
\end{axis}
\end{tikzpicture}
\end{document}
`

• Torbjørn T. Thank you so much.That is very helpful. I am thinking to change it into 3D graph, which also takes the value of P from file too. I used \addplot3 but the output is not a smooth 3D graph. Any help how I can convert it into 3D? Mar 6, 2021 at 16:21
• @TaniaMalik Probably not from me. I'd suggest posting a new question about that. Mar 6, 2021 at 16:24
• Thanks, Torbjørn T. I am going to post another question with your modified code. Thank you for your help Mar 6, 2021 at 16:26