pgfplots
- Concentration/flux map sequence for diffusion (Fick's laws): stationary caseby Sebastiano on November 29, 2026 at 11:26 am
I would like to create a sequence of illustrations of diffusion (concentration and flux) and Fick's laws under steady-state conditions from a transdermal patch across a layer $x \in [0, L]$, where the following conditions hold: The concentration decreases linearly: $c(x,t) = C_0(1 - x/L)$, with boundary conditions $c(0,t) = C_0$ and $c(L,t) = 0$, where $L$ is the thickness of the dermis; The diffusive flux is constant: $\q(x,t) = \left(\frac{D C_0}{L}, 0\right)$. Since I do not know how to use Mathematica 14, I would like to create an animation or a sequence of panels in TikZ/PGFPlots. I am not sure if this code is a starting point and if it can also be easily adapted to model non-stationary (time-dependent) problems. I have done this: \documentclass{article} \usepackage{tikz, pgfplots} \pgfplotsset{compat=1.18} \begin{document} \begin{center} \begin{tikzpicture} \pgfplotsset{mappa/.style={view={0}{90}, width=1.8cm, height=4.5cm, scale only axis, xmin=0, xmax=1, ymin=0, ymax=1, y dir=reverse, xtick=\empty, ytick={0,0.5,1}, yticklabels={$0$,$L/2$,$L$}, ylabel={profondit\`a $x$}, colormap/jet, colorbar, domain=0:1, y domain=0:1, samples=2, samples y=40}} \begin{axis}[mappa, title={$c(x)/C_0$}, point meta min=0, point meta max=1, colorbar style={ytick={0,0.5,1}}, name=A] \addplot3[surf, shader=interp]{1-y}; \end{axis} \begin{axis}[mappa, at={(A.east)}, xshift=1.7cm, anchor=west, title={$q_x/(DC_0/L)$}, point meta min=0, point meta max=2, colorbar style={ytick={0,1,2}}, ylabel={}, yticklabel=\empty, name=B] \addplot3[surf, shader=interp]{1}; \end{axis} \begin{axis}[at={(B.east)}, xshift=1.9cm, anchor=west, width=3cm, height=4.5cm, scale only axis, xmin=0, xmax=1, ymin=0, ymax=1.1, xlabel={$x/L$}, legend style={font=\scriptsize, at={(0.5,1.04)}, anchor=south}] \addplot[blue, very thick, domain=0:1] {1-x}; \addlegendentry{$c/C_0$} \addplot[red, very thick, dashed, domain=0:1] {1}; \addlegendentry{$q_x/(DC_0/L)$} \end{axis} \end{tikzpicture} \end{center} \end{document} Is it possible to animate it?
- Coordinates in pgfplotsby Patricio on November 27, 2026 at 2:04 pm
I'm trying to label a figure created with pgfplots in a beamer document, but my labels do not appear where I expect them. I am guessing that the problem is that coordinates for labels are not the same than those of the figure itself, but I do not know how to address this. For example, in my MWE, I expected the label to be placed somewhere near the top right end of the ray, but I get this instead: My code is \documentclass{beamer} \usepackage{pgfplots} \begin{document} \begin{frame} \begin{figure} \begin{tikzpicture} \begin{axis}[ ticks=none, axis x line=bottom, axis y line=left, xmin=0,xmax=10, ymin=0,ymax=10] \addplot[samples=2,thin,black,dotted,domain=0:10] {x*(3/2)}; \path (7,9) node {$\mathit{MP}_x=\text{Undefined}$}; \end{axis} \end{tikzpicture} \end{figure} \end{frame} \end{document}
- How do I assign more memory to PGFPlots to graph a function with more points?by Rain on November 18, 2026 at 3:24 pm
I want to graph a (pretty ugly) function in 3D using PGFPlots: \documentclass{article} \usepackage{pgfplots} \pgfplotsset{compat=1.12} \begin{document} \begin{tikzpicture} \begin{axis}[ axis equal, zmin=0, zmax=1.2, axis lines=center, x={(-0.257*\textwidth,-0.257*\textwidth)}, y={(\textwidth,0)}, z={(0,\textwidth)} ] \addplot3[smooth,surf,opacity=0.2,domain=-5:5,y domain=-5:5,samples=81]{ 1.5*(exp(-(4*(x+4))^2-(4*(y+4))^2)+exp(-(4*(x+4))^2-(4*(y+2))^2)+exp(-(4*(x+4))^2-(4*y)^2)+exp(-(4*(x+4))^2-(4*(y-2))^2)+exp(-(4*(x+4))^2-(4*(y-4))^2)+exp(-(4*(x+2))^2-(4*(y+4))^2)+exp(-(4*(x+2))^2-(4*(y+2))^2)+exp(-(4*(x+2))^2-(4*y)^2)+exp(-(4*(x+2))^2-(4*(y-2))^2)+exp(-(4*(x+2))^2-(4*(y-4))^2)+exp(-(4*x)^2-(4*(y+4))^2)+exp(-(4*x)^2-(4*(y+2))^2)+exp(-(4*x)^2-(4*(y-2))^2)+exp(-(4*x)^2-(4*(y-4))^2)+exp(-(4*(x-2))^2-(4*(y+4))^2)+exp(-(4*(x-2))^2-(4*(y+2))^2)+exp(-(4*(x-2))^2-(4*y)^2)+exp(-(4*(x-2))^2-(4*(y-2))^2)+exp(-(4*(x-2))^2-(4*(y-4))^2)+exp(-(4*(x-4))^2-(4*(y+4))^2)+exp(-(4*(x-4))^2-(4*(y+2))^2)+exp(-(4*(x-4))^2-(4*y)^2)+exp(-(4*(x-4))^2-(4*(y-2))^2)+exp(-(4*(x-4))^2-(4*(y-4))^2)) }; \end{axis} \end{tikzpicture} \end{document} This takes maybe a minute or two to compile and results in something like this: However, these are supposed to be gaussian curves and therefore smoother, not spiky like in the picture. I'd therefore like to increase the number of points. However, going above 71 points causes my editor (TeXShop) to throw the following error: TeX capacity exceeded, sorry [main memory size=5000000]. How do I assign more memory to TeX? I've seen a few posts asking similar things, but the answers I've seen involve typing things into the command line (which I don't know how to do with TeXShop; I only know how to open Terminal on my Mac and type commands to control the computer's file system, such as cd and rm and ls and so on) or setting up LuaLaTeX (and I use XeLaTeX for font reasons). (Can an arbitrarily large amount of memory be assigned to LaTeX? My computer has 32 GB of RAM; I wouldn't mind giving it 5 GB to play with if it means it'll produce the graphs I want it to.) Thanks in advance.
- pgfplots rotation around axisby bbujeya on March 5, 2026 at 11:24 pm
I'm trying to plot a 2D graph and have the students find the volume of solid of revolution. I can use pgfplots to do the bulk of it, but my question is: how do I show the arrows on the axis to demonstrate that we need to rotate around the x or y axes? \documentclass[tikz]{standalone} \usepackage{tikz} \usepackage{tkz-euclide} \usepackage{pgfplots}\pgfplotsset{compat=1.18} \usepackage{siunitx}\usepgfplotslibrary{fillbetween,statistics} \usetikzlibrary{calc,positioning,arrows.meta,patterns,patterns.meta,intersections,shapes.geometric,decorations,shapes.callouts,decorations.pathmorphing,decorations.pathreplacing}\usepackage{amssymb} \begin{document} \begin{tikzpicture} \begin{axis} [width=0.8\linewidth, xmin=-0.5,xmax=16, ymin=-0.2,ymax=5, axis lines=center,axis on top, xlabel=$x$,ylabel=$y$, xticklabels=\empty,yticklabels=\empty] \addplot[name path=cubic,thick,black,samples=50,smooth,domain=-0.5:16]{sqrt(x+1)}; \addplot[name path=line,thick,black,samples=50,smooth,domain=-0.5:16]{4}; \addplot[darkgray,opacity=0.5] fill between[of=cubic and line,soft clip={domain=0:15}]; \end{axis} \end{tikzpicture} \end{document} Thanks for your help/suggestions.
- How can I improve the appearance of a 3D parametric curve with projections in PGFPlots?by Marco Moldenhauer on July 15, 2025 at 5:53 am
I'd like to recreate the appearance of a 3D parametric curve with vertical projection lines, as shown in the image below (you can ignore the text labels): Below is my code and the output it produces. Unfortunately, I'm not very happy with the result — it doesn't quite match the the target image. Any ideas for improving the visual appearance of this parametric plot would be greatly appreciated! my code \documentclass{article} \usepackage{geometry} \usepackage{tikz} \usetikzlibrary{arrows.meta} \usetikzlibrary{tikzmark} \usepackage{pgfplots} \pgfplotsset{compat=1.18} \pagenumbering{gobble} \usepackage{xcolor} \geometry{ %showframe, paperwidth=34.40cm, paperheight=14.40cm, margin=1cm } \begin{document} \begin{tikzpicture} [ % Define a paremtric curve declare function = { fx(\t) = 1+\t^3; fy(\t) = 1+1.6*\t; fz(\t) = 1.5-\t^2; SamplesLow = 5; SamplesHigh = 50; DomLowerBound = 0; DomUpperBound = 1; stepsize = (DomUpperBound-DomLowerBound)/SamplesLow; AxisXmin = -.5; AxisXmax = 4; AxisYmin = -.5; AxisYmax = 4; AxisZmin = 0; AxisZmax = 2; }, ] \begin{axis} [%% view={120}{45}, plot box ratio=1 1 1, width=22cm, height=12cm, xmin=AxisXmin, xmax=AxisXmax, ymin=AxisYmin, ymax=AxisYmax, zmin=AxisZmin, zmax=AxisZmax, grid, grid style={very thin, gray}, trig format plots=rad, enlargelimits=false, xlabel=$x$, ylabel=$y$, zlabel=$z$, xtick={0,1,...,AxisXmax-1}, ytick={0,1,...,AxisYmax-1}, ztick={0,1,...,AxisZmax} %axis x line=middle, %axis y line=middle, %axis z line=middle, ]%% % Plot the parametric surface using the defined functions \addplot3 [ variable=t, mesh, domain=DomLowerBound:DomUpperBound, samples=SamplesLow+1 ] ( { fx(t) }, { fy(t) }, { fz(t) } ); \addplot3 [ variable=t, mesh, domain=DomLowerBound:DomUpperBound, samples=SamplesLow+1, gray ] ( { fx(t) }, { fy(t) }, { AxisZmin } ); \pgfplotsinvokeforeach {0,stepsize,...,DomUpperBound} { \draw[gray, dotted] ({fx(#1)},{fy(#1)},AxisZmin) -- ({fx(#1)},{fy(#1)},{fz(#1)}); } \end{axis} \end{tikzpicture} \end{document} my output
- What is the most elegant way to shift the xy-plane to 𝑧=0 in a 3D TikZ diagram?by Marco Moldenhauer on June 28, 2025 at 3:42 am
I'm trying to draw a 3D diagram, and I want the xy-plane of the coordinate system to appear at z=0 — in effect, "lifting" the standard xy-plane to a higher position in the 3D coordinate space, as shown in the image below. What would be the most elegant and idiomatic way to achieve this? my code \documentclass{standalone} \usepackage{tikz} \usepackage{pgfplots} \pgfplotsset{compat=1.18} \begin{document} \begin{tikzpicture} [ % Define a paremtric curve declare function = { fx(\t) = 0.25+\t^3; fy(\t) = 0.5+1.6*\t; fz(\t) = 1-\t^2; Samples = 8; DomLowerBound = 0; DomUpperBound = 1.2; }, ] \begin{axis} [%% view={120}{45}, plot box ratio=1 1 1, width=22cm, height=12cm, xmin=-0.5, xmax=2.5, ymin=-0.5, ymax=2.5, zmin=-1, zmax=1.5, grid, grid style={very thin, gray}, trig format plots=rad, enlargelimits=false, xlabel=$x$, ylabel=$y$, zlabel=$z$, xtick={0.0,0.5,...,2}, ytick={0.0,0.5,...,2} ]%% % Plot the parametric surface using the defined functions \addplot3 [ variable=t, mesh, domain=DomLowerBound:DomUpperBound, samples=Samples ] ( { fx(t) }, { fy(t) }, { fz(t) } ); \addplot3 [ variable=t, mesh, domain=DomLowerBound:DomUpperBound, samples=Samples, gray ] ( { fx(t) }, { fy(t) }, { -1 } ); \pgfplotsinvokeforeach {0,DomUpperBound/(Samples-1),...,DomUpperBound} { \draw[gray, dotted] ({fx(#1)},{fy(#1)},-1) -- ({fx(#1)},{fy(#1)},{fz(#1)}); } \end{axis} \end{tikzpicture} \end{document} my output
- pgfmath function with loop not supported by addplotby Yijun Yuan on February 22, 2025 at 8:35 am
The following code \documentclass{article} \usepackage{pgfplots} \usetikzlibrary{math} \begin{document} \begin{tikzpicture} \tikzmath{ function test(\x,\y){ \res=0; for \z in {1,...,3} { \res=\res+\x+\y; }; return \res; }; } \begin{axis} \addplot3[surf,variable=\x,variable y=\y]({\x},{\y},{test(\x,\y)}); \end{axis} \end{tikzpicture} \end{document} will give error Illegal unit of measure (pt inserted). <to be read again> Y l.21 ...=\x,variable y=\y]({\x},{\y},{test(\x,\y)}); while the code \documentclass{article} \usepackage{pgfplots} \usetikzlibrary{math} \begin{document} \tikzmath{ function test(\x,\y){ \res=0; for \z in {1,...,3} { \res=\res+\x+\y; }; return \res; }; \res = test(1,2); print{\res}; } \end{document} works as expected (prints 9.0). Why is that? (This function is just a MWE, so rather than replacing test(\x,\y) with (\x+\y)*3, I'd like to know the reason behind this error)
- Why does \node work inside an axis environment while \pgfpointanchor fails?by cjorssen on November 14, 2024 at 10:47 pm
Consider the following code, where a node named A is defined inside an axis environment at the end of an \addplot macro. \documentclass{standalone} \usepackage{pgfplots} \begin{document} \begin{tikzpicture} \begin{axis} \addplot coordinates {(0, 0) (1, 1)} node[pos = .5, sloped, above] (A) {to}; % Does not work here % \pgfmathanglebetweenpoints{\pgfpointanchor{A}{south west}}{\pgfpointanchor{A}{south east}} \node[rotate = 0, red] at (A) {YO}; \end{axis} % Works here % \pgfmathanglebetweenpoints{\pgfpointanchor{A}{south west}}{\pgfpointanchor{A}{south east}} \end{tikzpicture} \end{document} The low-level pgf command \pgfpointanchor does work outside of axis environment but does not work inside (the error I get is No shape named A is known). If I understand correctly, this is not surprising since pgfplots needs to process all the plots to compute the dimensions of the axis and then determine the coordinates of the node. What I don't understand is why the \node at (A) command works inside the axis environment. Could someone explain this behavior?
- Quiver plot on surfaceby mixotrov on October 26, 2024 at 4:33 pm
Is it possible to combine a quiver plot and a surface plot, such that the z coordinate is respected? From what I can find, z buffer = sort only works within one \addplot3 command, or is there a way around this? The problem is that the arrows should sometimes be hidden behind the surface. As a MWE, I've written down the code for the normal vectors of a ball. The following old question is related: How can I draw a vector field (quiver plot) on a torus in 3D? Feel free to give feedback on the code as well! I'm currently getting about 80 000 Missing character: There is no <character> in font nullfont! warnings.. \documentclass{standalone} \usepackage{pgfplots} \pgfplotsset{ compat = newest, colormap name = viridis } \begin{document} \begin{tikzpicture}[ declare function = { xfun(\u,\v) = sin(deg(\u))*cos(deg(\v)); yfun(\u,\v) = sin(deg(\u)))*sin(deg(\v)); zfun(\u,\v) = cos(deg(\u)); usamples = 18; vsamples = 18; } ] \begin{axis}[ hide axis, axis equal image, z buffer = sort, ] \addplot3[ variable = \u, variable y = \v, samples = {usamples}, samples y = {vsamples}, domain = 0:2*pi, domain y = 0:pi, surf, shader = faceted, thin, ] ( {xfun(\u,\v)}, {yfun(\u,\v)}, {zfun(\u,\v)} ); \addplot3[ variable = \u, variable y = \v, samples = {usamples/2}, samples y = {vsamples}, domain = 0:2*pi, domain y = 0:pi, -stealth, quiver = { u = {xfun(\u,\v)}, v = {yfun(\u,\v)}, w = {zfun(\u,\v)}, scale arrows = 0.3, } ] ( {xfun(\u,\v)}, {yfun(\u,\v)}, {zfun(\u,\v)} ); \end{axis} \end{tikzpicture} \end{document}
- How derive the projection of the curve intersection with specific x-axis notation in pgfplots?by Explorer on June 11, 2024 at 4:42 pm
I encounter a confusing problem when deal with the projection of the function figure as below. I wonder how to modify my tex code's definition of \ShowIntersectionsAndVerticalLine with 4 arguments: name path global1 name path global2 the color of the intersection the notation of the x-axis (as in the figure x_1 x'_1 etc.). I found similar brilliant answer as below, but they didn't fit my case. Intersection -| of a named and a calculated coordinate TiKz-pgf: Project intersection points in the abscissa axis What I want to get is shown below: Here below is my MWE of the problem. \documentclass{standalone} \usepackage{tikz} \usepackage{pgfplots} \usetikzlibrary{intersections} \usetikzlibrary{math} \usepackage{amsmath} \tikzset{elegant/.style={smooth,thick,samples=50}} % Define this as a command to ensure that it is same in both cases \newcommand*{\ShowIntersection}[3]{ \fill [name intersections={of=#1 and #2, name=i, total=\t}] [#3, opacity=1, every node/.style={above left, black, opacity=1}] \foreach \s in {1,...,\t}{(i-\s) circle (1pt)}; } \begin{document} \begin{tikzpicture} \begin{axis}[ axis x line = middle, axis y line = middle, xmin = -1, xmax = 4, ymin = -1, ymax = 3 ] \addplot[elegant,domain=-4:4,name path global=A]{(x-1)*ln(x)-x+1} node [left=2pt] {$f(x)$}; \addplot[blue,mark=*,mark size=1pt] coordinates {(1,0)}; \addplot[blue,mark=*,mark size=1pt] coordinates {(e,0)}; \addplot[blue,thick,name path global=B]{-x+1}; \addplot[blue,thick,name path global=C]{(1-1/e)*x-e+1}; \addplot[orange,thick,name path global=D] coordinates {(-1,0.5) (4,0.5)}; \ShowIntersection{A}{D}{orange} \ShowIntersection{B}{D}{blue} \ShowIntersection{C}{D}{blue} %% TBC \end{axis} \end{tikzpicture} \end{document}
- Numerical precision issues within pgfplotstableby aeroben on March 23, 2023 at 12:41 pm
I am creating a table which automatically generates outputs of various quantities of interest for compressible flow. The first column contains Mach numbers, which are used to calculate the quantities in the other columns: \documentclass{book} \usepackage[utf8]{inputenc} \usepackage{amsmath} % Used by equations \usepackage{amssymb} \usepackage{pgfplotstable} \usepackage{pgfplots} \usepackage{longtable} \pgfplotsset{compat=1.17} \begin{document} \pgfkeys{/pgf/number format/.cd,fixed,precision=15} \def\gma{1.40} \pgfplotstableset{ create on use/Mach/.style={ create col/expr={0.01*\pgfplotstablerow+0.91} }, create on use/P_over_P0/.style={ create col/expr={((1+((\gma-1)/2)*\thisrow{Mach}^2)^(-\gma/(\gma-1)))} }, create on use/m_dot_sqrt_cpT0_over_Ap0/.style={ create col/expr={\gma/sqrt(\gma-1)*\thisrow{Mach}*(1 + (\gma-1)/2*\thisrow{Mach}^2)^(-(\gma+1)/(2*(\gma-1)))} }, create on use/m_dot_sqrt_cpT0_over_Ap/.style={ create col/expr={\thisrow{m_dot_sqrt_cpT0_over_Ap0}/\thisrow{P_over_P0}} }, create on use/impulse_function/.style={ create col/expr={1/\thisrow{m_dot_sqrt_cpT0_over_Ap}*(1+\gma*\thisrow{Mach}^2)} }, } \pgfplotstablenew[ columns={Mach, P_over_P0, m_dot_sqrt_cpT0_over_Ap0, m_dot_sqrt_cpT0_over_Ap, impulse_function}, ]{10}{\loadedtable} % Custom command for empty row \newcommand{\emptyrow}{& & & &\\} \pgfplotstabletypeset[ begin table=\begin{longtable}, end table=\end{longtable}, columns/Mach/.style={column name=M, column type={|l}, fixed zerofill, precision=3}, columns/P_over_P0/.style={column name=$\frac{P}{P_0}$, column type=l, fixed zerofill, precision=4}, columns/m_dot_sqrt_cpT0_over_Ap0/.style={column name=$\frac{\dot{m}\sqrt{c_p T_0}}{A p_0}$, column type=l, fixed zerofill, precision=4}, columns/m_dot_sqrt_cpT0_over_Ap/.style={column name=$\frac{\dot{m}\sqrt{c_p T_0}}{A p}$, column type=l, fixed zerofill, precision=4}, columns/impulse_function/.style={column name=$\frac{F}{\dot{m}\sqrt{c_p T_0}}$, column type=l|, fixed zerofill, precision=4}, every head row/.style={output empty row, before row={ \multicolumn{5}{c}{Gas Flow Tables ($\gamma=1.400$): Subsonic Flow} \\ \hline \fontsize{11}{13}\selectfont M & $\frac{P}{P_0}$ & $\frac{\dot{m}\sqrt{c_p T_0}}{A p_0}$ & $\frac{\dot{m}\sqrt{c_p T_0}}{A p}$ &$\frac{F}{\dot{m}\sqrt{c_p T_0}}$ \\[\arraystretch \tabcolsep] \hline\endhead\hline\endfoot\hline\endlastfoot}}, every nth row={5}{before row={\emptyrow}}, ]{\loadedtable} \end{document} This produces the following table: However, the numbers are very slightly incorrect. For example, the very bottom row, the middle value should return "1.2810", not 1.2813. I have tried increasing the precision of the top line above 4 but it does not make any difference. It is in longtable format because there will actually be 100 rows, not the 10 shown here.
- How do I add gray grid lines to a three-dimensional plot using pgfplots?by twoblackboxes on January 21, 2023 at 3:28 pm
I am trying to use pgfplots to draw a three-dimensional space. Using the following code I'm able to get the axes to show, and I know how to add coordinates and nodes and some other objects: \documentclass [border = .2cm] {standalone} \usepackage{pgfplots} \pgfplotsset{compat = newest} \begin{document} \begin{tikzpicture} \begin{axis} [ axis lines*=middle, grid style={line width=3pt},%, draw=gray!40}, major grid style={line width=4pt},%,draw=gray!90}, xmin=-10,xmax=10, ymin=-10,ymax=10, zmin=-10,zmax=10, xtick={0}, ytick={0}, ztick={0}, ] \end{axis} \end{tikzpicture} \end{document} That code produces this: What I would like, though, is to have the other grid lines off the major axes appear in gray. I don't know how to say this any more clearly--basically what I want is something like this: Ignore the tick marks and the labels for the axes--all I care about is reproducing the gray coordinate lines on the xy plane. I've been through the documentation for pgfplots and cannot figure out what I'm missing. If it's possible, I'd also be interested in drawing the grids for the z plane too.
- Shading Region of a Graphby James Evershed on April 7, 2020 at 10:02 am
Hope you're safe! I'm new to making graphs in pgfplots, and I've made the following graph (code below): What I'd like to do is to shade the area of the graph corresponding to all the points which lie above the black, red, and green lines, but within the co-ordinates' domain and range. Does anyone know how to do this? Thanks! \documentclass[12pt]{article} \usepackage{tikz-cd} %diagrams \usepackage{pgfplots} %graphs \begin{document} \begin{center} \begin{tikzpicture} \begin{axis}[ scaled ticks=false, width=14.9cm, xmin=0,xmax=0.1, ymin=0,ymax=0.1, axis x line=middle, axis y line=middle, axis line style= ->, xlabel={$\delta_{I}$}, ylabel={$\delta_{D}$}, y tick label style={ /pgf/number format/.cd, fixed, fixed zerofill, precision=2, /tikz/.cd }, x tick label style={ /pgf/number format/.cd, fixed, fixed zerofill, precision=2, /tikz/.cd }, x label style={at={(axis description cs:1,-0.1)},anchor=north}, y label style={at={(axis description cs:-0.1,1)},anchor=south}, ] \addplot expression[black,mark=none,domain=0:.1,very thick]{x} node[pos=0.95,anchor= south east]{$\delta_{D}=\delta_{I}$}; \addplot expression[red,mark=none,domain=0:.1,very thick]{9/13*x+4/195} node[pos=0.75,anchor= north west]{$\delta_{D}=\frac{9}{13}\delta_{I} + \frac{4}{195}$}; \addplot expression[blue,mark=none,domain=0:.1,very thick]{-1/2*x+1/30} node[pos=0.5,anchor= south west]{$\delta_{D}=-\frac{1}{2}\delta_{I}$ + $\frac{1}{30}$}; \addplot expression[green,mark=none,domain=0:.1,very thick]{-2*x+1/15} node[pos=0.05,anchor= south west]{$\delta_{D}=-2\delta_{I} + \frac{1}{15}$}; \end{axis} \end{tikzpicture} \end{center} \end{document}
- Jagged edge in pgfplotsby Rushi on January 28, 2020 at 12:28 am
I am trying to generate a surface plot using pgfplots: \documentclass[border=10pt]{standalone} \usepackage{pgfplots} \begin{document} \begin{tikzpicture}[declare function = {U(\x,\y)=(abs(\x)*(abs(\x)>abs(\y)) + abs(\y)*(abs(\x)<=abs(\y)) )*((abs(\x)<1) && (abs(\y)<1)) + 1*((abs(\x)>=1) || (abs(\y)>=1)) + max(2*\x-2,0) - min(2*\x+2,0) + max(2*\y-2,0) - min(2*\y+2,0);}] \begin{axis}[ colormap name = viridis, width = 8in, height = 6in, view = {25}{11}, grid = major, domain = -2:2, y domain = -2:2, ] \addplot3 [surf,samples at={-2,-1.95,...,2}] {U(\x,\y)}; \end{axis} \end{tikzpicture} \end{document} This results in a jagged bottom left edge. There are some line intersection issues as well. Any ideas on how to fix these?
- How to make the axes physically properly clip through a sphere?by Jack Tiger Lam on September 28, 2019 at 7:46 am
I have only seen questions where people wanted the axis to be overlaid on top of the plot. I want to do something more advanced. I want the axes to be rendered behind where you would expect them to be inside the surface, and rendered in front where you would expect them to be outside the surface. The first image is what I currently have. The second image is approximately what I would like to have. (apologies for the poor paint edit) \documentclass{article} \usepackage[a4paper,top=3cm,bottom=3cm,left=3cm,right=3cm,marginparwidth=1.75cm]{geometry} \usepackage{amsmath} \usepackage{amssymb} \usepackage{tikz} \usepackage{pgfplots} \usepgfplotslibrary{patchplots} \usepgfplotslibrary{colormaps} \pgfplotsset{compat=1.8} \usetikzlibrary{arrows.meta} \usetikzlibrary{shadings} \usepackage{xcolor} \begin{document} \begin{tikzpicture}[scale=2] \begin{axis}[axis equal image, axis lines=center, ticks=none, view/h=120, view/v=20, enlarge x limits={abs=27pt}, enlarge y limits={abs=13.3pt}, enlarge z limits={abs=10pt}] %sphere \addplot3[surf, shader=interp ,colormap={custom}{rgb255=(200,200,200)rgb255=(200,200,200)}, opacity=0.5, z buffer = sort, samples = 50, variable = \u, variable y = \v, domain = 0:180, y domain = 0:360] ({2*cos(u)*sin(v)}, {2*sin(u)*sin(v)}, {2*cos(v)}); %viviani cylinder parts \addplot3[surf, shader=interp, colormap={grayslate}{rgb255=(50,50,50)rgb255=(200,200,200)}, opacity=0.5, z buffer = sort, samples = 35, variable = \u, variable y = \v, domain = 0:360, y domain = -2:0, ] ({1+cos(u)}, {sin(u)}, {max(-sqrt(2*(2-x)),v)}); \addplot3[surf, shader=interp, colormap={grayslate}{rgb255=(50,50,50)rgb255=(200,200,200)}, opacity=0.5, z buffer = sort, samples = 35, variable = \u, variable y = \v, domain = 0:360, y domain = 0:2, ] ({1+cos(u)}, {sin(u)}, {min(sqrt(2*(2-x)),v)}); %viviani window edge \addplot3+[domain=0:4*pi, samples=50, samples y=0, no marks, smooth, thick, black]({1+cos(deg(x))},{sin(deg(x))},{2*sin(deg(x)/2)}); %equator \addplot3+[domain=0:2*pi, samples=50, samples y=0, no marks, smooth, dotted, black, line width=0.5pt]({2*cos(deg(x))},{2*sin(deg(x))},{0}); %equator cylinder \addplot3+[domain=0:2*pi, samples=30, samples y=0, no marks, smooth, solid, thick, black]({1-cos(deg(x))},{sin(deg(x))},{0}); \end{axis} \end{tikzpicture} \end{document}
- `imshow` like diplay of image with colormapby Manuel Schmidt on January 15, 2019 at 5:39 pm
I would like to show an originally gray scale image with a specific colormap like matlabs/matplotlibs imshow. Original: % python import matplotlib.pyplot as plt import matplotlib.image as mpimg import numpy as np img=mpimg.imread('stinkbug.png') imgplot = plt.imshow(img) plt.imshow(img, cmap="hot") Is this possible with tikz or pgfplots? I can export the image to csv or any other format if necessary. However I would like to specify the colormap in latex/tikz/pgfplots and not export an already colored image from python.
- Is there a bug with the `ybar interval stacked` key of pgfplots?by AndréC on October 31, 2018 at 5:43 pm
The ybar interval key is used to place the graph bars between the given coordinates. The next 5 coordinates define 4 intervals. This key works as expected and produces the following result. \documentclass{article} \usepackage{tikz} \usetikzlibrary{patterns} \usepackage{pgfplots} \pgfplotsset{compat=1.16} \begin{document} \begin{tikzpicture} \begin{axis}[axis x line=bottom,axis y line=left, ymin=0,ymax=8, xmin=20,xmax=50, minor y tick num=1] \addplot+ [ybar interval,fill=green] coordinates{(25,1) (30,4) (35,5) (40,2) (45,0)}; \end{axis} \end{tikzpicture} \end{document} When the key is ybar stacked the code produces 5 bars as expected. \documentclass{article} \usepackage{tikz} \usetikzlibrary{patterns} \usepackage{pgfplots} \pgfplotsset{compat=1.16} \begin{document} \begin{tikzpicture} \begin{axis}[axis x line=bottom,axis y line=left, ymin=0,ymax=8, xmin=20,xmax=50, minor y tick num=1] \addplot+ [ybar stacked,fill=green] coordinates{(25,1) (30,4) (35,5) (40,2) (45,0)}; \addplot+ [ybar stacked,pattern color=blue,draw=black,pattern=north east lines]coordinates{(25,1) (30,1) (35,2) (40,1) (45,2)}; \end{axis} \end{tikzpicture} \end{document} When the key is ybar interval stacked the code produces the following result which is not a stacking in the intervals. \documentclass{article} \usepackage{tikz} \usetikzlibrary{patterns} \usepackage{pgfplots} \pgfplotsset{compat=1.16} \begin{document} \begin{tikzpicture} \begin{axis}[axis x line=bottom,axis y line=left, ymin=0,ymax=8, xmin=20,xmax=50, minor y tick num=1] \addplot+ [ybar interval stacked,fill=green] coordinates{(25,1) (30,4) (35,5) (40,2) (45,0)}; \addplot+ [ybar interval stacked,pattern color=blue,draw=black,pattern=north east lines]coordinates{(25,1) (30,1) (35,2) (40,1) (45,2)}; \end{axis} \end{tikzpicture} \end{document} Is there a bug in pgfplots? Otherwise how does the ybar interval stacked key work? I opened a ticket about it and Stefan Pinnow confirmed that it is a bug. Translated with www.DeepL.com/Translator
- Shading the edge of inequalities in pgfplotsby Christopher Donham on November 29, 2017 at 9:19 pm
I teach College Algebra and need to create black and white handouts that show the solution to a system of inequalities. Normally, I would color the solution side of each inequality and note that the answer to all of the inequalities is the area where all of the colors overlap. In black and white, this is often done by hashing the solution side. As you add inequalities to the system, though, this can become a mess and hard to read (see for example this link , or this link). What I want to do is hashing as shown in the example image below (not from my MWE) where the hash just follows the edge of the inequality rather than filling the entire region. Here is an MWE that graphs two inequalities with pgfplots. How do I do the hashing along the edge? Note that I need the solution to work not just with linear inequalities, but graphs of parabolas and circles as well. \documentclass[10pt]{article} \usepackage{pgfplots} \begin{document} $\left\{ \begin{array}{l} y \le -3x + 6 \\ x > -2 \\ y \le 4 \\ \end{array} \right.$ \begin{tikzpicture} \begin{axis}[ axis x line=center, axis y line=center, width=4in, height=4in, x axis line style={<->}, y axis line style={<->}, xmin=-10, xmax=10, ymin=-10, ymax=10 ] \addplot [very thick,cyan!50!black,domain=-10:10] {6 - 3*x}; \addplot [very thick,cyan!50!black,domain=-10:10] {4}; \addplot [very thick,cyan!50!black,dashed] coordinates {(-2,-10) (-2,10)}; \end{axis} \end{tikzpicture} \end{document} UPDATE A good answer to this question was provided below. I also stumbled upon Decorating a path's edge with a hatch pattern in TikZ from which I was able to also develop the code below. I am posting it here for completeness with an example output image. \documentclass[10pt]{article} \usepackage{pgfplots} \begin{document} $\left\{ \begin{array}{l} y \le -3x + 6 \\ x > -2 \\ y \le 4 \\ y \ge x^2-4 \\ \end{array} \right.$ % Amplitude is the length of the hatching. % Segment Length is the distance between the marks. % Angle is the orientation of the hatches with +-90 being perpendicular. \pgfplotsset{IneqStyleLess/.style = {% decoration={border,segment length=1mm, amplitude=3mm,angle=-90}, postaction={decorate,draw}, thick,blue!50!white, draw opacity=0.5 } } \pgfplotsset{IneqStyleGtr/.style = {% decoration={border,segment length=1mm, amplitude=3mm,angle=90}, postaction={decorate,draw}, thick,blue!50!white, draw opacity=0.5 } } \begin{tikzpicture} \begin{axis}[ axis x line=center, axis y line=center, axis on top, width=4in, height=4in, x axis line style={<->}, y axis line style={<->}, xmin=-10, xmax=10, ymin=-10, ymax=10 ] \addplot [IneqStyleLess, domain=-10:10, ] {6 - 3*x}; \addplot [IneqStyleLess, thick,red!50!white, domain=-10:10, ] {4}; \addplot [IneqStyleLess, thick,green!50!white] coordinates {(-2,-10) (-2,10)}; \addplot [IneqStyleGtr, thick,purple!50!white, domain=-10:10, samples=100, ] {x*x-4}; \addplot [black,domain=-10:10] {6 - 3*x}; \addplot [black,domain=-10:10] {4}; \addplot [black,dashed] coordinates {(-2,-10) (-2,10)}; \addplot [black,domain=-10:10, samples=100] {x*x-4}; \end{axis} \end{tikzpicture} \end{document}
- PGFPlots fillbetween layer issue with main layerby bz-mof on April 6, 2017 at 9:02 pm
I am using fill between to create some diagrams where I need semi-transparent fillings above ploted lines. As normally fillings are put into a separate layer pre main below plot lines, I tried to move the fillings via on layer=main. They disappear? I don't know if this behaviour is intended. I can draw them on any other layer but this one. See the minimal example below: \documentclass[]{article} \usepackage{pgfplots} \pgfplotsset{compat=1.14} \usepgfplotslibrary{fillbetween} \begin{document} \begin{tikzpicture} \begin{axis} \addplot+[name path=A,domain=0:1,samples=10] {x}; \addplot+[name path=B,domain=0:1, samples=10] {x^2}; \addplot fill between[of=A and B, on layer=main]; \end{axis} \end{tikzpicture} \end{document} Result My workaround at the moment is to draw everything onto the pre main layer. Edit: Giving the option in the \addplot part of the statement has absolutely no effect: \documentclass[]{article} \usepackage{pgfplots} \pgfplotsset{compat=1.14} \usepgfplotslibrary{fillbetween} \begin{document} \begin{tikzpicture} \begin{axis}[domain=0:1] \addplot+[name path=A, draw=none, mark=none] {x}; \addplot+[name path=B, draw=none, mark=none] {x^2}; \addplot+[draw=black, mark=none] {x-0.1}; \addplot [on layer=axis foreground, fill=green] fill between[of=A and B]; \addplot+[draw=blue, mark=none] {x-0.15}; \end{axis} \end{tikzpicture} \end{document} Result
- How to draw a 2D vector field in pgfplots?by ILoveJesus on January 26, 2017 at 8:43 pm
I'm pretty new to LaTeX and I need to draw a 2D vector field (Like a graph with vector arrows all over it. I can't find it anywhere online. I'd like to use pgfplots if that's possible.
- Compute convolution of discrete signals in Tikzby gbernardi on September 8, 2016 at 3:57 pm
I want to draw a simple graphical example of a convolution of two discrete sequences in TikZ. I used the code from this link to get my result, which is already what I want but, so far, the values of the different signals at the different time instants are just numbers computed somewhere else. I'm curious to know if there is an easy way to define two variables for the two sequences and compute the convolution signal, i.e. directly in TikZ. \documentclass{standalone} \usepackage{tikz,pgfplots} \usetikzlibrary{pgfplots.groupplots} \begin{document} \begin{tikzpicture} %/* \pgfplotsset{ standard/.style={%Axis format configuration width=5cm, axis x line=middle, axis y line=middle, enlarge x limits=0.15, enlarge y limits=0.15, every axis x label/.style={at={(current axis.right of origin)},anchor=north west}, every axis plot post/.style={mark options={fill=white}}, xticklabel style={align=center}, xmin=-0.5, xmax=6, ymin=0, ymax=2, xlabel={$n$}, } } % \begin{groupplot}[ group style={ group size=3 by 1, vertical sep=25pt, horizontal sep=35pt }, ] % Plot [1, 1] \nextgroupplot[ standard, title={$a[n]$}, ] \addplot+[ycomb,black,thick] plot coordinates {(0,1) (1,0.5) (2,1) (3,1)}; % Plot [1, 2] \nextgroupplot[ standard, title={$b[n]$}, ] \addplot+[ycomb,black,thick] plot coordinates {(0,1) (1,0.5) (2,0.25)}; % Plot [1, 3] \nextgroupplot[ standard, title={$c[n] = a[n]\ast b[n]$}, ] \addplot+[ycomb,black,thick] plot coordinates {(0,1) (1,1) (2,1.5) (3,1.625) (4,0.75) (5,0.25)}; \end{groupplot} %*/ \end{tikzpicture}% \end{document}
- 3D spherical coordinate system in pgf plotsby Tony Mau on June 27, 2016 at 5:01 am
I just learned 3d rectangular graphs in pgf-plots, and now I need to graph in spherical coordinates. Ive been reading the manual and cant find an answer. Is there a simple way to plot something such as rho=1+sin(theta)*sin(phi). Thank you for the help. \begin{tikzpicture} \begin{axis}[ xmin=-5,xmax=5, ymin=-5,ymax=5, zmin=-5,zmax=5, xlabel={$x$}, ylabel={$y$}, zlabel={$z$}, zlabel style={rotate=90}, view={75}{20}] addplot3[surf,opacity=0.75,domain=-360:360,y domain=-360:360,samples=20]({x},{y},{1+0.2*sin(x)*sin(y)}); \end{axis} \end{tikzpicture}
- How to draw a region of inequalityby U.M. on December 16, 2015 at 8:15 pm
Could you help me draw a region of this inequality: \begin{eqnarray} 3x_1 + 3x_2 \geq 27 x_1 + 2x_2 \geq 18 4x_1 + 2x_2 \geq 28 \end{eqnarray} I need two graphs: one with three inequalities marked with different patters and one with the set of the solution. Inspired by an answer to a different question I made somethinhg like that: it works, but I can't write anything below and when I tried to change it so it'd show only the solution but that didn't work. \documentclass{article} \usepackage{pgfplots} \usetikzlibrary{patterns} \makeatletter \newcommand{\pgfplotsdrawaxis}{\pgfplots@draw@axis} \makeatother \pgfplotsset{only axis on top/.style={axis on top=false, after end axis/.code={ \pgfplotsset{axis line style=opaque, ticklabel style=opaque, tick style=opaque, grid=none}\pgfplotsdrawaxis}}} \newcommand{\drawge}{-- (rel axis cs:1,0) -- (rel axis cs:1,1) -- (rel axis cs:0,1) \closedcycle} \begin{document} \begin{figure}[htpb] \centering \label{fig:p3:c1} \begin{tikzpicture} \begin{axis}[only axis on top, axis line style=very thick, axis x line=bottom, axis y line=left, ymin=0,ymax=14,xmin=0,xmax=14, xlabel=$x_1$, ylabel=$x_2$,grid=major ] \addplot [draw=none, pattern=vertical lines, domain=0:14] {9-x} \drawge; \addplot [draw=none, pattern=north west lines, domain=0:14] {9-0.5*x} \drawge; \addplot [draw=none, pattern=horizontal lines, domain=0:14] {14-2*x} \drawge; \addplot[very thick, domain=0:14] {9-x}; \addplot[very thick, domain=0:14] {9-0.5*x}; \addplot[very thick, domain=0:14] {14-2*x}; \end{axis} \end{tikzpicture} \end{figure} \end{document}
- Is there a way to automatically force xticks on first and last data point in pgfplots?by user85845 on September 1, 2015 at 4:41 pm
I have created a custom pgfplot style which allows me to create line plots with the x axis being a time in hh:mm format. The data is loaded from .csv files. The automatic tick placement on the x axis is fine, but I would like to make sure there is a tick for the first and last data point. The x axis range and the number of data points varies, so I'd rather avoid manual tick placement. Is there a way to force ticks on the first and last data point while in between all ticks are automatically placed? Code \documentclass{article} \usepackage{pgfplots} \def\removeleadingzeros#1{\if0#1 \expandafter\else#1\fi} \def\transformtime#1:#2:#3!{ \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed} \pgfmathparse{\removeleadingzeros#1*3600+\removeleadingzeros#2*60+\removeleadingzeros#3} \pgfkeys{/pgf/fpu=false} } \pgfplotsset{ timeplot/.style={ x coord trafo/.code={\expandafter\transformtime##1!}, x coord inv trafo/.code={% \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed} \pgfmathsetmacro\hours{floor(##1/3600)} \pgfmathsetmacro\minutes{floor((##1-\hours*3600)/60)} \def\pgfmathresult{\pgfmathparse{mod(\hours,60)<10?"0":{},int(mod(\hours,60))}\pgfmathresult:\pgfmathparse{mod(\minutes,60)<10?"0":{},int(mod(\minutes,60))}\pgfmathresult} \pgfkeys{/pgf/fpu=false} }, scaled x ticks=false, xticklabel=\tick, ymin=0, grid=major, width=\linewidth } } \begin{filecontents*}{juli.csv} 12:22:00,181 12:37:00,180 12:52:00,176 13:07:00,171 13:22:00,164 13:37:00,156 13:52:00,146 14:07:00,134 14:22:00,121 \end{filecontents*} \begin{document} \begin{tikzpicture} \begin{axis}[timeplot] \addplot [mark=none] table [col sep=comma] {juli.csv}; \end{axis} \end{tikzpicture} \end{document}
- Shaded 3D ellipsoid with pgfplotby Tyrion on August 13, 2015 at 12:01 pm
I'm trying to display a shaded 3D ellipsoid with axis labels with PGFplots. So far I achieved this. \documentclass{standalone} \usepackage{tikz} \usepackage{pgfplots,tikz-3dplot} \begin{document} \begin{tikzpicture} \begin{axis}[% width=0.8\textwidth, axis equal, axis lines = center, y label style={at={(axis cs:0,2,0)},anchor=west}, xlabel = {$\lambda_1,\ \vec{e}_1$}, ylabel = {$\lambda_2,\ \vec{e}_2$}, zlabel = {$\lambda_3,\ \vec{e}_3$}, ticks=none, ] \addplot3[% shader=interp, opacity = 0.3, fill opacity=0.3, surf, colormap/blackwhite, variable = \u, variable y = \v, domain = 0:180, y domain = 0:360, ] ({2*cos(u)*sin(v)}, {2*sin(u)*sin(v)}, {0.5*cos(v)}); \end{axis} \end{tikzpicture} \end{document} The result looks like this: This shading does not give a 3D impression, I would like to have somethin like this: Is it possible to define a new 3d shading in this coordinate system? I could not find such a command in the pgfplot documentation. All predefined shadings are not suitable. Glad for any help!
- Drawing a Cube with Tikz/PgfPlots Looks Stretched along X-Axisby Rüdiger on October 10, 2014 at 10:38 am
I'm trying to draw a cubic structure (fcc crystal structure) with Tikz. I'm using pgfplots's axis cs function for the 3D projection. But the result looks like a rectangle stretched along the x axis. Here is the result: Here is the code: \documentclass{article} \usepackage{tikz} \usetikzlibrary{calc,arrows,positioning} \usepackage{pgfplots} \pgfplotsset{compat=1.9} \begin{document} \begin{tikzpicture}[ vector/.style={thick,black,>=stealth,->}, atom/.style={blue} ] \begin{axis}[ axis lines=none, xmin=-0.1, xmax=1.1, ymin=-0.1, ymax=1.1, zmin=-0.1, zmax=1.1, xtick=\empty, ytick=\empty, ztick=\empty ] \coordinate (A0) at (axis cs:0,0,0); \coordinate (A1) at (axis cs:1,0,0); \coordinate (A2) at (axis cs:0,1,0); \coordinate (A3) at (axis cs:0,0,1); \coordinate (A4) at (axis cs:1,1,0); \coordinate (A5) at (axis cs:1,0,1); \coordinate (A6) at (axis cs:0,1,1); \coordinate (A7) at (axis cs:1,1,1); \coordinate (A8) at (axis cs:0.5,0.5,0); \coordinate (A9) at (axis cs:0.5,0,0.5); \coordinate (A10) at (axis cs:0,0.5,0.5); \coordinate (A11) at (axis cs:0.5,0.5,1); \coordinate (A12) at (axis cs:0.5,1,0.5); \coordinate (A13) at (axis cs:1,0.5,0.5); \draw[black!25] (A0) -- (A8) -- (A4); \draw[black!25] (A4) -- (A12) -- (A6); \draw[black!25] (A0) -- (A10) -- (A6); \draw[black!25] (A1) -- (A8) -- (A2); \draw[black!25] (A2) -- (A10) -- (A3); \draw[black!25] (A7) -- (A12) -- (A2); \draw[black] (A1) -- (A13) -- (A7); \draw[black] (A3) -- (A11) -- (A7); \draw[black] (A5) -- (A13) -- (A4); \draw[black] (A6) -- (A11) -- (A5); \draw[black] (A5) -- (A9) -- (A0); \draw[black] (A0) -- (A1); \draw[black] (A0) -- (A2); \draw[black] (A0) -- (A3); \draw[black] (A0) -- (A3); \draw[black] (A3) -- (A5); \draw[black] (A3) -- (A6); \draw[black] (A6) -- (A2); \draw[black] (A5) -- (A1); \draw[black] (A7) -- (A4); \draw[black] (A5) -- (A7); \draw[black] (A1) -- (A4); \draw[black] (A6) -- (A7); \draw[black] (A2) -- (A4); \fill[blue] (A8) circle (5pt); \draw[vector] (A0) -- (A8); \fill[blue] (A10) circle (5pt); \draw[vector] (A0) -- (A10); \draw[black] (A3) -- (A9) -- (A1); \fill[atom] (A1) circle (5pt); \fill[atom] (A2) circle (5pt); \fill[atom] (A3) circle (5pt); \fill[atom] (A4) circle (5pt); \fill[atom] (A5) circle (5pt); \fill[atom] (A6) circle (5pt); \fill[atom] (A7) circle (5pt); \fill[atom] (A9) circle (5pt); \fill[atom] (A11) circle (5pt); \fill[atom] (A12) circle (5pt); \fill[atom] (A13) circle (5pt); \draw[vector] (A0) -- (A9); \fill[blue] (A0) circle (5pt); \end{axis} \end{tikzpicture} \end{document} What causes the stretching? How can I change it into a perfect cube?
- pgfplots 3d cylindrical coordinate systemby Markus on March 31, 2014 at 3:16 am
I am trying to plot a cylindrical coordinate system using pgfplots. The idea would be to plot the r,theta plane similar to what polaraxis provides, see here. I don't know how to translate something like this into a 3D environment. Is there an easy way to plot a custom coordinate system within the current 3D axis? UPDATE: I may not have been precise enough. I am specifically interested in plotting the coordinate/axis system, i.e. the r,theta grid in a 3D plot.
- How to draw the region of inequality?by Yichao Zhou on October 9, 2012 at 2:29 am
Everyone, I would like to draw the region \begin{align*} x_1 + x_2 &\ge 3 \\ 2x_1 - x_2 &\le 5 \\ -x_1 + 2x_2 &\le 3 \end{align*} Here is my code to draw the region of inequality: \documentclass{article} \usepackage{pgfplots} \usetikzlibrary{patterns} \begin{document} \begin{figure}[htpb] \centering \label{fig:p3:c1} \begin{tikzpicture} \begin{axis}[axis on top,smooth, axis line style=very thick, axis x line=bottom, axis y line=left, ymin=-1,ymax=6,xmin=-1,xmax=6, xlabel=$x_1$, ylabel=$x_2$,grid=major ] \addplot[thick,pattern=crosshatch,pattern color=blue!30!white, domain=-10:4]{3-x} -| (axis cs:50, 50); \addplot[thick,pattern=crosshatch dots,pattern color=green!60!white, domain=-10:12]{-5+2*x} -| (axis cs: -50,50); \addplot[thick,pattern=grid,pattern color=yellow, domain=-10:10]{3/2+x/2} -| (axis cs: 20,0); \addplot[very thick, domain=-10:10]{3-x}; \addplot[very thick, domain=-10:10]{-5+2*x}; \addplot[very thick, domain=-10:10]{3/2+x/2}; \end{axis} \end{tikzpicture} \caption{Problem 4, Part A} \end{figure} \end{document} This code have several problems. In order not let pattern to cover the line, I draw every function twice. Can we draw the pattern just before draw all the line? The grid overlap show before the line. (I also want to let axis on the top) How to fill the region above/under a curve/line? As you can see, I just use a very ugly way. It will draw unnecessary line (now it is overlapped with axis) and it is not beautiful. I need to modify the coordinate every time I change other things. Any better method?
- pgfplots quadricsby Brownian on August 9, 2012 at 10:05 pm
I wonder whether it is possible to draw the three following quadric surfaces (cone, hyperboloids) x^2+y^2-z^2=0 x^2+y^2-z^2=1 x^2+y^2-z^2=-1 (and the axis) using pgfplots 3d or TikZ as it is done in the following image
- How can I draw a vector field (quiver plot) on a torus in 3D?by Håkon Marthinsen on June 21, 2012 at 2:34 pm
I'd like to draw a vector field on a torus using PGFPlots and/or TikZ. I managed to do it using ePiX, based on the file decorate.xp from the example gallery, but then I have to do z-sorting by hand, which is very ugly. How can I get PGFPlots and/or TikZ to give the same result? It seems to me that PGFPlots can plot the vector field using quiver, and plot the torus as a surf, but not both at once. Is it possible to get around this by chopping up the drawing into smaller pieces? How should I do that? NB: The code is a hack, and is probably badly written and formatted. Any tips for improving the figure and/or code are appreciated! /* -*-ePiX code based on on the example file decorate.xp-*- */ #include <algorithm> #include "epix.h" using namespace ePiX; const int N1(60); // latitudes const int N2(60); // longitudes const double du(2*M_PI/N1); const double dv(2*M_PI/N2); const double r_0(.999); // minor radius const double R_0(1); // major radius P F(double u, double v) { return polar(R_0 + r_0*Cos(u), v) + P(0,0,r_0*Sin(u)); } namespace ePiX { class mesh_quad { private: P pt1, pt2, pt3, pt4, center; double distance; public: mesh_quad(P f(double u, double v), double u0, double v0) : pt1(f(u0,v0)), pt2(f(u0+du,v0)), pt3(f(u0+du,v0+dv)), pt4(f(u0,v0+dv)), center(0.25*(pt1 + pt2 + pt3 + pt4)), distance(max(max(max(norm(pt1-camera.viewpt()), norm(pt2-camera.viewpt())), norm(pt3-camera.viewpt())), norm(pt4-camera.viewpt()))) { } double how_far() const { return distance; } void draw() const { P direction(center-camera.viewpt()); P normal((pt2 - pt1)*(pt4 - pt1)); normal *= 1/norm(normal); fill(Gray(normal|(recip(distance)*direction))); pen(0); quad(pt1, pt2, pt3, pt4); fill(false); } }; class mesh_vf { private: P pt1, pt2, center; double distance; public: mesh_vf(P f(double u, double v), double u0, double v0) { pt1 = f(u0,v0); double C = 2 * (pt1.x2() - 8); double Gamma = -C * Sin(u0) * Sin(v0); double Delta = C * (1 + Cos(u0)) * Cos(v0); pt2 = f(u0,v0) + .005 * P(Gamma * Sin(u0) * Cos(v0) + Delta * (1 + Cos(u0)) * Sin(v0), Gamma * Sin(u0) * Sin(v0) - Delta * (1 + Cos(u0)) * Cos(v0), -Gamma * Cos(u0)); center = .5*(pt1 + pt2); distance = min(norm(pt1-camera.viewpt()),norm(pt2-camera.viewpt())); //distance = norm(center-camera.viewpt()); } double how_far() const { return distance; } void draw() const { plain(Black()); arrow(pt1, pt2, .3); } }; class by_distance { public: bool operator() (const mesh_quad& arg1, const mesh_quad& arg2) { return arg1.how_far() > arg2.how_far(); } bool operator() (const mesh_vf& arg1, const mesh_vf& arg2) { return arg1.how_far() > arg2.how_far(); } bool operator() (const mesh_quad& arg1, const mesh_vf& arg2) { return arg1.how_far() > arg2.how_far(); } bool operator() (const mesh_vf& arg1, const mesh_quad& arg2) { return arg1.how_far() > arg2.how_far(); } }; } // end of namespace int main() { picture(P(-2.5,-3.5),P(3.5,3), "12x10cm"); begin(); set_crop(); viewpoint(sph(10, 80 * M_PI / 180, 30 * M_PI / 180)); // background grids pen(Gray(.5)); grid(P(-2, -2, -2), P(2, 2, -2), 4,4); grid(P(-2, -2, -2), P(2, -2, 2), 4,4); // n.b. (z,x) divisions grid(P(-2, -2, -2), P(-2, 2, 2), 4,4); label(P(2, 0, -2), P(-15, -15), "$y$", bl); label(P(2, -2, -2), P(-2, -2), "$-2$", bl); label(P(2, -1, -2), P(-2, -2), "$-1$", bl); label(P(2, 0, -2), P(-2, -2), "$0$", bl); label(P(2, 1, -2), P(-2, -2), "$1$", bl); label(P(-2, 2, 0), P(15, -15), "$z$", br); label(P(-2, 2, -1), P(2, 0), "$-1$", br); label(P(-2, 2, 0), P(2, 0), "$0$", br); label(P(-2, 2, 1), P(2, 0), "$1$", br); label(P(-2, 2, 2), P(2, 0), "$2$", br); label(P(0, 2, -2), P(0, -20), "$x$", b); label(P(-1, 2, -2), P(0, -3), "$-1$", b); label(P(-2, 2, -2), P(0, -3), "$-2$", b); label(P(0, 2, -2), P(0, -3), "$0$", b); label(P(1, 2, -2), P(0, -3), "$1$", b); label(P(2, 2, -2), P(0, -3), "$2$", b); // build and draw a torus with vector field std::vector<mesh_quad> mesh; for (int i=0; i<N1; ++i) for (int j=0; j<N2; ++j) mesh.push_back(mesh_quad(F, i*du, j*dv)); sort(mesh.begin(), mesh.end(), by_distance()); std::vector<mesh_vf> vf; for (int i=0; i<N1; ++i) for (int j=0; j<N2; ++j) vf.push_back(mesh_vf(F, i*du, j*dv)); // vf.push_back(mesh_vf(F, (i+.5)*du, (j+.5)*dv)); sort(vf.begin(), vf.end(), by_distance()); int j = 0; for (unsigned int i=0; i<mesh.size(); ++i) { mesh.at(i).draw(); if (j < vf.size()) { for ( ; (vf.at(j).how_far() > mesh.at(i).how_far()) && (j < vf.size()); j++) vf.at(j).draw(); } } for ( ; j < vf.size(); j++) vf.at(j).draw(); //label_color(Red()); //spot(P(0,2,0)); tikz_format(); end(); } Here is a screenshot of the result: