I want to plot a matrix of color-values from a csv file. Inspired by this website, I set out to write my MWE below, to which I added this way of dynamically loading symbolic ticks/labels from my data file.
However, It turns out that my resulting ticks/labels are not distinct, but instead are repeated, like my rendering shows.
Another way to think of it (according to @Jake's comments) is that flexible (x|y)ticklabels from table
does not tell PGFplots that these labels are supposed to be symbolic, as doing it manually using symbolic (x|y) coords
would.
How can I tell PGFplots that all distinct values from the xitem
and yitem
columns in my data file are to be used as symbolic labels?
Maybe the problem lies within (x|y) expr=\coordindex
. What I want here is to fill in the (symbolic, not numerical) value from the respective colum (xitem
|yitem
). But using x=xitem,y=yitem,
instead gives the error "Package PGF Math Error: Could not parse input 'X' as a floating point number".
Rendering:
MWE:
\documentclass{standalone}
\usepackage{filecontents}
\usepackage{pgfplots, pgfplotstable}
\begin{filecontents}{data.csv}
yitem,xitem,val
r,X,0.1
r,Y,0.2
r,Z,0.3
d,X,0.4
d,Y,0.5
d,Z,0.6
j,X,0.5
j,Y,0.8
j,Z,0.9
\end{filecontents}
\pgfplotstableread[col sep=comma]{data.csv}\datatable
\makeatletter
\pgfplotsset{
/pgfplots/flexible xticklabels from table/.code n args={3}{
\pgfplotstableread[#3]{#1}\coordinate@table
\pgfplotstablegetcolumn{#2}\of{\coordinate@table}\to\pgfplots@xticklabels
\let\pgfplots@xticklabel=\pgfplots@user@ticklabel@list@x
},
/pgfplots/flexible yticklabels from table/.code n args={3}{
\pgfplotstableread[#3]{#1}\coordinate@table
\pgfplotstablegetcolumn{#2}\of{\coordinate@table}\to\pgfplots@yticklabels
\let\pgfplots@yticklabel=\pgfplots@user@ticklabel@list@y
}
}
\makeatother
\begin{document}
\begin{tikzpicture}
\begin{axis}[
flexible xticklabels from table={data.csv}{xitem}{col sep=comma},
xtick=data,
flexible yticklabels from table={data.csv}{yitem}{col sep=comma},
ytick=data,
unbounded coords=jump,
]
\addplot[
mark=square*,
only marks,
scatter,
point meta=explicit,
] table [
col sep=comma,
x expr=\coordindex,
y expr=\coordindex,
meta expr=\thisrow{val},
] {\datatable};
\end{axis}
\end{tikzpicture}
\end{document}
symbolic x coords={a,b,c}
andsymbolic y coords={X,Y,Z}
(while also removingx expr=\coordindex, y expr=\coordindex
). That's a bit cumbersome to work with, though. There might be better ways to organise your data. How do you generate your data table?flexible (x|y)ticklabels from table
in a way that it filters duplicates. But I don't know how.flexible ticklabels from table
won't help here, because that's just for generating the labels, but not the coordinates. You're currently plotting the data points at(i|i)
, wherei
is the coordinate index, so filtering duplicates out of the labels won't help you. Rather, you need to mapa
,b
,c
... tox=1
,2
,3
... andX
,Y
,Z
toy=1
,2
,3
... (which is whatsymbolic x coords
does).a
,b
,c
andX
,Y
,Z
, but eventually I need these original values (not the IDs) to be written near the axes.