3D Column Plot à la Excel

Tabellen und Grafiken erstellen und anordnen


BennyS
Forum-Anfänger
Forum-Anfänger
Beiträge: 10
Registriert: Mo 10. Okt 2016, 14:14

3D Column Plot à la Excel

Beitrag von BennyS »

Hallo zusammen,
ich habe mir in Excel ein Diagramm im "3D Column" Stil erstellt.( Siehe Anhang)
So etwas würde ich auch gerne mit Tikz/PGFPLOT erstellen. Leider habe ich bis jetzt noch keine schöne Lösung gefunden. Nur Umwege über Matlab bzw matlab2tickz.
Schön wäre natürlich auch wenn die Farbeinteilung so beibehalten werden würde.

Könntet ihr mir hier weiterhelfen? Wäre sehr dankbar.

Grüße
Benny
Dateianhänge
Capture.PNG
Capture.PNG (56.91 KiB) 2783 mal betrachtet

Gast

Beitrag von Gast »

Lies mal Wie hänge ich ein Bild an meine Frage? bis zum Ende durch und bearbeite dann deinen Beitrag so, dass das Bild auch wirklich ohne expliziten Download angezeigt wird.

cis
Forum-Anfänger
Forum-Anfänger
Beiträge: 47
Registriert: Mi 22. Jul 2020, 20:14

Re: 3D Column Plot à la Excel

Beitrag von cis »

Ungeachtet des Umstandes, dass das eine perspektivisch verwirrende Darstellung ist, kann man mit pgfplots ein 3D-Bardiagramm so erstellen:

Die Idee ist mark=cube* zu verwenden und die cube/size z so zu manipulieren, dass die gewünschten 3D-Bars entstehen.

Setzt man z.B. cube/size z=4mm, so dimensioniert das Würfelchen von seiner Raumposition aus um 2mm und um -2mm in z-Richtung.
Also müssen sie bei x=X, y=Y, z expr={0.5*\thisrow{Z}} positioniert werden.

Die richtige Höhe der cubes kann mit
visualization depends on={\thisrow{Z} \as \zvalue},
scatter/@pre marker code/.append style={
/utils/exec=\pgfmathsetmacro{\barheight}{\zunitlength*\zvalue},
/pgfplots/cube/size z=\barheight
},
bestimmt werden, wobei die zunitlength mit
\path let \p1=($(axis cs:0,0,1)-(axis cs:0,0,0)$) in
\pgfextra{ \pgfmathsetglobalmacro{\zunitlength}{\y1} } };
gemessen werden kann.

€dit: Es zeigt sich, dass es sinnvoll ist zmax=\zMax zu setzen, dazu kann der maximale z-Wert in der Form
\pgfplotstablegetrowsof{\datatable}
\pgfmathtruncatemacro{\RowsNo}{\pgfplotsretval-1}
%Zeilenzahl: \RowsNo

\pgfmathsetmacro\zMax{0}
\foreach \n in {0,...,\RowsNo}{
\pgfplotstablegetelem{\n}{Z}\of{\datatable}
\pgfmathparse{\pgfplotsretval > \zMax ? \pgfplotsretval : \zMax}
\xdef\zMax{\pgfmathresult}
}
%Maximalwert z-Achse: \zMax
ermittelt werden.

55555a5.png
\documentclass[border=5pt, tikz]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.17}
\usetikzlibrary{calc}

\def\pgfmathsetglobalmacro#1#2{\pgfmathparse{#2}%
\global\let#1\pgfmathresult}

\pgfplotsset{
colormap = {mycolormap}{
color(0)  = (blue!50!black); 
color(1) = (purple);
color(2) = (green!55!black);
color(3) = (brown);
color(4) = (blue!66)
color(5) = (violet)
},
colormap name=mycolormap,
%colormap name=viridis
}

\begin{document}
\pgfplotstableread[col sep=comma,header=true]{
X,   Y,    Z
2,    0,   4
1,    0,   5
0,    1,   10
3,    1,   1
1,    1,   3
2,    1,   0
1,    2,   0
2,    2,   5
2,    3,   6
1,    4,   7
1,    5,   11
}{\datatable}

% z-Maximum determination ==================
\pgfplotstablegetrowsof{\datatable}
\pgfmathtruncatemacro{\RowsNo}{\pgfplotsretval-1}  
%Number of rows: \RowsNo \pgfmathsetmacro\zMax{0} \foreach \n in {0,...,\RowsNo}{ \pgfplotstablegetelem{\n}{Z}\of{\datatable} \pgfmathparse{\pgfplotsretval > \zMax ? \pgfplotsretval : \zMax} \xdef\zMax{\pgfmathresult} } %Maximum z-Axis: \zMax % ============================ \begin{tikzpicture}[] \begin{axis}[ %height=2cm, width=7cm, % view={120}{40}, x dir=reverse, xmin=0, ymin=0, zmin=0, zmax=\zMax, enlarge z limits={rel=0.25,upper}, xtick={1,...,10}, ytick={1,...,10}, %ytick={0,25,...,100}, grid=both, xlabel={$x$}, ylabel={$y$}, zlabel={$z$}, minor z tick num=1, point meta=explicit, scatter/use mapped color={draw=mapped color!50!black, fill=mapped color!70}, ] % unitlenghth z-Axis determination \path let \p1=($(axis cs:0,0,1)-(axis cs:0,0,0)$) in \pgfextra{ \pgfmathsetglobalmacro{\zunitlength}{\y1} } node[xshift=2cm, yshift=2cm]{%\zunitlength % show value }; \addplot3[ scatter, only marks, mark=cube*, mark size=5, %opacity=0.8, nodes near coords*=, % will er... %%% barheight determination visualization depends on={\thisrow{Z} \as \zvalue}, scatter/@pre marker code/.append style={ /utils/exec=\pgfmathsetmacro{\barheight}{\zunitlength*\zvalue}, /pgfplots/cube/size z=\barheight
}, ] table[x=X, y=Y,
z expr={0.5*\thisrow{Z}}, meta expr={\thisrow{Y}} ]{\datatable}; \end{axis} \end{tikzpicture} \end{document}

cis
Forum-Anfänger
Forum-Anfänger
Beiträge: 47
Registriert: Mi 22. Jul 2020, 20:14

Re: 3D Column Plot à la Excel

Beitrag von cis »

Mir wird bei weiterem Experimentieren noch klar, dass die Werte für diese Darstellung im allgemeinen sortiert werden müssen.
Mal schauen, vielleicht hat pgfplotstable sowas inzwischen.


Antworten