1

Let's say you want to draw a plot:

\documentclass[dvipsnames]{article}
\usepackage{pgfplots}
\usetikzlibrary{babel,calc,backgrounds,quotes,angles,patterns,decorations.markings,intersections,arrows,arrows.meta}
\pgfplotsset{compat=newest}                       
\usepgfplotslibrary{fillbetween}

\begin{document}

\begin{tikzpicture} \begin{axis}[ axis lines = middle, axis line style = {-Latex[round],very thick}, % enlargelimits = true, grid = both, grid style={help lines}, xmin = -4, ymin = -3.1, xmax = 4, ymax = 4, xtick = {-3, ..., 3}, ytick = {-3, ..., 3}, xlabel style={below right}, ylabel style={above left}, x tick label style={below left}, samples = 100, axis on top=true, xlabel = {$x$}, ylabel = {$y$}, ] \coordinate (O) at (0, 0);

\addplot[very thick, color=Mahogany] {-x^2 + 2};

\end{axis} \end{tikzpicture}

\end{document}

The result is:

enter image description here

How to make some fancy changes:

  1. make x tick labels below right for negative x and below left for positive x;

  2. show tick labels only for these points: (0, 0), (1, 0), (0, 1), (-1, 0), (0, -1)? If I change xtick = {-3, ..., 3} and ytick = {-3, ..., 3}, it will change the grid, but I want to keep it as it is for {-3, ..., 3) ticks.

Allok
  • 405

1 Answers1

1

Edit:

In the first try, according to OP comment, apparently I do not understood question. The second try is based on guessing, but I'm not sure what is desired result ...

\documentclass[dvipsnames, margin=3mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{arrows.meta,
%
                babel % for languages issues, not needed in this MWE
                } % other libraries are not used
\pgfplotsset{compat=1.17}
%\usepgfplotslibrary{fillbetween} % not used

\begin{document} \begin{tikzpicture} \begin{axis}[ width=9cm, % enlarged image width axis lines = middle, axis line style = {-Latex[round], very thick}, % grid, xmin = -3.5, xmax = 3.5, ymin = -3.5, ymax = 3.5, xtick = {-3,...,3}, % added, for tick labels placements tick style = {major tick length=3mm, semithick, color=black}, xticklabels={,,$-1$, , , , }, xticklabel style = {inner sep=2pt, anchor=north west, font=\footnotesize}, extra x ticks={0,1},
extra x tick labels={0,1}, extra x tick style={tick label style={xshift=-0.9em}}, yticklabels=\empty,
extra y ticks={-1,1},
xlabel = {$x$}, ylabel = {$y$}, xlabel style={below right}, ylabel style={above left}, axis on top, samples = 100, % ] \addplot[very thick, color=Mahogany] {-x^2 + 2}; \end{axis} \end{tikzpicture} \end{document}

enter image description here

Zarko
  • 296,517
  • thanks. However, the grid is now all over the place) Grey lines are over Oy, over the plot of -x^2 + 2 - is there a way to fix it? – Allok Nov 19 '20 at 12:55
  • 1
    @Allok: See https://tex.stackexchange.com/questions/456859/put-grid-below-plot-in-pgfplots – hpekristiansen Nov 19 '20 at 13:15
  • @Allok, the answer is almost as you request in question. Now I see, that I forgot on two labels. I can add them, however, fors please, clarify, what you like to have. Grid you can remove by deleting option grid in axis options. – Zarko Nov 19 '20 at 13:29
  • @Zarko, thanks again for the answer. I edited the post to clarify the problem. yes, I'd like to have the same grid (as it was for {-3, ..., -3} ticks) – Allok Nov 19 '20 at 19:34
  • @hpekristiansen, thanks for the link – Allok Nov 19 '20 at 19:35
  • @Allok, sorry, after your edit of the question is (to me) even less clear. – Zarko Nov 19 '20 at 19:46