1

I've just tried to make a graph with grid, with GeoGebra Classic 5. I've exported the file but things didn't work as I hoped to. Could someone help to figure this out? Here I give some picture detail:

  1. First picture: The given picture I need to recreate with TikZ.

First Image

  1. Second picture: The image produced from GeoGebra.

Second Image

Here is the code for my second image:

\documentclass[10pt]{article}
\usepackage{pgf,tikz,pgfplots}
\pgfplotsset{compat=1.15}
\usepackage{mathrsfs}
\usetikzlibrary{arrows}
\pagestyle{empty}
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm]
\begin{axis}[
x=1.0cm,y=1.0cm,
axis lines=middle,
ymajorgrids=true,
xmajorgrids=true,
xmin=-0.2,
xmax=2.84,
ymin=-1.3299999999999998,
ymax=1.8299999999999996,
xtick={-0.0,0.5,...,2.5},
ytick={-1.0,-0.5,...,1.5},]
\clip(-0.2,-1.33) rectangle (2.84,1.83);
\draw [line width=2.pt,domain=-0.2:0.0] plot(\x,{(-0.-0.*\x)/-10.});
\draw [line width=2.pt,domain=2.0:2.84] plot(\x,{(-0.-0.*\x)/9.});
\draw [line width=2.pt] (0.,1.)-- (2.,1.);
\draw [line width=2.pt,dash pattern=on 1pt off 1pt] (2.,1.)-- (2.,0.);
\draw [line width=2.pt,dash pattern=on 1pt off 1pt] (0.,1.)-- (0.,0.);
\end{axis}
\end{tikzpicture}
\end{document}
5
  • But, how could I name the axis though? Just like my first image?
    – Wilory Lu
    Aug 8, 2020 at 16:29
  • 2
    Hi and welcome. Please add the code of your attempts.
    – AndréC
    Aug 8, 2020 at 17:00
  • Sorry for the wait, I've just editted my post, hoped it help for the details.
    – Wilory Lu
    Aug 9, 2020 at 4:20
  • @pluton, wow thanks for editting my question. I didn't know that I did much mistake there.
    – Wilory Lu
    Aug 9, 2020 at 16:02

1 Answer 1

1

The image

enter image description here

is produced by

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17} % <---
\usetikzlibrary{arrows}
\usepackage{mathrsfs}

\begin{document}
\pagestyle{empty}
   \begin{tikzpicture}[line cap=round,line join=round]
\begin{axis}[
grid=both, minor tick num=1, % <---
axis lines=middle,
xlabel=$x$, ylabel=$g(x)$,
xtick={0,1,2,3},
extra x ticks={0},
tick label style={fill=white, font=\footnotesize},
xmin=-0.4,  xmax=2.8,
ymin=-1.4,  ymax=1.8,
            ]
\draw[very thick]
  (-0.4,0) -- (0,0) (0,1) -- (2,1) (2,0) -- (2.8,0); % <---
\draw[very thick, dashed] 
  (0,0) -- (0,1) (2,1) -- (2,0); % <---
\end{axis}
    \end{tikzpicture}
\end{document}

The main changes in above MWE in comparison to your MWE are marked by % <---.

Edit:

  • added is tick at origin, which enable to draw minor grid. It was deleted by mistake at previous editing of answer
  • extra x tick is added because diagram with used axis line=middle` doesn't enable to write tick label at origin
  • for lines are used \draw commands by which drawing straight line is simpler and faster as at using plot functions
3
  • Ohh, thanks a lot! But, what is MWE, though? And, I think you miss a vline on the grid, is there any way to fix that, (I mean vline on 0.5 tick) without make a tick? Oh ya, is there any reference so I could search more about fixing this case?
    – Wilory Lu
    Aug 9, 2020 at 12:53
  • @WiloryLu, oh at edit of answer I by mistake deleted tick at zero. Now corrected. for reference you need to read documentation for the pdfplots. Issues about ticks are explained in section 4.15 Tick Options, pp 334 -- 352.
    – Zarko
    Aug 9, 2020 at 14:04
  • OK, thanks. Looking into it.
    – Wilory Lu
    Aug 9, 2020 at 15:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .