pgfplots
- How to reproduce the correct intersection curve of two 3D surfaces using PGFPlots?by Samia Rani on August 10, 2026 at 10:50 am
I am trying to reproduce a 3D figure from a mathematics book in LaTeX using TikZ/PGFPlots. The figure shows two surfaces and their intersection. The two surfaces are given by x^2 + xy - y - 3z = 0 and 2x^2 + 3xy - 2y - 3z = 0. I plotted both surfaces directly from their equations and also added their intersection curve. However, the shape and especially the intersection curve in my output look quite different from the figure in the book, even though I am using the same equations. I am not sure whether the difference is caused by the viewing angle, the plotting ranges, the parametrization of the intersection curve, or something else. Here is my current code: \documentclass[border=5pt]{standalone} \usepackage{pgfplots} \usepackage{tikz} \pgfplotsset{compat=1.18} \begin{document} \begin{tikzpicture} \begin{axis}[ view={120}{25}, axis lines=none, xmin=-2.5, xmax=2.5, ymin=-2.5, ymax=2.5, zmin=-5, zmax=5, width=11cm, height=8cm, ticks=none, ] %================================================ % GREEN SURFACE % 2x^2 + 3xy - 2y - 3z = 0 %================================================ \addplot3[ surf, shader=interp, colormap={greenmap}{ color(0cm)=(green!15); color(1cm)=(green!35) }, colormap name=greenmap, domain=-2:2, y domain=-2:2, samples=35, samples y=35, opacity=0.65, ] { (2*x^2 + 3*x*y - 2*y)/3 }; %================================================ % BLUE SURFACE % x^2 + xy - y - 3z = 0 %================================================ \addplot3[ surf, shader=interp, colormap={bluemap}{ color(0cm)=(blue!15); color(1cm)=(blue!35) }, colormap name=bluemap, domain=-2.2:2.2, y domain=-2.2:2.2, samples=30, samples y=30, opacity=0.55, ] { (x^2 + x*y - y)/3 }; %================================================ % INTERSECTION CURVE % gamma(t) = % ( t, -t^2/(2t-1), t^3/(3(2t-1)) ) %================================================ \addplot3[ very thick, red, samples=100, domain=-2.2:0.35, variable=\t, ] ( {\t}, {-\t^2/(2*\t-1)}, {\t^3/(3*(2*\t-1))} ); \addplot3[ very thick, red, samples=50, domain=0.65:2.2, variable=\t, ] ( {\t}, {-\t^2/(2*\t-1)}, {\t^3/(3*(2*\t-1))} ); \end{axis} \end{tikzpicture} \end{document} The book's figure has a noticeably different orientation and shape for the two surfaces and their intersection. The book's figure looks like this: My current output looks like this: Could someone please explain what is causing this difference and how I can modify my PGFPlots code to obtain a figure that looks like the one in the book? In particular, I would like to know whether I should change the view angles, the plotting domains, or the parametrization of the intersection curve.
- How can I draw the plane and intersection curve on a one-sheet hyperboloid in TikZ?by Samia Rani on August 9, 2026 at 5:23 pm
I am trying to recreate the following mathematical figure in LaTeX/TikZ. The figure shows the one-sheet hyperboloid x^2 + y^2 - z^2 = 1 together with the plane 2x + z = 1. Their intersection is the red curve shown on the hyperboloid. After quite a lot of effort, I have managed to draw the one-sheet hyperboloid reasonably well in TikZ. However, I am having difficulty with the two remaining parts: How can I draw the transparent red plane 2x + z = 1 behind/in front of the hyperboloid, as shown in the reference image? How can I draw the red intersection curve between the plane and the hyperboloid so that it lies correctly on the surface? I would like the final figure to look as close as possible to the reference image, including the perspective, transparency of the plane, and the red intersection curve. Here is the code I currently have for the hyperboloid: \documentclass[border=5pt]{standalone} \usepackage{pgfplots} \usepackage{tikz} \pgfplotsset{compat=1.18} \begin{document} \begin{tikzpicture}[>=stealth] \begin{axis}[ view={120}{22}, axis lines=none, xmin=-3.3, xmax=3.3, ymin=-3.3, ymax=3.3, zmin=-2.0, zmax=2.0, xtick=\empty, ytick=\empty, ztick=\empty, clip=false, ] %------------------------------------------------ % Red point at the origin %------------------------------------------------ \addplot3[ only marks, mark=*, mark size=2.5pt, draw=red, fill=red, ] coordinates {(0,0,0)}; %------------------------------------------------ % Smooth one-sheet hyperboloid %------------------------------------------------ \addplot3[ surf, shader=interp, draw=blue!55!black, opacity=0.65, samples=45, samples y=80, domain=-2.2:2.2, y domain=0:360, z buffer=sort, ] ( {sqrt(1+x^2)*cos(y)}, {sqrt(1+x^2)*sin(y)}, {x} ); %------------------------------------------------ % POSITIVE x-axis %------------------------------------------------ \draw[ ->, black, thick ] (axis cs:0,0,0) -- (axis cs:5.0,0,0) node[right] {$x$}; %------------------------------------------------ % NEGATIVE x-axis -- dashed %------------------------------------------------ \draw[ dashed, black, thick ] (axis cs:0,0,0) -- (axis cs:-2.0,0,0); %------------------------------------------------ % POSITIVE y-axis %------------------------------------------------ \draw[ ->, black, thick ] (axis cs:0,0,0) -- (axis cs:0,4.0,0) node[above left] {$y$}; %------------------------------------------------ % NEGATIVE y-axis -- dashed %------------------------------------------------ \draw[ dashed, black, thick ] (axis cs:0,0,0) -- (axis cs:0,-1.0,0); %------------------------------------------------ % POSITIVE z-axis %------------------------------------------------ \draw[ ->, black, thick ] (axis cs:0,0,0) -- (axis cs:0,0,3.0) node[above] {$z$}; %------------------------------------------------ % NEGATIVE z-axis -- dashed %------------------------------------------------ \draw[ dashed, black, thick ] (axis cs:0,0,0) -- (axis cs:0,0,-2.0); %------------------------------------------------ % Origin %------------------------------------------------ \node[ below right, inner sep=1pt ] at (axis cs:0,0,0) {$0$}; %------------------------------------------------ % Equation %------------------------------------------------ \node[ blue!75!black, font=\large ] at (axis cs:2.0,-0.5,1.1) {$x^2+y^2-z^2=1$}; \end{axis} \end{tikzpicture} \end{document} I would be very grateful for any suggestions on how to add the plane and, especially, the intersection curve correctly. Thank you!
- How to easily draw and parameterize various pH titration curves (including diprotic/dibasic) for a beginner?by user446526 on August 6, 2026 at 8:23 am
I am a complete beginner when it comes to drawing complex functional plots in LaTeX. I am trying to create several types of pH titration curves for a document, but I am struggling to write the code from scratch. I found a very helpful French website that seems to have the exact mathematical logic and LaTeX code for what I need: https://www.astrolabe-science.fr/courbes-titrages-acido-basiques-latex/ However, as a beginner, I find it quite difficult to extract just the essential parts to create a simple, easily editable template. I need to recreate the following types of curves (I have attached images of what I am aiming for): The 4 basic monoprotic titrations: Strong Acid, Strong Base, Weak Acid, and Weak Base. Diprotic acid titration: A curve with two equivalence points (increasing pH). Dibase titration: A curve with two equivalence points (decreasing pH). My request: Could someone help provide a simplified, beginner-friendly pgfplots (or TikZ) template based on the math from that website? Ideally, I am looking for a code structure where the main parameters (like initial concentrations, volumes, and pK_a values) are defined at the top so I can easily change them. It would be incredibly helpful if you could include brief comments/instructions inside the code explaining how to switch between the different types of curves (e.g., "change this sign for a base", "uncomment this for a diprotic acid"). Thank you so much for your time and help! I have attached images of what I am aiming for.
- Stacking multiple number lines on top of each otherby Muhannad Al Ayoubi on August 1, 2026 at 9:09 am
I made the following diagram, which is supposed to depict how the intersection of two intervals can be determined. However, as you can see, I could include only one number line, which makes the diagram look rather empty, in my opinion. Is there a away to draw two more number lines above the original one, aligned with the "flying" blue intervals? I have thought about drawing three separate tikz pictures above each other, but then I can't draw the red dashed line, and it would also make it difficult to control the invisible space taken by the y-axis. \documentclass{book} \usepackage{pgfplots} \begin{document} \begin{tikzpicture}[every node/.style={font=\small},] \begin{axis}[ clip=false, axis x line=middle, axis y line=none, x axis line style={<->}, xmin=-3.4, xmax=7.4, ymin=-.5, ymax=3, xtick distance=1, x=.6cm, y=1cm, ] % Red dashed lines \draw [red,dashed] (axis cs:-1,0) -- (axis cs:-1,2.2) (axis cs:6,0) -- (axis cs:6,2.2); % Points \addplot[only marks,mark options={fill=white}] coordinates {(6,2) (-1,1) (6,0) (-1,0)}; % Uppermost number line for x < 6 \draw[<-,very thick] (axis cs:-3,2) -- (axis cs:6,2); % Middle number line for x > -1 \draw[->,very thick] (axis cs:-1,1) -- (axis cs:7,1); % Lower number line for intersection of inequalities \draw[very thick] (axis cs:-1,0) -- (axis cs:6,0); % Labels \draw (axis cs:8,2) node[right]{$x<6$} (axis cs:8,1) node[right]{$x>-1$} (axis cs:8,0) node[right]{$x<6$ and $x>-1$, or simply $-1<x<6$.}; \end{axis} \end{tikzpicture} \end{document} Here is what it looks like (note I have removed color and font information from the code to keep things simple).
- tikz and pgf not framedby user446112 on July 29, 2026 at 3:55 pm
I'm drawing the graph below using Beamer, but the plot is not framed with the points (1/Pi/2) and (-1,-Pi/2). What’s happening here? My code is: \documentclass{beamer} \usepackage{pgfplots} \usepackage{tikz} \usetikzlibrary{tikzmark,calc,positioning,fit,arrows,shapes,backgrounds,shapes.geometric,arrows.meta,patterns.meta,decorations.pathreplacing} \begin{document} \begin{frame} \begin{center} \begin{tikzpicture}[scale=0.7, samples=100] \draw[-latex,thick] (-3,0) -- (3,0); \draw[-latex,thick] (0,-2.5) -- (0,2.5); \draw [thick](-1,-0.125)--(-1,0.125); \draw [thick](1,-0.125)--(1,0.125); \draw [thick](-0.125,-1.570796327)--(0.125,-1.570796327); \draw [thick](-0.125,1.570796327)--(0.125,1.570796327); \node at (2.5,-0.5) {$x$}; %\draw[-latex,thick] (0,-2) -- (0,3); \node at (-0.5,2.3) {$y$}; \node at (-1,-0.5) {{\scriptsize$-1$}}; \node at (1,-0.5) {{\scriptsize$1$}}; \node at (0.6,-1.570796327) {{\scriptsize$-\frac{\pi}{2}$}}; \node at (-0.4,1.570796327) {{\scriptsize$\frac{\pi}{2}$}}; \draw [thick](-0.125,-1.570796327)--(0.125,-1.570796327); \draw [thick](-0.125,1.570796327)--(0.125,1.570796327); \draw [dotted](1,0)--(1,1.570796327); \draw [dotted](-1,-0.8)--(-1,-1.570796327); \draw [dotted](0,1.570796327)--(1,1.570796327); \draw [dotted](-1,-1.570796327)--(0,-1.570796327); \begin{axis}[smooth,anchor=origin,xmin=-3,xmax=3, domain=-3:3,ymin=-2.5,ymax=2.5,restrict y to domain=-2.5:2.5,axis equal,axis x line=none,axis y line=none,xticklabels=\empty,yticklabels=\empty,samples=100] \addplot[domain=-1:1,blue,line width=2pt] {rad(asin(x))}; \end{axis} \end{tikzpicture} \end{center} \end{frame} \end{document} (compiled through LuaLaTeX).
- Fine-tuning the box surrounding a bar plotby Muhannad Al Ayoubi on July 18, 2026 at 5:22 pm
This is my first time making a bar plot using pgfplots. I am happy so far with what I have, but I still need some points. How can I make the top-most tick (14) coincide with the top boundary of the box? I do not like the tiny space between the top-most gridline and the top boundary of the box. I've already tried removing 14 from the key ytick but I don't like the result. I think the 14 has to stay. Suppose instead I decided to completely remove the top and right boundaries of the box. I know I can do that using the options axis x line*=bottom and axis y line*=left, but then I'd like to include arrowheads, at least for the y-axis. How to do that? Finally, I've managed to adjust the distance between successive bars by playing with the option x=0.8 cm. Is there a better way? Here is my code. I've also attached the figure. \documentclass[10pt]{book} \usepackage{tikz,pgfplots} \begin{document} \begin{tikzpicture}[every node/.style={font=\footnotesize}] \begin{axis}[ clip=false, ybar, ymajorgrids, grid style={dashed}, ymin=0, %axis x line*=bottom, %axis y line*=left, xlabel=Number of siblings, ylabel=Frequency, ytick pos=left, xtick pos=left, xtick=data, ytick={0,2,4,...,12,14}, bar width=.4cm, x=0.8cm, y=0.3cm, label style={font={\footnotesize \itshape}}, tick label style={font={\footnotesize \itshape}}, nodes near coords, nodes near coords align={vertical}, ] \addplot [fill=orange,] coordinates { (0,3) (1,13) (2,8) (3,3) (4,1) }; \end{axis} \end{tikzpicture} \end{document}
- PGFplot ybar stacked with meta databy Paul on July 16, 2026 at 4:11 pm
I want to use ybar stacked to plot data for multiple series. I first tried to organise my data.dat file like that : { config; value1; value2; value3 config1; 3; 4.2; 1 config2; 3.25; 6; 0.758 } with for each row the series symbolic name and all the values. I then used a for loop to plot each column. This worked fine and all the data were stacked correctly. But I also need for each data point to have specific label and color meta data like in my MWE : \documentclass[class=article, crop=true,varwidth=false]{standalone} \usepackage{tikz,pgfplots} \pgfplotsset{compat=1.18,width=15cm,height = 5.5cm} \begin{document} \begin{tikzpicture} \begin{axis}[ axis y line* = left,axis x line* = bottom, ybar stacked, table/col sep=semicolon, symbolic x coords={config1,config2}, xtick = data, ] \addplot+[nodes near coords,point meta=explicit symbolic,] table [trim cells=true,y expr=\thisrow{value}, x=config,meta = label ] { config; value; type; label config1; 3; L; $W_{L1}$ config1; 4.2; C; $W_{C1}$ config1; 1; L; $W_{L2}$ config2; 3.25; L; $W_{L1}$ config2; 6; C; $W_{C1}$ config2; 0.758; L; $W_{C2}$ }; \end{axis} \end{tikzpicture} \end{document} This is compiling but data are not stacked. PGFplog interprets each row as a new serie and plot the bar from 0. What would be the easiest way to plot the stacked bar with meta data for each point ? Thank for your help !
- Controlling what appears in front in a 3d pgfplotby James on July 12, 2026 at 6:00 pm
I'm trying to make a nice diagram to illustrate partial derivatives for my multivariable class. I have the graph of a function, a plane, the intersection of these in the graph, a tangent line, and some guide lines to show what's occurring, but (presumably because of the order in which they appear in the code) the diagram doesn't respect what object is behind a different object, i.e., the graph is drawn on top of the plane, or the plane is drawn on top of the graph, even though they ``should'' pass through one another. Is there any nice way to make whatever ``should'' be on top be drawn on top without manually finding intersections and having different domains/clip regions for different features? My code so far is below, along with the image that is produced \begin{standalone} \usepackage{tikz} \usepackage{pgfplots} \begin{document} \begin{tikzpicture} \begin{axis}[axis lines = center, xmin = 0, xmax = 3, ymin = 0, ymax = 3, view = {100}{30}, xlabel = {$x$}, ylabel = {$y$}, zlabel = {$z$}, xtick=\empty, ytick=\empty, ztick=\empty, ] % Using f(x,y) = 2 - (x-2)^3 - (y-1.5)^2 %graph \addplot3[ domain =1:3, y domain = 0.5:2.5,surf,colormap/viridis, opacity=0.9] {-(x-2)^3 - (y-1.5)^2 + 2}; %plane \draw[fill = blue!90, color = blue, opacity = 0.3] (2,0,0)--(2,3,0)-- (2,3,3) node[black, above, opacity = 0.9] {$x=a$}--(2,0,3)--cycle ; %intersection of line and plane \addplot3[domain = 0.5:2.5, blue, samples y = 1, thick] ({2},{x},{2 - (2-2)^3 - (x-1.5)^2}); %tangent line \addplot3[black, thick, domain = -1:1.23] ({2},{x+1},{-2*(1-1.5)*x + 2 - (1-1.5)^2}); %point of tangancy \draw[fill = black] (2,1, {2 - (1-1.5)^2}) circle (2pt); %guide lines for point \draw[dashed, thick] (2,0,0)--(2,1,0) node[below] {$(a,b,0)$}--(0,1,0); \draw[dashed, thick,] (2,1,0) -- (2,1,{2 - (1-1.5)^2}); \end{axis} \end{tikzpicture} \end{document}
- how to align axis-coordinate system with tikzpicture-coordinate systemby flawr on June 30, 2026 at 3:13 pm
I'd like to use a pgfplots axis environment to render some plots. I'd like them to be aligned with the surrounding TiKz graphics. However, I have not yet managed to find the correct options. I'm also only interested in the plotted line itself, so no ticks or axes need to be drawn in addition. So far I have this (failed) example. My goal would be aligning the coordinate system the axis environment such that the two curves are right on top of each other. With the width/height I assumed I could set the exact dimensions, but that already seems to be off. Is there a way to modify this axis-environment to match the coordinate system of the tikzpicture-environment? \documentclass{standalone} \usepackage{tikz} \usepackage{pgfplots} \pgfplotsset{compat=1.18} \begin{document} \begin{tikzpicture} \draw[lightgray] (-2, -3) grid (4, 4); \fill[lightgray] (0,0) circle (0.2); % origin \draw[green] plot [red, domain=-2:4, samples=200] (\x, {exp(-abs(\x)^2)}); \begin{axis}[ xtick=\empty, ytick=\empty, hide axis, xmin=-2, xmax=4, axis equal, width=6cm, height=7cm, anchor=south west, at={(-2cm,-2cm)}, ] \addplot [red, domain=-2:5, samples=200] (\x, {exp(-abs(\x)^2)}); \end{axis} \end{tikzpicture} \end{document}
- `memoize` and `PGFPlots`: multiple graphs get "merged" into one?by Thibault de Villèle on June 29, 2026 at 12:08 pm
I've got a bunch of figures which are PGFPlots elements side by side, as such: \begin{figure}[ht]% \centering% \begin{subfigure}{.49\textwidth}% \centering% \sizedplot{../path/to/plot-dir/bunny-multires}{p1-rmse.tikz}% \end{subfigure}\hfill% \begin{subfigure}{.49\textwidth}% \centering% \sizedplot{../path/to/plot-dir/bunny-multires}{p1-times-graph.tikz}% \end{subfigure}\\[1pt]% % Note: the `bunny-multires-plot` key is defined as the "legend name" in p1-rmse.tikz: \pgfplotslegendfromname{bunny-multires-plot}% \caption{Blah, blah, blah.}% \label{fig/bunny-multires}% \end{figure}% Where the \sizedplot command is defined as such: \NewDocumentCommand{\sizedplot}{s O{\textwidth} m m}{% \bgroup% \def\basepath{#3}% \def\fullpath{\basepath/#4}% \mmznext{meaning to salt={\fullpath}}% \begin{memoize}% \IfBooleanTF{#1}% If star, do not resize to #2. {\begin{tikzpicture}\input{\fullpath}\end{tikzpicture}}% {\resizebox{#2}{!}{\begin{tikzpicture}\input{\fullpath}\end{tikzpicture}}}% \end{memoize}% \egroup% }% This indirection is mainly due to the fact I include data from CSV files relative to the path of the plots, like: \addplot table [options] {\basepath/losses.csv}. When doing this, and enabling external compilation through the use of the memoize package, both graphs (the subfigure elements) are detected as the same "source", leading to one of the graphs (often the second one in the order they were defined) being copied in both sub-figures after memoization + recompilation. What could go wrong here? I've added -- as shown above -- the full path as a source for the salt in memoize's options, hoping they would thus get detected as two different graphs, but nothing seems to make that specific use case work. If I only have one TikZ / PGFPlots image in a figure environment, it compiles just fine. Here is a MWE, with all contents laid inline (sorry, the file is a bit long as a result): \documentclass{scrarticle} \usepackage{subcaption} \usepackage{siunitx} \usepackage{tikz} \usepackage{pgfplots} % IMPORTANT: switch this on/off to see the changes. \usepackage{nomemoize} %\usepackage{memoize} \NewDocumentCommand{\ValeurLegende}{m}{\num[output-exponent-marker=e,tight-spacing=true]{#1}}% \NewDocumentCommand{\ValeurAnnotation}{m}{\bgroup\scriptsize\num[output-exponent-marker=e,tight-spacing=true]{#1}\egroup}% \mmzset{padding=0pt, include context in ccmemo, trace} \pgfplotsset{% rmse/.style={table/col sep=comma,table/x={iteration}, table/y={classic}},% timings/.style={table/col sep=comma,table/x={iteration}, table/y={time-ms}},% }% \NewDocumentCommand{\sizedplot}{s O{\textwidth} m m}{% \bgroup% \def\basepath{#3}% \def\fullpath{\basepath/#4}% \mmznext{meaning to salt={\fullpath}}% \mmznext{meaning to context={\fullpath}}% \begin{memoize}% \IfBooleanTF{#1}% If star, do not resize to #2. {\begin{tikzpicture}\input{\fullpath}\end{tikzpicture}}% {\resizebox{#2}{!}{\begin{tikzpicture}\input{\fullpath}\end{tikzpicture}}}% \end{memoize}% \egroup% }% \begin{filecontents}{inline-loss-1.csv} iteration,time-ms,loss-abs,loss-cen,loss-fro 0,217.78973,9.07334387e-02,3.75475502e-04,2.29247450e-03 1,13.39216,8.98691937e-02,3.73302639e-04,3.80451954e-03 2,11.54511,8.92234743e-02,3.69715272e-04,4.83295647e-03 3,11.24531,8.90074968e-02,3.68322566e-04,5.51446620e-03 4,11.06645,8.86987820e-02,3.66538385e-04,6.02372689e-03 5,11.16146,8.89326632e-02,3.68111941e-04,6.37550279e-03 6,11.35311,8.90174806e-02,3.68967390e-04,6.63577626e-03 \end{filecontents} \begin{filecontents}{inline-loss-7.csv} iteration,time-ms,loss-abs,loss-cen,loss-fro 0,440.79675,4.83646780e-01,8.67330883e-08,4.42878436e-03 1,120.62730,4.86005545e-01,9.31322575e-10,5.97733771e-03 2,121.23386,4.86002266e-01,9.31322575e-10,6.64955098e-03 \end{filecontents} \begin{filecontents}{inline-rmse-1.csv} iteration,classic,normalized,normalizedpercent 00000,2.43285656e-01,1.58218700e+00,158.22 00001,8.82571621e-04,2.21190803e-01,22.12 00002,8.10880039e-04,2.27497900e-01,22.75 00003,7.84764765e-04,2.19129181e-01,21.91 00004,7.73038832e-04,2.14945569e-01,21.49 00005,7.67099671e-04,2.28823943e-01,22.88 00006,7.63582473e-04,2.29091554e-01,22.91 \end{filecontents} \begin{filecontents}{inline-rmse-7.csv} iteration,classic,normalized,normalizedpercent 00000,2.43285656e-01,1.58218700e+00,158.22 00001,7.45367841e-04,1.91473720e-01,19.15 00002,6.76008989e-04,1.71335431e-01,17.13 \end{filecontents} \begin{filecontents}{inline-rmse.tikz} \begin{semilogyaxis}[% width=7cm,% height=5cm,% legend columns=7,% legend to name={plot-loss},% xtick pos=bottom,% ytick pos=left,% ylabel={\texttt{Losses}},% ]% \addplot table [rmse] {\basepath/inline-rmse-7.csv};% \addlegendentry{\(\epsilon=\ValeurLegende{1e-7}\)}% \addplot table [rmse] {\basepath/inline-rmse-1.csv};% \addlegendentry{\(\epsilon=\ValeurLegende{1e-1}\)}% \end{semilogyaxis}% \end{filecontents} \begin{filecontents}{inline-timings.tikz} \begin{semilogyaxis}[% width=7cm, height=5cm,% xtick pos=bottom, ytick pos=left,% ylabel={\(t_i~(\unit{\milli\second})\)}, log ticks with fixed point,% ]% \addplot table [timings] {\basepath/inline-loss-7.csv};% \addplot table [timings] {\basepath/inline-loss-1.csv};% \end{semilogyaxis}% \end{filecontents} \begin{document} \begin{figure}[ht]% \centering% \begin{subfigure}{.49\textwidth}% \centering% \sizedplot{.}{inline-rmse.tikz}% \end{subfigure}\hfill% \begin{subfigure}{.49\textwidth}% \centering% \sizedplot{.}{inline-timings.tikz}% \end{subfigure}\\[1pt]% \pgfplotslegendfromname{plot-loss}% \caption{Blah, blah, blah.}% \label{fig/losses}% \end{figure}% \end{document} Passing include context in ccmemo and trace both helped me discover memoize does not expand the macro \fullpath when trying to memoize the second graph (inline-timings.tikz). Instead, it only receives this bit of code to generate the hash: Code: \IfBooleanTF {\BooleanFalse }{\begin {tikzpicture}\input {\fullpath }\end {tikzpicture}}{\resizebox {\textwidth }{!}{\begin {tikzpicture}\input {\fullpath }\end {tikzpicture}}} And since it is the same for both pictures, this means the second graph (encountered later) will be saved under the "common" hash when compiling for the first time, leading to the graphs being the same on the final PDF... Now that I've discovered the cause, I need to dig deeper into why it happens 🙂
- PGFPlots / TikZ: Filtering data from a file when the x-axis contains string valuesby joseldsm on June 10, 2026 at 9:57 am
I am generating a plot in PGFPlots/TikZ where the x-axis contains string values and the y-axis contains numerical data. The data is imported from a CSV file, and everything works correctly so far. However, when I use skip coordinates between index, the filtering is applied to the y-values and not to the x-axis categories. In this case, I want to draw only the data with the string value B. Is there a way to correctly apply this filtering to both x-axis ? Thanks! Here is the MWE. Keep in mind that in my case the data are extract from csv file. \documentclass{article} \usepackage{pgfplots} \usepackage{pgfplotstable} \pgfplotsset{compat=1.18} \pgfplotstableread{ String Value A 0 A 6 A 4 B 9 B 5 B 1 B 3 B 7 C 4 C 7 C 9 C 3 }\data \begin{document} \begin{tikzpicture} \begin{axis}[ ybar, ymin=0, xtick=data, xticklabels from table={\data}{String}, nodes near coords ] \addplot table[ x expr=\coordindex, y={Value}, skip coords between index={0}{3}, skip coords between index={8}{100} ]{\data}; \end{axis} \end{tikzpicture} \end{document}
- How to improve graphs of functions of two variables with jagged edges in pgfplots?by orion2112 on January 9, 2026 at 10:19 pm
In the spirit of creating "beautiful plots" of functions of two variables, I am reaching out to this community to try and find possible improvements in the way I have been using pgfplots and \addplot3. Here are 3 attempts at graphing the paraboloid $f(x,y)=x^2+y^2$. Attempt 1: plotting {x^2+y^2} in cartesian coordinates with no restriction on the z domain yields a plot that "hides part of itself" and doesn't readily show the "bowl" shape we all love: \documentclass[tikz,12pt]{standalone} \usepackage{tikz} \usepackage{pgfplots} \usepgfplotslibrary{colorbrewer} %For the colormap \begin{document} \begin{tikzpicture} \begin{axis}[ axis line style={very thick}, axis on top=false, xlabel={$x$}, ylabel={$y$}, zlabel={$z$}, zlabel style={rotate=90}, xtick={-3,-2,-1,0,1,2,3}, ytick={-3,-2,-1,0,1,2,3}, ztick={-3,-2,-1,0,1,2,3}, tick label style={font=\scriptsize}, no marks, xmin=-3, xmax=3, ymin=-3, ymax=3, zmin=-3, zmax=3, view={130}{30}, title={Smooth and ugly} ] \addplot3[ surf, samples=40, colormap/PuBu-9, shader=flat, domain=-3:3, y domain=-3:3, opacity=0.5 ] {x^2+y^2}; \end{axis} \end{tikzpicture} \end{document} Attempt 2: adding the "restrict z to domain=-3:3" option, but this produces a very jagged top edge to the paraboloid, and not a nice smooth circle. Adding samples reduces the jaggedness slightly but there is a limit to how many samples I can put before I get an error, and that isn't enough to get a smooth circle. \documentclass[tikz,12pt]{standalone} \usepackage{tikz} \usepackage{pgfplots} \usepgfplotslibrary{colorbrewer} %For the colormap \begin{document} \begin{tikzpicture} \begin{axis}[ axis line style={very thick}, axis on top=false, xlabel={$x$}, ylabel={$y$}, zlabel={$z$}, zlabel style={rotate=90}, xtick={-3,-2,-1,0,1,2,3}, ytick={-3,-2,-1,0,1,2,3}, ztick={-3,-2,-1,0,1,2,3}, tick label style={font=\scriptsize}, no marks, xmin=-3, xmax=3, ymin=-3, ymax=3, zmin=-3, zmax=3, view={130}{30}, title={Jagged edges at top} ] \addplot3[ surf, samples=40, colormap/PuBu-9, shader=flat, domain=-3:3, y domain=-3:3, restrict z to domain={-3:3}, opacity=0.5 ] {x^2+y^2}; \end{axis} \end{tikzpicture} \end{document} Attempt 3: using data cs=polar and a domain of 0:360 instead of -3:3 gets me to an acceptable graph that doesn't hide itself and that has a smooth border: \documentclass[tikz,12pt]{standalone} \usepackage{tikz} \usepackage{pgfplots} \usepgfplotslibrary{colorbrewer} %For the colormap \begin{document} \begin{tikzpicture} \begin{axis}[ axis line style={very thick}, axis on top=false, xlabel={$x$}, ylabel={$y$}, zlabel={$z$}, zlabel style={rotate=90}, xtick={-3,-2,-1,0,1,2,3}, ytick={-3,-2,-1,0,1,2,3}, ztick={-3,-2,-1,0,1,2,3}, tick label style={font=\scriptsize}, no marks, xmin=-3, xmax=3, ymin=-3, ymax=3, zmin=-3, zmax=3, view={130}{30}, title={Not jagged, but polar coords} ] \addplot3[ surf, samples=40, colormap/PuBu-9, shader=flat, data cs=polar, domain=0:360, y domain=-3:3, restrict z to domain={-3:3}, opacity=0.5 ] ({x},{y},{y^2}); \end{axis} \end{tikzpicture} \end{document} Problem solved? Not really. What if now I want to plot f(x,y)=x/(y-x)? If done in cartesian coordinates without restricting the z domain, the graph "hides itself". If done in cartesian coordinates and restricting the z domain, some of the edges are jagged. For instance: (I seem to notice that if the mesh line is parallel to one of the "walls" of the plot box, there is no jagged edge, but if a "square" of the mesh is outside the z domain, the whole square is erased, and not just the part that is outside the z domain). And here I don't see how polar coordinates would be usable to bypass that problem. "But, this is not a nice function, of course the graph will be ugly". Fair enough, but seeing that Desmos does it instantly and even allows you to rotate the plot smoothly, I was hoping that maybe it's possible to do better here: So, is there anything that can be done about this? Am I using this wrong? Or is it just a limitation of plotting surfaces in LaTeX? Thanks in advance for all the insight. Note: I am aware of the topic here and that it seems there are only rectangular (and not triangular) surface elements, but I am not sure the issue is the same nor how \clip and \begin{scope} were used to fix the issue. If this is the way to go, can someone explain what those commands do exactly?
- pgfplots / 2D contour filled plot from a csv fileby TheBeeTee on December 1, 2024 at 11:19 pm
Trying to plot a 2D colormap of my data like this: Here's my MWE with a sample of data. x,y are the coordinates. P is the value on which I want to base the shading. \documentclass[border=1 cm]{standalone} \usepackage{pgfplots} \begin{document} \begin{filecontents}{test.csv} x,y,P 0.6029680189,0.7383407645,2.3729000000 0.5323565117,0.6601831742,3.2726000000 0.4677196601,0.5882540709,4.0038000000 0.0946800948,0.6688882286,5.0510000000 0.1884371445,0.6355349568,5.0222000000 0.2792665104,0.5997515545,4.8900000000 0.3623180375,0.5641482523,4.6771000000 0.4853625211,0.1178246418,5.0150000000 0.4696592171,0.2354673908,4.9738000000 0.4525723312,0.3510530681,4.8490000000 0.4353225341,0.4584688067,4.6566000000 \end{filecontents} \begin{tikzpicture} \begin{axis}[ view = {0}{90}, colorbar, ] \addplot3[ contour filled={number = 20} ] table[x=x, y=y, z=P, col sep=comma]{test.csv}; \end{axis} \end{tikzpicture} \end{document} The code above was throwing: ERROR: shader=interp: got unsupported pdf shading type '0'. *****After more searching, I modified my MWE as follows. I don't get any errors, I just get an empty plot 🙁 \documentclass[border=1 cm]{standalone} \usepackage{pgfplots} \usepgfplotslibrary{patchplots} \begin{document} \begin{filecontents}{test.csv} x,y,P 0.6029680189,0.7383407645,2.3729000000 0.5323565117,0.6601831742,3.2726000000 0.4677196601,0.5882540709,4.0038000000 0.0946800948,0.6688882286,5.0510000000 0.1884371445,0.6355349568,5.0222000000 0.2792665104,0.5997515545,4.8900000000 0.3623180375,0.5641482523,4.6771000000 0.4853625211,0.1178246418,5.0150000000 0.4696592171,0.2354673908,4.9738000000 0.4525723312,0.3510530681,4.8490000000 0.4353225341,0.4584688067,4.6566000000 \end{filecontents} \begin{tikzpicture} \begin{axis}[ %view = {0}{90}, colorbar, ] \addplot[contour filled={number = 8,labels={false}}, mesh/rows=100,mesh/cols=100,mesh/check=false, patch type=bilinear, point meta=explicit, ] table [x=x, y=y, z=P, col sep=comma]{test.csv}; \end{axis} \end{tikzpicture} \end{document}
- How to add space between x axis labels and draw a vertical line after a set of IDs?by Student on November 21, 2024 at 8:13 pm
I'm trying to add space between x axis labels 10 Flows, 20 Flows, and 30 Flows and also properly draw a line after for every 1 to 8 IDs. Could someone help me with this problem? Code: % restructured data file: % - 'n' values are sorted ascending % - added column 'z' to know which data belong to which measurement % - added a dummy line at the end of each data set to produce an empty entry % for the separation of the blocks % - put all data in one file \begin{filecontents}{data.txt} z n pFA pFB 10 1 6 1 10 2 7 1 10 3 8 4 10 4 9 7 10 5 10 9 10 6 0 0 10 7 9 4 10 8 12 6 20 1 6 1 20 2 7 1 20 3 8 4 20 4 9 7 20 5 10 9 20 6 0 0 20 7 9 4 20 8 12 6 30 1 6 1 30 2 7 1 30 3 8 4 30 4 9 7 30 5 10 9 30 6 0 0 30 7 9 4 30 8 12 6 \end{filecontents} \documentclass[border=5pt]{standalone} \usepackage{pgfplots} \usetikzlibrary{patterns} \pgfplotsset{compat=1.3} \begin{document} \begin{tikzpicture} \begin{axis}[ footnotesize, % set the `width' of the plot to the maximum length ... width=\textwidth, % ... and use half this length for the `height' height=0.5\textwidth, ymin=0, ymax=100, %yticklabel=\pgfmathprintnumber{\tick}\,$\%$, % use `data' for the positioning of the `xticks' ... xtick=data, % ... and use table data for labeling the `xticks' xticklabels from table={data.txt}{n}, % add extra ticks "at the empty entries to add the vertical lines extra x ticks={8,16}, % this ticks shouldn't be labeled ... extra x tick labels={}, % ... but grid lines should be drawn without the tick lines extra x tick style={ grid=major, major tick length=0pt, }, xlabel={X Axis Label}, ylabel={Y Axis Label}, % because of the category labels, shift the `xlabel' a bit down xlabel style={ yshift=-4ex, }, legend pos=north west, legend entries={ {\color{black}{Algorithm A}}, {\color{red}{Algorithm B}}, }, area legend, % adjust `bar width' so it fits your needs ... bar width=10pt, % ... and with that you also have to adjust the x limits enlarge x limits=0.05, % set `clip mode' to `individual' so the category labels aren't clipped away clip mode=individual, ] % plot the "red" ybars \addplot [ ybar, draw=black, pattern color=black, pattern=dots, ] table [ % use just the `coordindex' as x coordinate, % the correct labeling is done with `xticklabels from table' x expr=\coordindex, y=pFA, ] {data.txt}; % plot the "blue" ybars \addplot [ ybar, draw=red, pattern color=red, pattern=north east lines, ] table [ x expr=\coordindex, y=pFB, ] {data.txt}; % add the category labels \begin{scope}[ % because the reference point will be the lower axis line the % labels have to be moved a bit more down to don't overlap with % the `xticklabels' every label/.append style={ label distance=2ex, }, ] \node [label=below:10 Flows] at (axis cs:4,\pgfkeysvalueof{/pgfplots/ymin}) {}; \node [label=below:20 Flows] at (axis cs:12,\pgfkeysvalueof{/pgfplots/ymin}) {}; \node [label=below:30 Flows] at (axis cs:20,\pgfkeysvalueof{/pgfplots/ymin}) {}; % \node [label=below:40 Flows] % at (axis cs:20,\pgfkeysvalueof{/pgfplots/ymin}) {}; %\node [label=below:50 Flows] % at (axis cs:26,\pgfkeysvalueof{/pgfplots/ymin}) {}; \end{scope} \end{axis} \end{tikzpicture} \end{document} Output:
- Tikz externalization with luacodeby pschulz on October 23, 2024 at 1:57 pm
I'm using luacode environments to assemble plot code for pgfplots. For many plots that works perfectly fine even with tikzexternalize. However, I have one plot where it does not work and the error message is cryptic. I reduced it to: \documentclass{article} \usepackage{pgfplots} \usepackage{luacode} \usetikzlibrary{external} \tikzexternalize \begin{document} %\tikzexternaldisable % doesn't compile with externalization, don't know why \begin{tikzpicture} \begin{axis} \begin{luacode*} local t = {} table.insert(t, "(0, 0)") table.insert(t, "(1, 1)") tex.print("\\addplot coordinates {") tex.print(table.concat(t)) tex.print("};") \end{luacode*} \end{axis} \end{tikzpicture} \end{document} So I used to get an error message like this: File ended while scanning use of \tikzexternal@laTeX@collect@until@end@tikzpicture I thought it might be related to a percent sign in the lua code (which I used for string.format), but I removed them. Now the strings are assembled a bit more manually. The problem persists, however, I don't get an error message any more. It just fails and it also does not create a main-figure0.log file, only the .md5 file. So what is going on?
- Prevent connection of ends of a partial circle in a 3D tikz pictureby Melissa on April 3, 2024 at 5:00 pm
I want to draw a 3-dimensional parabola (i.e. plot $z=x^2 + y^2$) with maximal value of z=1 and then mark the part of the parabola where $x^2 + y^2 =1$, $x\leq 0$ and $y \leq 0$, which is part of a circle. I have the following code: \documentclass{article} \usepackage{pgfplots} \begin{document} \begin{tikzpicture}[radius=1] \begin{axis}[ xlabel=$l_1$, ylabel=$l_2$, zlabel=$u$, xmin=-1, xmax=1, ymin=-1, ymax=1, zmin=-0.1, zmax=1.05, % x={(-0.125cm,-0.05cm)}, % y={(0.125cm,-0.05cm)}, % z={(0cm,0.05cm)}, axis lines=middle, every axis x label/.style={ at={(ticklabel* cs:1.05)}, }, every axis y label/.style={ at={(ticklabel* cs:1.05)}, }, every axis z label/.style={ at={(ticklabel* cs:1.05)}, }, colormap/blackwhite,] % Paraboloid \addplot3[ surf, samples=50, shader=interp, draw=lightgray, ultra thin, opacity=0.5, fill opacity=0.5, data cs=polar, domain=0:360, y domain=0:1, restrict z to domain=0:1, ](x, y, y^2); % Part of parabola \addplot3[ surf, samples=50, shader=interp, draw=lightgray, fill=green, ultra thin, left color=green, right color=green, middle color=green!25, data cs=polar, domain=180:270, y domain=0:1, restrict z to domain=0:1, ](x, y, y^2); %partial circle \addplot3[ domain=180:270, samples=20, variable=\t, color=red, untra thin, ] ({cos(\t)}, {sin(\t)}, {1}); \end{axis} \end{tikzpicture} \end{document} I do not want to connect the end points of the partial circle. How do I prevent this from happening?
- ERROR: Missing number, treated as zeroby RIRM506 on November 3, 2023 at 3:03 am
I can't find the problem in the code, but it says at the end of that "Missing number, treated as zero" \begin{tikzpicture}[line cap=round,line join=round,>=stealth,x=1.0cm,y=1.0cm] \begin{axis}[ x=1.5cm,y=1.5cm, axis lines=middle, xlabel=$x$, ylabel=$y$, xmin=-0.5, xmax=6.0, ymin=-0.5, ymax=4.5, xticklabels={,,1,,,4}, ytick={\empty},] \clip(-0.5,-0.5) rectangle (6.,4.5); \fill[fill=black,fill opacity=0.20000000298023224] (1.0045836142718776,1.4243770120110515) -- (1.0045836142718776,0.) -- (4,0) -- (3.996318624435588,0.5559251076370629) -- (3.764797284337927,0.5803447447339725) -- (3.5176114060500536,0.609277926604037) -- (3.0369721982680775,0.676251412660451) -- (2.7554549479957773,0.7294953956208646) -- (2.535734167295445,0.7672558117999624) -- (2.309147112198228,0.8185829277750029) -- (2.0550949595134687,0.8866827343500797) -- (1.8010428068287099,0.9697221311676422) -- (1.5401243797470656,1.0769669337835905) -- (1.3135373246498483,1.1965218380095286) -- (1.1487467391245991,1.306084296440851) -- cycle; \draw[line width=1.2,smooth,samples=100,domain=6-6:6.1] plot(\x,{10/(2*(\x)+5*sqrt((\x)))}); \draw [line width=1.2pt] (1.0045836142718776,1.4243770120110515)-- (1.0045836142718776,0); \draw [line width=1.2pt] (3.996318624435216,0.5559251076932408)-- (4.,0.); \draw (-0.4,0.02) node[anchor=north west] {$O$}; \draw (2.2424396505089295,0.6291874906703163) node[anchor=north west] {$\mathbf{R}$}; \end{axis} \end{tikzpicture}
- types of systems of linear equations and intersection of planes, with tikzby Juan Leal on July 22, 2023 at 2:57 pm
Is there a simple way using tikz to graph a plane from its equation (like those of the systems on the right side, e.g. 2x +3y -z = 11), and that allows me to represent the intersection of two or three of them (even four)? If possible, I additionally need it to allow me to have flat colors in each of the planes involved and not to be represented by pieces as suggested by some of the solutions I have seen. I want to represent graphically to my students all the possibilities that can be presented with a system of linear equations with three unknowns, in a way similar to the one in the image I include. I have been reading the material related to this topic for three days and I have not found a completely satisfactory solution (or maybe there is one, but I have not appreciated it properly). I know a bit about tikz, pgfplots and tikz-3dplot (and I come from the old school of pstricks).
- How to plot the 3D graph of the following three inequalities in latex (e.g., tikz/pgf)?by learner on July 9, 2021 at 9:27 am
How to plot the 3D graph of the following three inequalities in latex (tikz/pgf) ? z <= x+y+1, z <= 4x, z <= 5y. My attempt: \documentclass[12pt,leqno]{amsart} \usepackage{pgfplots} \usepackage{tikz} \begin{document} \begin{tikzpicture} \begin{axis} \addplot3 [ domain=-5:20, domain y = -3:10, samples = 20, samples y = 8, surf] {x+y+1}; \addplot3[domain=-5:20, domain y = -3:10, samples = 20, samples y = 8, surf, opacity=0.25]{4*x}; \addplot3[domain=-5:20, domain y = -3:10, samples = 20, samples y = 8, surf, opacity=0.25]{5*y}; \end{axis} \end{tikzpicture} \end{document} The above Latex code produces the above figure: But the above graph is not clear, the intersecting line of the three half-planes are not clear. Any help to draw clear image with clear intersection. Any help both with the 3D graph of the given 3 inequalities. Thanks Edit: The 2D projection of the above 3 inequalities becomes: and this is justified by the answer of @Juan Castaño.
- How do I crop a 3D surface in pgfplots?by Lucas Abounader on February 20, 2021 at 2:00 am
I am attempting to reproduce the following graph using pgfplots: Here is the MWE code I developed, along with the corresponding plot generated: \documentclass{article} \usepackage[utf8]{inputenc} \usepackage{pgfplots} \pgfplotsset{compat=1.17} \begin{document} \begin{center} \begin{tikzpicture} \begin{axis}[ width=8cm, height=12cm, axis lines=center, view={120}{30}, mesh/interior colormap name=hot, colormap/cool, point meta min=0.5, point meta max=6, xmin=-2, xmax=2, xlabel=\Large{$s$}, ymin=-0.5, ymax=2.5, ylabel=\large{$t$}, zmin=0, zmax=6, ] \addplot3[ surf, shader=interp, samples=50, domain=-2:2, y domain=-0.5:2.5 ] {14*x*x+20*x*y-22*x+11*y*y-24*y+14}; \end{axis} \end{tikzpicture} \end{center} \end{document} There are a few issues: The view box does not crop the plot as I would like. I am looking for the elliptic intersection with the plane z=6 akin to the first image. The interior surface (colored by mesh/interior colormap name=hot) is not visible. pgfplots is giving the following warning: Package pgfplots Warning: the combination 'shader=interp,interior colormap name ' is inefficient (it uses 'shader=faceted interp,faceted color=none') and may s low down the *display* of your document on input line 44. I only know the command shader=interp to remove mesh gridlines and I would like to shade the interior and exterior surfaces with different colormaps. What is going on? How can I get the desired "crop" effect? For reference, here is the surface rendered with gridlines:
- Plotting 3d surface from data pointsby NaveganTeX on July 26, 2019 at 7:47 pm
I have the following code which works great and produces the plot I want. This is it: \documentclass[border=5mm]{standalone} \usepackage{pgfplots, filecontents} \begin{filecontents*}{filename.txt} 0 0 -0.64 1 0 -0.5 2 0 -0.4 3 0 -0.31 4 0 -0.23 5 0 -0.2 6 0 -0.24 7 0 -0.32 8 0 -0.42 9 0 -0.59 10 0 -0.76 0 1 -0.54 1 1 -0.4 2 1 -0.27 3 1 -0.17 4 1 -0.08 5 1 -0.03 6 1 -0.05 7 1 -0.14 8 1 -0.27 9 1 -0.45 10 1 -0.63 0 2 -0.46 1 2 -0.31 2 2 -0.16 3 2 -0.03 4 2 0.07 5 2 0.12 6 2 0.11 7 2 0.02 8 2 -0.13 9 2 -0.32 10 2 -0.51 0 3 -0.44 1 3 -0.26 2 3 -0.08 3 3 0.07 4 3 0.18 5 3 0.25 6 3 0.24 7 3 0.14 8 3 -0.01 9 3 -0.2 10 3 -0.41 0 4 -0.44 1 4 -0.26 2 4 -0.04 3 4 0.13 4 4 0.25 5 4 0.32 6 4 0.31 7 4 0.22 8 4 0.07 9 4 -0.14 10 4 -0.37 0 5 -0.44 1 5 -0.26 2 5 -0.05 3 5 0.14 4 5 0.27 5 5 0.34 6 5 0.33 7 5 0.24 8 5 0.09 9 5 -0.12 10 5 -0.36 0 6 -0.48 1 6 -0.29 2 6 -0.08 3 6 0.09 4 6 0.22 5 6 0.29 6 6 0.28 7 6 0.2 8 6 0.06 9 6 -0.13 10 6 -0.35 0 7 -0.55 1 7 -0.34 2 7 -0.15 3 7 -0.0 4 7 0.11 5 7 0.18 6 7 0.18 7 7 0.11 8 7 -0.02 9 7 -0.2 10 7 -0.39 0 8 -0.63 1 8 -0.44 2 8 -0.27 3 8 -0.13 4 8 -0.03 5 8 0.04 6 8 0.04 7 8 -0.02 8 8 -0.13 9 8 -0.29 10 8 -0.45 0 9 -0.71 1 9 -0.59 2 9 -0.43 3 9 -0.28 4 9 -0.18 5 9 -0.13 6 9 -0.13 7 9 -0.17 8 9 -0.26 9 9 -0.38 10 9 -0.53 0 10 -0.83 1 10 -0.75 2 10 -0.59 3 10 -0.45 4 10 -0.35 5 10 -0.3 6 10 -0.3 7 10 -0.34 8 10 -0.42 9 10 -0.52 10 10 -0.66 \end{filecontents*} \begin{document} \begin{tikzpicture} \begin{axis}[view={-20}{20}, grid=both] \addplot3[surf] file {filename.txt}; %\addplot3[surf, point meta=explicit] table [z expr=0.5, meta index=2] {filename.txt}; \end{axis} \end{tikzpicture} \end{document} Now, my data points are the following: \documentclass[border=5mm]{standalone} \usepackage{pgfplots, filecontents} \begin{filecontents*}{filename.txt} 10 0 2.8637 15 0 2.8131 20 0 2.8727 25 0 2.8644 30 0 2.8842 35 0 2.8675 40 0 2.7089 45 0 2.6247 10 0.001 2.8436 15 0.001 2.8692 20 0.001 2.8859 25 0.001 2.4132 30 0.001 2.7664 35 0.001 2.8796 40 0.001 2.8752 45 0.001 2.8782 10 0.002 2.8752 15 0.002 2.8693 20 0.002 2.8703 25 0.002 2.6594 30 0.002 2.8643 35 0.002 2.5554 40 0.002 2.8875 45 0.002 2.8887 10 0.003 2.8151 15 0.003 2.8856 20 0.003 2.8701 25 0.003 2.857 30 0.003 2.8785 35 0.003 2.6176 40 0.003 2.8835 45 0.003 2.9234 10 0.004 2.8815 15 0.004 2.8792 20 0.004 2.8213 25 0.004 2.8877 30 0.004 2.9202 35 0.004 2.8595 40 0.004 2.885 45 0.004 2.8489 10 0.005 2.8907 15 0.005 2.8521 20 0.005 2.6148 25 0.005 2.8762 30 0.005 2.8576 35 0.005 2.8363 40 0.005 2.7048 45 0.005 2.7617 10 0.006 2.8599 15 0.006 2.8941 20 0.006 2.8474 25 0.006 2.876 30 0.006 2.8206 35 0.006 2.5735 40 0.006 2.3208 45 0.006 2.3754 10 0.007 2.8833 15 0.007 2.8886 20 0.007 2.8691 25 0.007 2.6355 30 0.007 2.7528 35 0.007 2.5829 40 0.007 2.583 45 0.007 2.5111 10 0.008 2.8711 15 0.008 2.8815 20 0.008 2.8452 25 0.008 2.7511 30 0.008 2.5907 35 0.008 2.5598 40 0.008 2.6029 45 0.008 2.5253 10 0.009 2.8708 15 0.009 2.6844 20 0.009 2.7038 25 0.009 2.6988 30 0.009 2.4988 35 0.009 2.4059 40 0.009 1.8292 45 0.009 2.2368 10 0.01 2.8806 15 0.01 2.8899 20 0.01 2.386 25 0.01 2.5372 30 0.01 2.3707 35 0.01 2.1165 40 0.01 2.1553 45 0.01 2.3411 10 0.011 2.8725 15 0.011 2.7136 20 0.011 2.5851 25 0.011 2.4942 30 0.011 2.478 35 0.011 2.3578 40 0.011 2.465 45 0.011 2.0726 10 0.012 2.8873 15 0.012 2.6652 20 0.012 2.6406 25 0.012 2.4053 30 0.012 2.3922 35 0.012 2.3308 40 0.012 2.3423 45 0.012 2.0778 10 0.013 2.8902 15 0.013 2.7437 20 0.013 2.6815 25 0.013 2.2707 30 0.013 2.1961 35 0.013 2.1585 40 0.013 2.2469 45 0.013 2.3896 10 0.014 2.8826 15 0.014 2.6679 20 0.014 2.4037 25 0.014 2.2751 30 0.014 2.3569 35 0.014 2.3838 40 0.014 2.2706 45 0.014 2.2171 10 0.015 2.8688 15 0.015 2.5697 20 0.015 2.4726 25 0.015 2.2542 30 0.015 2.344 35 0.015 2.3119 40 0.015 2.0676 45 0.015 2.2591 \end{filecontents*} \begin{document} \begin{tikzpicture} \begin{axis}[view={-20}{35}, grid=both] \addplot3[surf] file {filename.txt}; % \addplot3[surf, point meta=explicit] table [z expr=0.2, meta index=2] {filename.txt}; \end{axis} \end{tikzpicture} \end{document} And get the following disaster: Could you tell me whats going on and how could I fix this issue? I need to fix the scale where it says 10^-2. I want to have on the x axis from 0 to 0.015 with steps of 0.001. This is the desired output:
- Creating a surface plot of a function on the simplex (using PSTricks or TikZ?)by Martin J. Osborne on March 18, 2017 at 6:46 pm
I'd like to create an exact surface plot of a function defined on the unit simplex {(x, y, z): x + y + z = 1}. Here's a sketch of the figure I'd like to create. I know about surface plots in PSTricks and TikZ, about barycentric coordinate systems in TikZ, and about ternaryaxis in pgfplots, but I don't see a way of combining them to produce what I want. (My difficulty is not with the specific form of the function; I'll be happy with an answer that plots the function x.) I'd like to say something like \documentclass{article} \usepackage{pgfplots} \begin{document} \begin{tikzpicture} \begin{axis} % or some special type of axis \addplot3 {<f(x,y,z)>}; \end{axis} \end{tikzpicture} \end{document} where f(x,y,z) is the definition of my function. Is something like that possible?
- How to deal in pgfplots with repeated symbolic coords using data from a fileby Yves on September 18, 2016 at 8:44 am
I am trying to generate a series of industrial plots resulting from a DoE, according to the example of the MWE, but I cannot find a way to generate it directly from a .csv file or \datatable, since the xticks gets duplicated for each occurence found in the .csv file. Is there a way to force all datapoints sharing the same symbolic xcoord to be aligned vertically, as in the example? \documentclass{article} \usepackage{pgfplots} \pgfplotsset{width=4cm, height =8cm} \usepackage{filecontents} \begin{document} \begin{tikzpicture} \begin{axis}[ title= {Fatigue test results.}, only marks, mark size=0.5mm, enlarge x limits=0.3, xlabel={Configuration},ylabel={Cycles before failure}, symbolic x coords={A,C}, xtick={A,C}] \addplot coordinates {(A,1362)(A,2840) (A,687) (A,2771)}; \addplot coordinates {(C,2130)(C,3544) (C,1844)(C,3447)}; \end{axis} \end{tikzpicture} \begin{filecontents}{data1.csv} config;cycles A;1362 A;2840 A;687 A;2771 C;2130 C;3544 C;1844 C;3447 \end{filecontents}{data1.csv} \pgfplotstableread[col sep=semicolon]{data1.csv}{\datatableone} % what code could lead to the same plot, using the file or the datatable ? % see below \begin{tikzpicture} \begin{axis}[ xtick=data, title= {Fatigue test results.}, only marks, mark size=0.5mm, enlarge x limits=0.3, xlabel={Configuration},ylabel={Cycles before failure}, symbolic x coords={A,C}] \addplot table [x= config, y=cycles] \datatableone; \end{axis} \end{tikzpicture} \end{document} I have found a solution and added it to the code.
- PGF: draw longitudinal arcs in 3D axis environmentby Bloch on February 4, 2015 at 3:54 pm
I want to draw a sphere similar to the first one here: http://www.texample.net/tikz/examples/map-projections/ But, of course, different. This is what I did so far: The dashed lines are behind the arrows and the solid lines on top to create the illusion of the arrows to be in the sphere. What I did is a bit tricky and probably not the most elegant method. I created a tilted sphere (surface plot) in the background and covered it with a better looking 'ball'. I originally intended the surface plot sphere to be the actual sphere and then decided that it did not look good enough. But I kept it in the background because I needed the axis environment as I did not manage to do it without the axis cs. The latitude lines are basically 360° arcs originating from the (axis cs: 1,0,0) point (arc (0:360:1)). It turned out that arc interpreted the angle as the polar angle (the angle in the x-y-plane), which made it easy. But it seems to make it impossible for longitude lines. Is there something like an arc3D knowing an azimuthal angle in addition to the polar angle? Or could I redraw the latitude lines and rotate them by 90 degrees? Does anyone know another solution? My attempts to do it without the axis environment also failed. Actually I am not unhappy with my axis solution, as it makes it easier to plot the arrows in the right direction (what it will be all about in the end). I hope I could make it clear what my problem is and hope there is someone coming up with an easy arc3D solution. 😉 My code is: \documentclass[tikz]{standalone} \usepackage{pgfplots} \pgfplotsset{compat=1.9} \usetikzlibrary{arrows.meta} \tikzset{>=Latex} \begin{document} \begin{tikzpicture} \def\tilt{45} \def\azimuth{30} \begin{axis}[% axis equal, width=14cm, height=14cm, hide axis, enlargelimits=0.3, view/h=\tilt, view/v=\azimuth, scale uniformly strategy=units only, colormap={bluewhite}{color=(blue) color=(white)}, ] \coordinate (X) at (axis cs: 1,0,0); \coordinate (-X) at (axis cs: -1,0,0); \coordinate (Y) at (axis cs: 0,1,0); \coordinate (-Y) at (axis cs: 0,-1,0); \coordinate (Z) at (axis cs: 0,0,1); \coordinate (-Z) at (axis cs: 0,0,-1); Kugel \addplot3[ surf, shader= interp, opacity = 1, samples=2, domain=-1:1,y domain=0:2*pi, z buffer=sort ] ({sqrt(1-x^2) * cos(deg(y))}, {sqrt( 1-x^2 ) * sin(deg(y))}, x); \filldraw[ball color=white] (axis cs: 0,0,0) circle (2.5cm); %dashed lines %Breitengrade \pgfplotsinvokeforeach {-80,-60,...,80}{ \pgfplotsextra{ \pgfmathsetmacro\sinVis{sin(#1)/cos(#1)*sin(\azimuth)/cos(\azimuth)} % angle of "visibility" \pgfmathsetmacro\angVis{asin(min(1,max(\sinVis,-1)))} \coordinate (X) at (axis cs: {cos(#1)},0,{sin(#1)}); \draw [gray,dashed] (X) arc (0:360:{100*cos(#1)}); } } %Achsen \draw [-{>[scale=2]},gray] (axis cs: -1,0,0) -- (axis cs: 1.5,0,0) node [above] {$\sigma_x$}; %x-Achse \draw [-{>[scale=2]},gray] (axis cs: 0,-1,0) -- (axis cs: 0,1.5,0) node [above] {$\sigma_y$}; %y-Achse \draw [-{>[scale=2]},gray] (axis cs: 0,0,-1) node [below]{$\sigma_-$} -- (axis cs: 0,0,1.3) node [above] {$\sigma_+$}; %z-Achse \draw [-{>[scale=1]},cyan,line width = 3pt] (axis cs: 0,0,0) -- (axis cs: 0,0,1.1); \draw [-{>[scale=1]},blue,line width = 3pt] (axis cs: 0,0,0) -- (axis cs: 1,0,0); %Arcs \pgfplotsinvokeforeach {-80,-60,...,80}{ \pgfplotsextra{ \pgfmathsetmacro\sinVis{sin(#1)/cos(#1)*sin(\azimuth)/cos(\azimuth)} % angle of "visibility" \pgfmathsetmacro\angVis{asin(min(1,max(\sinVis,-1)))} \coordinate (X) at (axis cs: {cos(#1)},0,{sin(#1)}); \draw [gray] (X) arc (0:\tilt+\angVis:{100*cos(#1)}) (X) arc (0:-180+\tilt-\angVis:{100*cos(#1)}); } } \end{axis} \end{tikzpicture} \end{document}
- How can I map a 3D plot into the planes using pgfplots?by enthu on October 27, 2014 at 12:49 pm
After answering a question in pgfplots, asking to plot a function; I thought about how I can map the 3D function into the planes. Something like the plots in this page but I could not reach any solution. Here are my functions and their output. I want to show the left plot in the xy plane of the 3d plot; or map the function on xz and yz planes. % pdflatex \documentclass[margin=2mm]{standalone} \usepackage{tikz} \usepackage{pgfplots} \pgfplotsset{compat=newest} \begin{document} \begin{tikzpicture} \pgfmathdeclarefunction{F}{3}{\pgfmathparse{#1* exp(#2*#3)}} \begin{axis} [ smooth, grid=both,minor tick num=1, xlabel=$x$,ylabel=$y$, tick align=inside, samples=1000, samples y=0, ] \addplot [solid, thick, data cs=polarrad, domain=0:10*pi] {F(5,-0.1,x)}; \end{axis} \end{tikzpicture} ~ \begin{tikzpicture} \pgfmathdeclarefunction{F}{3}{\pgfmathparse{#1* exp(#2*#3)}} \begin{axis} [smooth, grid=both,minor tick num=1, xlabel=$x$,ylabel=$y$,zlabel=$z$, samples=1000, samples y=0, ] \addplot3+ [solid, thick, black, mark=none, thick, domain=0:10*pi, ] ({F(5,-0.1,x)*cos(deg(x))},{F(5,-0.1,x)*sin(deg(x))},{F(5,-0.1,x)}); \end{axis} \end{tikzpicture} \end{document}
- Flip x and y axis of ternary graphby Raphael on July 18, 2014 at 4:27 am
I am trying to reproduce the USDA Soil Triangle. To do so, I would need to flip the x and y axis of the default pgfplots-ternary. I understand, that I could switch my inputs (don't really want to do that), but I would need to change the directions as well, and I not even having any success with that. In the default ternary axis, the y axis is on the left and the x axis is on the right, and x runs from 0 (bottom right) to 100 (top) and y runs from top to bottom left. However, according to the USDA Soil Triangle, x needs to run from bottom left to top, y needs to run from top to bottom right, and z from bottom right to bottom left. Also, the x axis (and ticks) need to be horizontal (parallel to z), whereas y needs to be parallel to x and z should be parallel to y. I tried the x dir=reverse and axis on top commands, as I thought I could simply flip my coordinates, but I had no luck. Did anybody ever have a similar problem or knows an answer to this? I did what I could to at least have all the data and such. Here is what I got: \documentclass[]{standalone} \usepackage[dvipsnames]{xcolor} % color handling \usepackage{tikz} % draw pictures and diagrams \usepackage{pgfplots} % proper plotting \usepackage{file contents} % writing reading of data files \usepgfplotslibrary{ternary, units} \begin{filecontents*}{USDASoils.csv} name,clay,silt,sand Sand,0,0,100 Sand,10,0,90 Sand,0,15,85 Loamy Sand,0,15,85 Loamy Sand,10,0,90 Loamy Sand,15,0,85 Loamy Sand,0,30,70 Clay,55,0,45 Clay,100,0,0 Clay,60,40,0 Clay,40,40,20 Clay,40,15,45 Sandy Clay,35,0,65 Sandy Clay,55,0,45 Sandy Clay,35,20,45 Silty Clay,40,40,20 Silty Clay,60,40,0 Silty Clay,40,60,0 Loam,8,40,52 Loam,20,27,53 Loam,27,28,45 Loam,27,50,23 Loam,8,50,42 Sandy Loam,0,30,70 Sandy Loam,15,0,85 Sandy Loam,20,0,80 Sandy Loam,20,27,53 Sandy Loam,8,40,52 Sandy Loam,8,50,42 Sandy Loam,0,50,50 Clay Loam,27,28,45 Clay Loam,40,15,45 Clay Loam,40,40,20 Clay Loam,27,53,20 Sandy Clay Loam,20,0,80 Sandy Clay Loam,35,0,65 Sandy Clay Loam,35,20,45 Sandy Clay Loam,27,28,45 Sandy Clay Loam,20,27,53 Silty Loam,0,50,50 Silty Loam,27,50,23 Silty Loam,27,73,0 Silty Loam,13,87,0 Silty Loam,13,80,7 Silty Loam,0,80,20 Silty Clay Loam,27,53,20 Silty Clay Loam,40,40,20 Silty Clay Loam,40,60,0 Silty Clay Loam,27,73,0 Silt,0,80,20 Silt,13,80,7 Silt,13,87,0 Silt,0,100,0 \end{filecontents*} \pgfplotsset{ discard if not/.style 2 args={ x filter/.code={ \edef\tempa{\thisrow{#1}} \edef\tempb{#2} \ifx\tempa\tempb \else \def\pgfmathresult{inf} \fi } } } \begin{document} \begin{tikzpicture}[scale=0.9] \begin{ternaryaxis}[ xmax=100, ymax=100, zmax=100, ternary limits relative, xlabel=x (Clay), ylabel=y (Silt), zlabel=z (Sand), xtick={0,10,...,100}, ytick={0,10,...,100}, ztick={0,10,...,100}, minor tick num=1, grid=both, ] \def\myopacity{0.5} % Sands \addplot3 [fill=yellow, fill opacity=\myopacity, discard if not={name}{Sand}] table [x=clay,y=silt,z=sand, col sep=comma] {USDASoils.csv}; \addplot3 [fill=Melon, fill opacity=\myopacity,discard if not={name}{Loamy Sand}] table [x=clay,y=silt,z=sand, col sep=comma] {USDASoils.csv}; % Sand Text \node at (axis cs:3,5,) {\tiny Sand}; \node at (axis cs:5,13) {\tiny Loamy}; \node at (axis cs:2,18) {\tiny Sand}; % Clays \addplot3 [fill=yellow, fill opacity=\myopacity, discard if not={name}{Clay}] table [x=clay,y=silt,z=sand, col sep=comma] {USDASoils.csv}; \addplot3 [fill=red, fill opacity=\myopacity, discard if not={name}{Sandy Clay}] table [x=clay,y=silt,z=sand, col sep=comma] {USDASoils.csv}; \addplot3 [fill=Aquamarine!50, fill opacity=\myopacity, discard if not={name}{Silty Clay}] table [x=clay,y=silt,z=sand, col sep=comma] {USDASoils.csv}; %Clay Text \node at (axis cs:65,16) {\tiny Clay}; \node at (axis cs:43,6) {\tiny Sandy}; \node at (axis cs:38,8) {\tiny Clay}; \node at (axis cs:48,47) {\tiny Silty}; \node at (axis cs:44,49) {\tiny Clay}; % Loams \addplot3 [fill=brown, fill opacity=\myopacity, discard if not={name}{Loam}] table [x=clay,y=silt,z=sand, col sep=comma] {USDASoils.csv}; \addplot3 [fill=purple!50, fill opacity=\myopacity, discard if not={name}{Sandy Loam}] table [x=clay,y=silt,z=sand, col sep=comma] {USDASoils.csv}; \addplot3 [fill=purple!25, fill opacity=\myopacity, discard if not={name}{Sandy Clay Loam}] table [x=clay,y=silt,z=sand, col sep=comma] {USDASoils.csv}; \addplot3 [fill=SpringGreen, fill opacity=\myopacity, discard if not={name}{Clay Loam}] table [x=clay,y=silt,z=sand, col sep=comma] {USDASoils.csv}; \addplot3 [fill=Green, fill opacity=\myopacity, discard if not={name}{Silty Loam}] table [x=clay,y=silt,z=sand, col sep=comma] {USDASoils.csv}; \addplot3 [fill=Aquamarine, fill opacity=\myopacity, discard if not={name}{Silty Clay Loam}] table [x=clay,y=silt,z=sand, col sep=comma] {USDASoils.csv}; % Loams Text \node at (axis cs:19,40) {\tiny Loam}; \node at (axis cs:12,23) {\tiny Sandy Loam}; \node at (axis cs:28,13) {\tiny Sandy Clay Loam}; \node at (axis cs:33,35) {\tiny Clay Loam}; \node at (axis cs:13,65) {\tiny Silty Loam}; \node at (axis cs:36,55) {\tiny Silty}; \node at (axis cs:31,58) {\tiny Clay Loam}; % Silts \addplot3 [fill=green, fill opacity=\myopacity, discard if not={name}{Silt}] table [x=clay,y=silt,z=sand, col sep=comma] {USDASoils.csv}; % Silt Text \node at (axis cs:6,88) {\tiny Silt}; \end{ternaryaxis} \end{tikzpicture} \end{document} Which looks like this: I am considerably happy with my result, but I would like to: Switch the axis (as explained above), Remove the minor ticks (but keep the minor grid - I could not get that to work), and have the axis labels sloped along the triangle sides (as in the original picture above)
- plot 3d graphs of functions defined implicitlyby Jay on October 23, 2013 at 2:52 pm
How can I plot 3d graphs of functions defined implicitly (quadratic forms, for Linear Algebra course notes -- I'd like to include lots of examples)? As far as I can see it is not possible with pgfplots and not through gnuplot either. Is there any package that will help with that? For example, parabolic and hyperbolic cylinders, hyperboloids, ellipsoids, etc. Concrete examples: 2xy + 2xz = 1 (hyperbolic cylinder) (x^2)/(2^2) + (y^2)/(3^2) + (z^2)/(2^2) = 1 (ellipsoid) I also see that Maxima can do this: (%11) hc:2*x*y+2*x*z=2; (%i2) draw3d(enhanced3d=true,implicit(hc,x,-5,5,y,-5,5,z,-5,5)); This will work fine (the hyperbolic cylinder is correctly plotted on the screen), but I don't know what backend Maxima uses for this, and I'd like to use a plain LaTeX method, or something that could be called from LaTeX, as I may have to send the document for others to compile themselves on different environments.
- Unwanted Line Drawn Between Ends of Curves in 3Dby Geoff Pointer on October 20, 2013 at 4:57 am
Okay, this was about one of the technical pitfalls and how to avoid it, which was solved quite easily by adding samples y=0 as recommended by Francis. Initial Question The following does two turns of a helix and then joins the start and finish with a straight line for some reason. Plus there's some nasty overlapping happening in the smoothing process. MWE \documentclass{standalone} \usepackage{amsmath} \usepackage{amssymb} \usepackage{pgfplots} \usepackage{mathtools} \pgfplotsset{compat=1.9} \begin{document} % \par\begin{tikzpicture} % \begin{axis}[ scale=3, x={(-0.6cm,-0.3cm)}, y={(.6cm,0.0cm)}, z={(0cm,.2cm)}, xlabel={$x$}, ylabel={$y$}, zlabel={$z$}, axis lines=middle, axis on top, xtick={-1,1}, ytick={-1,1},ztick={4,8}, enlargelimits=true ] \addplot3[smooth, thick, color=green, domain=0:720] ({2*sin(x)},{2*cos(x)},{2*x/180}); % \end{axis} % \end{tikzpicture} % \end{document}
- Fill a contour-plotby spookyfw on August 30, 2013 at 12:25 pm
I did some calculations in Mathematica that gave me a contour plot: To include the output I wanted to however draw it with pfgplot. But there I don't get it to work, first of all the y-axis doesn't start at 0 and second I cannot find a specifier to fill the areas between the lines. The part that is white in the plot I am trying to reproduce is an area of complex values that I wanted to exclude as well. My MWE up to now looks like \documentclass{standalone} \usepackage{pgfplots} \pgfplotsset{compat = 1.7} \begin{document} \begin{tikzpicture} \begin{axis}[ xlabel = $x$ , ylabel = $y$ , domain = 1:2 , y domain = 0:90 , view = {0}{90} ] \addplot3[ contour gnuplot={number = 30,labels={false}}, thick ]{-2.051^3*1000/(2*3.1415*(2.99*10^2)^2)/(x^2*cos(y)^2)}; \end{axis} \end{tikzpicture} \end{document} with the following output It should be a common problem but I couldn't find a solution. Another thing that I tried was using addplot3 with surf, but the way how the colors are put together didn't seem to work right \addplot3[surf,shader=interp,samples=2, patch type=bilinear]
- Intersection of two surfacesby aagaard on April 23, 2012 at 6:19 am
I'm trying to create a signed distance function S(x), where I want to intersect S(x) = 0 with a plane. The problem is not to get the two functions into a plot, but to get the two functions to visual intersect with each other, i.e the nearest surface is in the foreground. Currently, I have something like: \documentclass{standalone} \usepackage{pgfplots} \begin{document} \begin{tikzpicture} \begin{axis}[grid=major] \addplot3[surf,domain=-10:10,samples=40] {-sqrt((x-0)^2/1 + (y-0)^2/1) + 5}; \addplot3[surf,domain=-10:10,samples=2,opacity=0.5] {0*x+0*y}; \end{axis} \end{tikzpicture} \end{document} Which gives: As you can see, the plane is only overlapping the function and not intersecting. How can it be done? Bonus question: Is it possible to create a contour on the 3d surf plot at the intersection?