How to add subscript in a vertex in tikz-latex? - latex

The problem I am facing is that I can define the label for each vertex, but then when I move to position the vertices south, or east, that just creates a new vertex.
\begin{figure}
\tikzstyle{VertexStyle} = [shape = ellipse,minimum width= 6ex,draw]
\tikzstyle{EdgeStyle} = [->,>=stealth']
\begin{tikzpicture}[scale=0.5, auto=left,every node/.style={circle}]
\SetGraphUnit{4}
\Vertex[L=$x_{1}$]{x};
\Vertex[L=$h_{1}$]{h};
\EA(x){h}
\Edges(x)(h)
\end{tikzpicture}
\end{figure}
Following is the output I get then:

For your code, line-by-line, the following is happening:
\Vertex[L=$x_{1}$]{x};
This creates a vertex (at the origin) with the name x and a Label $x_1$.
\Vertex[L=$h_{1}$]{h};
This also creates a vertex (at the origin) with the name h with a Label $h_1$.
\EA(x){h}
Here you add a vertex h to the EAst of x. The label name is h as well as the label.
\Edges(x)(h)
Now you try to add an edge between x and h.
Perhaps this is something that you might be interested in:
\Vertex[x = 1cm, L = $x_1$]{x};
Place a vertex called x with a Label $x_1$ (1cm east of the origin, just because).
\Vertex[x = 4cm, L = $h_1$]{h};
Place a vertex h with Label $h_1$ (4cm east of the origin, just so it doesn't sit on/close to x).
\Edges(x,h)
Draw an edge between nodes x and h.
\SetVertexMath
Node names and labels will include math content.
\EA(h){h_2}
Create a new vertex h_2 to the EAst of h.
\Edges(h,h_2)
Draw another edge between h and h_2.
Here is a complete minimal example:
\documentclass{article}
\usepackage{tkz-berge}
\usetikzlibrary{shapes}
\begin{document}
\begin{figure}
\tikzstyle{VertexStyle} = [
shape = ellipse,
minimum width = 6ex,
draw]
\tikzstyle{EdgeStyle} = [
->,
> = stealth']
\begin{tikzpicture}[scale=0.5]
\SetGraphUnit{4}
\Vertex[x = 1cm, L = $x_1$]{x};
\Vertex[x = 4cm, L = $h_1$]{h};
\Edges(x,h)
\SetVertexMath
\EA(h){h_2}
\Edges(h,h_2)
\end{tikzpicture}
\end{figure}
\end{document}

Related

Fill between function does not account for areas of other curves

In the below graph, the fill below function works great for filling in between, path B and path C. But it is not so good when I do not want to fill anything below path A (that is I do not want the area under blue line shaded, I do not how to get rid of that shaded area). Is there a way to do this?
\usepackage{tikz}
\usepackage{pgfplots}
\begin{tikzpicture}
\begin{axis}[
axis lines = left,
xlabel = $x$,
ylabel = $ $,
ymin = 0,
ymax = 6,
xmin = 0,
xmax = 4,
samples = 100,
xticklabels={},
yticklabels={},
]
\addplot[color=blue, domain=0:4,name path =A] {4-x};
\addplot[color=red, domain=0:4, name path =C] {x};
\addplot[color=purple, domain=0:4, name path =B] {1.5*x};
\addlegendentry{$f(x)= a-bx$}
\addlegendentry{$g(x)= \tilde{d}x$}
\addlegendentry{$g(x)= dx$}
\addplot fill between[
of = C and B,
soft clip={domain=1.6:2},
];
\addplot fill between[
of = C and A,
soft clip={domain=1.6:2},
];
\end{axis}
\end{tikzpicture}

adjust labels on tikz bar chart

How can I adjust the labels on the y-axis?
In the current version, the labels are not aligned to the bars.
The example is from:
https://texample.net/tikz/examples/bar-chart/
I add a few more data fields and removed the second category.
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[title = Contributions per category
at LaTeX-Community.org,
xbar,
y axis line style = { opacity = 0 },
axis x line = none,
tickwidth = 0pt,
enlarge y limits = 0.2,
enlarge x limits = 0.02,
symbolic y coords = {test1,test2,test3,test4,LaTeX, Tools, Distributions, Editors},
nodes near coords,
]
\addplot coordinates { (57727,LaTeX) (5672,Tools)
(2193,Distributions) (11106,Editors)
(11106,test1)
(11106,test2)
(11106,test3)
(11106,test4)
};
\legend{Topics}
\end{axis}
\end{tikzpicture}
\end{document}
With the ytick=data option:
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
title = Contributions per category at LaTeX-Community.org,
xbar,
y axis line style = { opacity = 0 },
axis x line = none,
tickwidth = 0pt,
enlarge y limits = 0.2,
enlarge x limits = 0.02,
symbolic y coords = {test1,test2,test3,test4,LaTeX,Tools,Distributions,Editors},
ytick=data,
nodes near coords,
nodes near coords align={horizontal},
]
\addplot coordinates {
(57727,LaTeX)
(5672,Tools)
(2193,Distributions)
(11106,Editors)
(11106,test1)
(11106,test2)
(11106,test3)
(11106,test4)
};
\legend{Topics}
\end{axis}
\end{tikzpicture}
\end{document}

How to draw double y-axis barplot graph like this in pgfplot?

I used this table to draw the plot using matplotlib.
Use two \begin{axis} ... \end{axis} environments, one for the left and the other for the right. You can find the relevant part axis y line in the code below.
To prevent the bars from being on top of each other, bar shift is used (and set to the half of the bar width so that the bars are glued together).
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{width=10cm,height=8cm,compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar,
symbolic x coords={A,B,C,D},
bar width=0.6cm, bar shift=-0.3cm,
xtick=data,
axis y line*=left,
ylabel=axis1,
xlabel=name,
]
\addplot[draw=red,fill=red] coordinates {
(A,16) (B,18) (C,3) (D,10)
};
\end{axis}
\begin{axis}[
ybar,
symbolic x coords={A,B,C,D},
bar width=0.6cm, bar shift=0.3cm,
xtick=data,
axis y line*=right,
ylabel=axis2
]
\addplot[draw=blue,fill=blue] coordinates {
(A, 299239.74) (B, 292816.67) (C, 72799.22) (D, 116248.54)
};
\end{axis}
\end{tikzpicture}
\end{document}

Line function after projective transform

I have a question related to projective transform. Suppose now we know a line function ax+by+c=0in an image, and the image will go through a projective distortion, and the distortion can be represented as a projective transformation matrix :
Then after the porjective transformation, how could I know the line function in the new distorted image? Thanks!
** EDIT **
Based on the suggestion, I have found the answer. Here, I posted the MATLAB codes to illustrate it:
close all;
% Step 1: show the images as well as lines on it
imshow(img);
line = hor_vt{1}.line(1).line; a = line(1); b=line(2); c=line(3);
[row,col] = size(img);
x_range = 1:col;
y_range = -(a*x_range+c)/b;
hold on; plot(x_range,y_range,'r*');
line = hor_vt{1}.line(2).line; a = line(1); b=line(2); c=line(3);
y_range = -(a*x_range+c)/b;
hold on; plot(x_range,y_range,'y*');
% Step 2: show the output distorted image that goes through projective
% distortion.
ma_imshow(output);
[row,col] = size(output);
x_range = 1:col;
line = hor_vt{1}.line(1).line;
line = reverse_tform.tdata.Tinv*line(:); % VERY IMPORT
a = line(1); b=line(2); c=line(3);
y_range = -(a*x_range+c)/b;
hold on; plot(x_range,y_range,'r*');
disp('angle');
disp( atan(-a/b)/pi*180);
line = hor_vt{1}.line(2).line;
line = reverse_tform.tdata.Tinv*line(:); % VERY IMPORT
a = line(1); b=line(2); c=line(3);
y_range = -(a*x_range+c)/b;
hold on; plot(x_range,y_range,'y*');
disp('angle');
disp( atan(-a/b)/pi*180);
The original images with two lines on it:
After projective distoration, the output image with lines on it becomes:
Here is an easier way to understand it than in your code above.
Given a non-singular homography H (i.e. a homography represented by a 3x3 matrix H with non-zero determinant):
Homogeneous 2D points (represented as 3D column vectors) transform from the right:
p' = H * p
2D lines (represented as 3D row vectors of their 3 coefficients) transform from the left by the inverse homography:
l' = l * H^-1
Proof: for every point p belonging to line l it is l * p = 0. But then l * (H^-1 * H) * p = 0, since (H^-1 * H) = I is the identity matrix. The last equation, by the associative property, can be rewritten as (l * H^-1) * (H * p) = 0. But, for every p belonging to the line, p' = H * p is the same point transformed by the homography. Therefore the last equation says that these same points, in transformed coordinates, belong to a line with coefficients l' = l * H^-1, QED.
I'm no mathematician so maybe there's a better solution, but you could use the equation as it is and then transform the output by multiplying by the transformation matrix.

How to Find Intersections with Ellipses in PGF/TikZ

I am trying to display a sphere in PGF/TikZ to illustrate the idea of great circles.
The code for my current result is:
\begin{tikzpicture}
\tikzfading[name=fade right,
left color=transparent!20,
right color=transparent!90]
\tikzfading[name=fade out,
inner color=transparent!100,
outer color=transparent!10]
\tikzfading[name=fade right gc,
left color=transparent!0,
right color=transparent!70]
\draw [<->, dashed] (0,-5) -- (0,5); % y-axis
\draw [->, dashed] (0, 0) -- (20:5); % x-axis
\draw [->, dashed] (0, 0) -- (200:5); % x-axis
\draw [->, dashed] (0, 0) -- (340:5); % z-axis
\draw [->, dashed] (0, 0) -- (160:5); % z-axis
\fill [color=cyan, opacity=0.15, path fading=fade out] (0,0) circle (4cm); % bounding circle
\fill [color=cyan, opacity=0.25, path fading=fade right, fading angle=90] (0,0) ellipse (4cm and 1cm); % x-y-axis area
% great circle 1
\draw [rotate=-40, color=red, path fading=fade right gc, fading angle=40] (0,0) ellipse (4cm and 1cm);
% great circle 2
\draw[rotate=5, color=red, path fading=fade right gc, fading angle=5] (0,0) ellipse (1.5cm and 4cm);
\end{tikzpicture}
How do I
find the two points of intersection of the two red ellipses (commented as great circle 1 and 2),
find the point of intersection of a line (originating at the center (0,0)) with a ellipse, and
place a little circle or rectangle there?
Placing a little circle or rectangle there is not an issue.
Thank you very much!
Check out section 4.1.4. of the TikZ and PGF manual, titled "The Intersection of the Circles." You need to use the intersections library, which allows you to use the name intersections key, as in \path [name intersections={of=path 1 and path 2}] ;. To use this, you'll need to use the name path key, as in \draw [name path = y axis, <->, dashed] (0,-5) -- (0,5) ; % y-axis. Accessing the intersections seems to vary between versions; my local copy of the manual has different instructions from the one I linked you to. However, at least on my version, you then access the intersections with (intersection-1), (intersection-2), etc. To get circles at each intersection in your example, then, I would change your code to look like the following:
\begin{tikzpicture}
\tikzfading[ name = fade right
, left color = transparent!20
, right color = transparent!90 ]
\tikzfading[name = fade out
, inner color = transparent!100
, outer color = transparent!10 ]
\tikzfading[name = fade right gc
, left color = transparent!0
, right color = transparent!70]
\draw [name path = y axis, <->, dashed] (0,-5) -- (0,5) ; % y-axis
\draw [name path = x- axis, ->, dashed] (0, 0) -- (20:5) ; % x-axis
\draw [name path = x+ axis, ->, dashed] (0, 0) -- (200:5) ; % x-axis
\draw [name path = z+ axis, ->, dashed] (0, 0) -- (340:5) ; % z-axis
\draw [name path = z- axis, ->, dashed] (0, 0) -- (160:5) ; % z-axis
% bounding circle
\fill [color=cyan, opacity=0.15, path fading=fade out]
(0,0) circle (4cm) ;
% x-y-axis area
\fill [color=cyan, opacity=0.25, path fading=fade right, fading angle=90]
(0,0) ellipse (4cm and 1cm);
% great circle 1
\draw [ name path = great circle 1
, rotate = -40
, color = red
, path fading = fade right gc
, fading angle = 40]
(0,0) ellipse (4cm and 1cm);
% great circle 2
\draw [ name path = great circle 2
, rotate = 5
, color = red
, path fading = fade right gc
, fading angle = 5]
(0,0) ellipse (1.5cm and 4cm);
% Intersections
\path [name intersections={of=great circle 1 and great circle 2}] ;
\foreach \i in {1,...,4}
\fill [color=red] (intersection-\i) circle (2pt) ;
\path [name intersections={of=y axis and great circle 1}] ;
\fill (intersection-1) circle (2pt) ;
\fill (intersection-2) circle (2pt) ;
\path [name intersections={of=y axis and great circle 2}] ;
\fill (intersection-1) circle (2pt) ;
\fill (intersection-2) circle (2pt) ;
\foreach \a in {x,z} {
\foreach \ss in {+,-} {
\def\s.{\ss} % Otherwise the space in `\a\s axis` would get gobbled.
\path [name intersections={of=\a\s. axis and great circle 1}] ;
\fill (intersection-1) circle (2pt) ;
\path [name intersections={of=\a\s. axis and great circle 2}] ;
\fill (intersection-1) circle (2pt) ;
}
}
\end{tikzpicture}
Other than the reformatting (to avoid the horizontal scroll bar), all I have changed of your existing code is to add the name path key to your axes and great circles. I then added the intersections code, which should be relatively self-explanatory. Remember to \usetikzlibrary{intersections} first, and everything should work.

Resources