TransWikia.com

How to improve this (added hatching)

TeX - LaTeX Asked on November 1, 2021

documentclass[12pt,a4paper,openany]{book}

usepackage{tikz}
usepackage{pgfplots}
pgfplotsset{compat=1.15}
usepackage{amsmath,amssymb,amsthm}
%%%%%%%%%%%%%%%%%%%
usepgfplotslibrary{fillbetween}
usetikzlibrary{patterns,shapes.geometric}
begin{document}

begin{tikzpicture}[line cap=round,line join=round,x=1cm,y=1cm]
begin{axis}[unbounded coords=jump,
x=1cm,y=1cm,
axis lines=middle,
xmin=-8,
xmax=8,
ymin=-5,
ymax=5,
ytick=empty,
xtick=empty]
addplot[line width=2pt,color=blue,smooth,samples=51,domain=-8:-0.1,name
path=left] {(1/x)} node[pos=0.1,below=40mm]{$(A_3)$}node[pos=0.1,below=20mm,right=40mm]{$(mathcal{C}_1)$};
addplot[line width=2pt,color=blue,smooth,samples=51,domain=0.1:8,name
path=right] {(1/x)} node[pos=0.95,above=20mm]{$(A_1)$}node[pos=0.95,above=20mm,left=50mm]{$(mathcal{C}_1)$};
addplot[line width=2pt,color=red,smooth,samples=51,domain=0.1:8,name
path=right] {-(1/x)} node[pos=0.95,below=40mm]{$(A_4)$}node[pos=0.95,below=20mm,left=50mm]{$(mathcal{C}_2) $};
addplot[line width=2pt,color=red,smooth,samples=51,domain=-8:-0.1,name
path=left] {-(1/x)} node[pos=0.1,above=30mm]{$(A_4)$}node[pos=0.1,above=20mm,right=40mm]{$(mathcal{C}_2) $};
end{axis}
end{tikzpicture}
end{document}

I want to obtain something like this

enter image description here

2 Answers

Compile here: Ahihi do ngoc

import graph;
import patterns;
usepackage("amsmath");

// The default unit and linewidth
unitsize(1cm);
size(8cm,false);
defaultpen(linewidth(1bp));

// Definition of the graphs
real F(real x){return 1/x;}
real G(real x){return -1/x;}
path f1=graph(F,-8,-0.2,350),f2=graph(F,0.2,8,350),
        g1=graph(G,-8,-0.2,350),g2=graph(G,0.2,8,350);

// Draw patterns
picture pic1, pic2;
add("hatch1",hatch(NE));
fill(pic1,box((-8,F(-0.2)),(8,F(0.2))),pattern("hatch1"));
clip(pic1,f1--(-8,F(-0.2))--cycle^^f2--(8,F(0.2))--cycle);
add(pic1);

add("hatch2",hatch(SE));
fill(pic2,box((-8,F(-0.2)),(8,F(0.2))),pattern("hatch2"));
clip(pic2,g1--(-8,F(0.2))--cycle^^g2--(8,F(-0.2))--cycle);
add(pic2);

pic2=new picture;
fill(pic2,box((-8,F(-0.2)),(8,F(0.2))),cyan);
path AhihidoNgoc=f1--g2--reverse(f2)--reverse(g1)--cycle;
clip(pic2,AhihidoNgoc);
add(pic2);

// Draw two graphs
draw(f1^^f2,blue);
draw(g1^^g2,red);

// Draw Xaxis and Yaxis
path Xaxis=(-8,0)--(8,0),Yaxis=(0,F(0.2))--(0,F(-0.2));
draw(Xaxis,Arrow(1bp,arrowhead=TeXHead));
draw(Yaxis,BeginArrow(1bp,arrowhead=TeXHead));

// Add labels
label(Label("$(mathcal{C}_1)$",Relative(0.6),UnFill),f1,blue);
label(Label("$(mathcal{C}_1)$",Relative(0.4),LeftSide,UnFill),f2,blue);
label(Label("$(mathcal{C}_2)$",Relative(0.6),LeftSide,UnFill),g1,red);
label(Label("$(mathcal{C}_2)$",Relative(0.4),UnFill),g2,red);

label(Label("$(A_1)$",UnFill),(7,G(-0.25)),blue);
label(Label("$(A_2)$",UnFill),(-7,G(-0.25)),red);
label(Label("$(A_3)$",UnFill),(-7,G(0.25)),blue);
label(Label("$(A_4)$",UnFill),(7,G(0.25)),red);

frame f;
label(f,Label("$(A_5)$"),0,Fill(white));
add(rotate(45)*f);

enter image description here

Answered by user213378 on November 1, 2021

For hatching, I didn't use patterns as it is hard to customize. In this attempt, I used:

  • rotated grid to create the hatching (you can play on the distance between parallel lines by changing the ystep parameter)
  • clip to specify the filling area

Here is the corresponding code:

documentclass[12pt,a4paper,openany]{book}

usepackage{pgfplots}
pgfplotsset{compat=1.15}
usepackage{amsmath,amssymb,amsthm}   

begin{document}

begin{tikzpicture}[line cap=round,line join=round,x=1cm,y=1cm]
begin{axis}[unbounded coords=jump,
  x=1cm,y=1cm,
  axis lines=middle,
  xmin=-8,
  xmax=8,
  ymin=-5,
  ymax=5,
  ytick=empty,
  xtick=empty,
  axis on top]

% A3 hatch
begin{scope}
  clip plot[samples=51,domain=-8:-0.1] (x, {1/x}) -| (-8,-1/8) --cycle;
  draw[blue!20,xstep=0cm,ystep=0.5cm,rotate=45] (-15,-15) grid (15,15);
end{scope}

% A1 hatch
begin{scope}
  clip plot[samples=51,domain=0.1:8] (x, {1/x}) |- (0.1,1/0.1) --cycle;
  draw[blue!20,xstep=0cm,ystep=0.5cm,rotate=45] (-15,-15) grid (15,15);
end{scope}

% A4 hatch
begin{scope}
  clip plot[samples=51,domain=-8:-0.1] (x, {-1/x}) -| (-8,1/8) --cycle;
  draw[red!20,xstep=0cm,ystep=0.5cm,rotate=-45] (-25,-25) grid (15,15);
end{scope}

% A2 hatch
begin{scope}
  clip plot[samples=51,domain=0.1:8] (x, {-1/x}) |- (0.1,-1/0.1) --cycle;
  draw[red!20,xstep=0cm,ystep=0.5cm,rotate=-45] (-25,-25) grid (15,15);
end{scope}

% Fill between 
begin{scope}
  fill[yellow!10,opacity=] plot[samples=51,domain=0.1:8] (x, {-1/x}) -- plot[samples=51,domain=8:0.1] (x, {1/x}) -- plot[samples=51,domain=-0.1:-8] (x, {-1/x}) -- plot[samples=51,domain=-8:-0.1] (x, {1/x}) -- cycle ; 
end{scope}


% Your code (I removed path names)
addplot[line width=2pt,color=blue,smooth,samples=51,domain=-8:-0.1] {(1/x)};

addplot[line width=2pt,color=blue,smooth,samples=51,domain=-8:-0.1] {(1/x)} node[pos=0.1,below=40mm]{$(A_3)$}node[pos=0.1,below=20mm,right=40mm]{$(mathcal{C}_1)$};

addplot[line width=2pt,color=blue,smooth,samples=51,domain=0.1:8] {(1/x)} node[pos=0.95,above=20mm]{$(A_1)$}node[pos=0.95,above=20mm,left=50mm]{$(mathcal{C}_1)$};
addplot[line width=2pt,color=red,smooth,samples=51,domain=0.1:8] {-(1/x)} node[pos=0.95,below=40mm]{$(A_4)$}node[pos=0.95,below=20mm,left=50mm]{$(mathcal{C}_2) $};
addplot[line width=2pt,color=red,smooth,samples=51,domain=-8:-0.1] {-(1/x)} node[pos=0.1,above=30mm]{$(A_4)$}node[pos=0.1,above=20mm,right=40mm]{$(mathcal{C}_2) $};
end{axis}
end{tikzpicture}
end{document}

yields:

enter image description here

For hatching the middle region, here is the corresponding code:

documentclass[12pt,a4paper,openany]{book}

usepackage{pgfplots}
pgfplotsset{compat=1.15}
usepackage{amsmath,amssymb,amsthm}
%%%%%%%%%%%%%%%%%%%


begin{document}

begin{tikzpicture}[line cap=round,line join=round,x=1cm,y=1cm]
begin{axis}[unbounded coords=jump,
  x=1cm,y=1cm,
  axis lines=middle,
  xmin=-8,
  xmax=8,
  ymin=-5,
  ymax=5,
  ytick=empty,
  xtick=empty,
  axis on top]

% A3 hatch
begin{scope}
  clip plot[samples=51,domain=-8:-0.1] (x, {1/x}) -| (-8,-1/8) --cycle;
  draw[blue!20,xstep=0cm,ystep=0.5cm,rotate=45] (-15,-15) grid (15,15);
end{scope}

% A1 hatch
begin{scope}
  clip plot[samples=51,domain=0.1:8] (x, {1/x}) |- (0.1,1/0.1) --cycle;
  draw[blue!20,xstep=0cm,ystep=0.5cm,rotate=45] (-15,-15) grid (15,15);
end{scope}

% A4 hatch
begin{scope}
  clip plot[samples=51,domain=-8:-0.1] (x, {-1/x}) -| (-8,1/8) --cycle;
  draw[red!20,xstep=0cm,ystep=0.5cm,rotate=-45] (-25,-25) grid (15,15);
end{scope}

% A2 hatch
begin{scope}
  clip plot[samples=51,domain=0.1:8] (x, {-1/x}) |- (0.1,-1/0.1) --cycle;
  draw[red!20,xstep=0cm,ystep=0.5cm,rotate=-45] (-25,-25) grid (15,15);
end{scope}

% Fill between 
begin{scope}
  clip plot[samples=51,domain=0.1:8] (x, {-1/x}) -- plot[samples=51,domain=8:0.1] (x, {1/x}) -- plot[samples=51,domain=-0.1:-8] (x, {-1/x}) -- plot[samples=51,domain=-8:-0.1] (x, {1/x}) -- cycle ; 

  draw[orange!40,xstep=0cm,ystep=0.5cm,rotate=-45] (-25,-25) grid (15,15);
  draw[orange!40,xstep=0cm,ystep=0.5cm,rotate=45] (-25,-25) grid (15,15);
  node[above right] at (0,0){$(A_5)$};

end{scope}



addplot[line width=2pt,color=blue,smooth,samples=51,domain=-8:-0.1] {(1/x)};

addplot[line width=2pt,color=blue,smooth,samples=51,domain=-8:-0.1] {(1/x)} node[pos=0.1,below=40mm]{$(A_3)$}node[pos=0.1,below=20mm,right=40mm]{$(mathcal{C}_1)$};

addplot[line width=2pt,color=blue,smooth,samples=51,domain=0.1:8] {(1/x)} node[pos=0.95,above=20mm]{$(A_1)$}node[pos=0.95,above=20mm,left=50mm]{$(mathcal{C}_1)$};
addplot[line width=2pt,color=red,smooth,samples=51,domain=0.1:8] {-(1/x)} node[pos=0.95,below=40mm]{$(A_4)$}node[pos=0.95,below=20mm,left=50mm]{$(mathcal{C}_2) $};
addplot[line width=2pt,color=red,smooth,samples=51,domain=-8:-0.1] {-(1/x)} node[pos=0.1,above=30mm]{$(A_4)$}node[pos=0.1,above=20mm,right=40mm]{$(mathcal{C}_2) $};
end{axis}
end{tikzpicture}
end{document}

which yields:

enter image description here

In this case, I've added two rotated grids.

Answered by LaTeXdraw-com on November 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP