3

Is there a better method to have the x=0 and y=0 axes thicker? I made a basic approach by adding lines, but is there a better method?

\documentclass{book} \usepackage{pgfplots} \pgfplotsset{compat=1.15} \usepackage{tikz} \begin{document} \begin{tikzpicture}[scale=0.8] \begin{axis}[ xmin=-2, xmax=6, ymin=-1.5, ymax=10, xtick={0,2,4}, ytick={0,2,4,6,8}, xmajorgrids=true, ymajorgrids=true, ytick style={draw=none}, xtick style={draw=none}, xlabel=$x$, ylabel=$y$, height=5cm, axis equal image ] \addplot [domain=-2:6, samples=100, color=blue!50, line width=1pt]{pow(x-2,2)}; \addplot [domain=-2:6, color=red!50, line width=1pt]{4}; \draw[line width=.5pt] ({axis cs:0,-2}) -- ({axis cs:0,10}); \draw[line width=.5pt] ({axis cs:-10,0}) -- ({axis cs:10,0}); \draw[line width=.5pt, green] (-2,0)--(0,0); \draw[green,fill=white] (0,0) circle(2pt); \draw[line width=.5pt, green] (4,0)--(6,0); \draw[green,fill=white] (4,0) circle(2pt); \end{axis} \end{tikzpicture} \end{document} 

This is not the same as pgfplots: how to make the axis thick? as that 'thicker' command results in thicker frame, not axes.

N.B. I don't want thicker arrow axes, I want thicker 'axes lines' in a framed plot. They do not seem to want to 'co-exist'.

10
  • 2
    This question is similar to: pgfplots: how to make the axis thick?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented 23 hours ago
  • @samcarter_is_at_topanswers.xyz OK. I improved it now. Commented 23 hours ago
  • @Marijn Not the same. Add the command ...thicker... and you will get a thicker FRAME, not axes. Commented 23 hours ago
  • @mf67 I didn't realize that you draw the axes manually, if you add axis lines=middle (like in the answer below) then axis line style=thick as in the duplicate would work. If you draw them manually then also \draw[thick] ({axis cs:0,-2}) -- ({axis cs:0,10}); (or \draw[ultra thick]) works, which would keep the frame. Commented 22 hours ago
  • @Marijn Adding those 2 commands does make the axes thick automatically, but it will place arrow axis and not framed plot. I want to avoid manual lines as I need to adjust them according to the x and y range. I want it to be by default without manual adjustments, if possible. Commented 22 hours ago

3 Answers 3

5

The question basically asks how to combine axis lines=middle with axis lines=box, which is already answered in combine `axis lines = middle` and box.

Unfortunately there is no built-in way to combine the two styles. Instead you can draw the frame axes separately in a new empty plot.

You need to select which parts of the settings for the size, ticks, grid etc. go in the first, empty plot and which go in the second. To switch off the arrow at the end of the axes you can use axis lines*=middle with a *, instead of the default axis lines=middle that produces an arrow.

MWE:

\documentclass{book} \usepackage{pgfplots} \pgfplotsset{compat=1.15} \usepackage{tikz} \begin{document} \begin{tikzpicture}[scale=0.8] % empty plot with box axes \begin{axis}[ xmin=-2, xmax=6, ymin=-1.5, ymax=10, xtick={0,2,4}, ytick={0,2,4,6,8}, xlabel=$x$, ylabel=$y$, xmajorgrids=true, ymajorgrids=true, ytick style={draw=none}, xtick style={draw=none}, height=5cm, axis equal image] \end{axis} % actual function plots with middle axes \begin{axis}[ axis lines*=middle, xmin=-2, xmax=6, ymin=-1.5, ymax=10, ticks=none, height=5cm, axis equal image ] \addplot [domain=-2:6, samples=100, color=blue!50, line width=1pt]{pow(x-2,2)}; \addplot [domain=-2:6, color=red!50, line width=1pt]{4}; \draw[line width=.5pt, green] (-2,0)--(0,0); \draw[green,fill=white] (0,0) circle(2pt); \draw[line width=.5pt, green] (4,0)--(6,0); \draw[green,fill=white] (4,0) circle(2pt); \end{axis} \end{tikzpicture} \end{document} 

Result:

middle and box axes combined

Note that given the extra overhead of writing and maintaining the second plot, the manual lines as used in the question may be the preferred solution.

1
  • It is interesting to see/learn that plots can be 'layered' like this. Thank you. Commented 16 hours ago
3

You can do it as in the code below.

\documentclass[tikz,border=3mm]{standalone} \usepackage{pgfplots} \pgfplotsset{compat=1.18} \begin{document} \begin{tikzpicture} \begin{axis}[ xmin=-2, xmax=6, ymin=-1.5, ymax=10, axis lines=box, axis line style={orange,line width=2pt}, xmajorgrids=true, ymajorgrids=true, major grid style={line width=0.2pt}, % regular grid lines % regular ticks (excluding 0) xtick={2,4}, ytick={2,4,6,8}, % add 0 as a "special" tick extra x ticks={0}, extra y ticks={0}, extra x tick style={ grid=major, major grid style={orange,line width=2pt} % thick line at x=0 }, extra y tick style={ grid=major, major grid style={orange,line width=2pt} % thick line at y=0 }, % tick marks / labels can be hidden xtick style={draw=none}, ytick style={draw=none}, xlabel=$x$, ylabel=$y$, height=5cm, axis equal image, ] \addplot [domain=-2:6, samples=100, color=blue, line width=1pt]{pow(x-2,2)}; \addplot [domain=-2:6, color=red, line width=1pt]{4}; \end{axis} \end{tikzpicture} \end{document} 

enter image description here

P.S.
I really hope that on the third attempt I finally understood the OP. I removed the two previous versions.

6
  • I don't want arrow axis, I want a framed plot. I tried to upload an image, but the link gets broken. Commented 22 hours ago
  • @mf67 If you want the plot to appear inside a frame, remove axis lines=middle. Commented 22 hours ago
  • @kabenyuk the OP wants lines in the middle and inside a frame. Commented 22 hours ago
  • 1
    @kabenyuk This was a neat solution. Is there some way to use style/defaults for tikz/pgf for common items like grid thickness, no tick marks etc. to minimze the definitions in each plot. I want my plots to have the same characteristics and it would be neat with some kind of "global settings" for those items that re-occur in each plot. Commented 16 hours ago
  • 1
    @mf67 you can easily defines styles with \pgfplotsset{my style/.style={whatever=x, another=y}} and then use mystyle in your axis environment... Commented 13 hours ago
1

Based on @Marijin answer, but plots styles are separated into three parts:

  • global settings for all plots in document
  • common settings for each picture local settings for each axis. For demonstrating of this settings devision is added one image:
\documentclass[border=3mm]{standalone}% <-- changed \usepackage{pgfplots} \pgfplotsset{compat=1.18, % common styles axis line style = thick, % just for empohasis axis line thickness grid, grid style = ultra thin, outer axis line style={ultra thin, gray}, % tick style={draw=none}, ticklabel style={font=\tiny}, % every axis plot post/.add style={}{very thick}, } \begin{document} \begin{tikzpicture}[scale=0.8] \pgfplotsset{% common for this picture height=5cm, axis equal image, % xmin=-2, xmax=6, ymin=-1.5, ymax=10, xtick distance=2, ytick distance=2, } % empty plot with box axes \begin{axis}[ xlabel=$x$, ylabel=$y$, ] \end{axis} % actual function plots \begin{axis}[ axis lines*=middle, ticks=none, % domain=-2:6, no marks, ] \addplot +[samples=100] {pow(x-2,2)}; \addplot +[samples=2] {4}; \end{axis} \end{tikzpicture} \qquad \begin{tikzpicture}[scale=0.8] \pgfplotsset{% common for this picture height=5cm, axis equal image, % xmin=-2, xmax=2, ymin=-4, ymax=4, xtick distance=1, ytick distance=1, } % empty plot with box axes \begin{axis}[ xlabel=$x$, ylabel=$y$, ] \end{axis} % actual function plots \begin{axis}[ axis lines*=middle, ticks=none, % domain=-2:2, no marks, ] \addplot +[samples=100] {pow(x,3)}; \addplot +[samples=2] {x}; \end{axis} \end{tikzpicture} \end{document} 

enter image description here

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.