2

Is it possible to change the direction of one (or both) axis?

I could achieve the visual result by using tikz and compute the coordinate by myself but this is obviously too long for a complex picture.

In the following there is the point X = (1,1) in a,b coordinates (the angle between a and b is 2*pi/3).

example

drawing using the following code

\documentclass{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
  \draw[->] (0,0) -- (-1,1.732050808) node[anchor=south] {$b$}; % (2*cos(2*pi/3),2*sin(2*pi/3))
  \draw[->] (0,0) -- (2,0) node[anchor=west] {$a$};
  \draw[dashed] (-0.5,0.866025404) -- (0.5,0.866025404);
  \draw[dashed] (1,0) -- (0.5,0.866025404) node[anchor=west] {$X$};
  \node[anchor=north] at (1,0) {$1$};
  \node[anchor=east] at (-0.5,0.866025404) {$1$};
\end{tikzpicture}
\end{document}

I want the same result using pgfplots and using (1,1) as coordinates.

Edit

Both solutions posted by percusse has the same problem, that is that TikZ takes as argument the angle in radians, not in degree. But that it's not a big problem.

After a check, this is the code:

\documentclass{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[y={(-0.5, 0.866025404)} ]
  \draw[->] (0,0) -- (0,2) node[anchor=south] {$b$};
  \draw[->] (0,0) -- (2,0) node[anchor=west] {$a$};
  \draw[dashed] (1,0) -- (1,1);
  \draw[dashed] (0,1) -- (1,1) node[anchor=west] {$X$};
  \node[anchor=north] at (1,0) {$1$};
  \node[anchor=east] at (0,1) {$1$};
\end{tikzpicture}
\end{document}

and this is the result:

example2

that is the visual result that I want. In his second solution instead the scale between the two axis is wrong.

If I try to implement the first solution with pgfplots I obtain nothing. This is the code:

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}[y={(-0.5, 0.866025404)}]
  \begin{axis}[axis lines=middle]
    \addplot[mark=*] coordinates {(1,1)};
    \draw[dashed] (1,0) -- (1,1);
    \draw[dashed] (0,1) -- (1,1) node[anchor=west] {$X$};
  \end{axis}
\end{tikzpicture}
\end{document}

And this is the result: example_pgfplots

gvgramazio
  • 2,660

1 Answers1

2

There are two direct possibilities together with other hacks

The first being, changing the unit vector and use dimensionless coordinates x=<...>, y=<...> as an option to the tikzpicture environment.

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[y={({cos(120)}, {sin(120)})}]
  \draw[->] (0,0) -- (-1,1) node[anchor=south] {$b$};
  \draw[->] (0,0) -- (2,0) node[anchor=west] {$a$};
  \draw[dashed] (-0.5,0.5) -- (0.5,0.5);
  \draw[dashed] (1,0) -- (0.5,0.5) node[anchor=west] {$X$};
  \node[anchor=north] at (1,0) {$1$};
  \node[anchor=east] at (-0.5,0.5) {$1$};
\end{tikzpicture}
\end{document}

The other is applying a transformation to the coordinate system and use just like you would code in a cartesian CS.

\documentclass{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[cm={1,0,{0.5*cos(120)},1,(0,0)}]
  \draw[->] (0,0) -- (-1,1) node[anchor=south] {$b$};
  \draw[->] (0,0) -- (2,0) node[anchor=west] {$a$};
  \draw[dashed] (-0.5,0.5) -- (0.5,0.5);
  \draw[dashed] (1,0) -- (0.5,0.5) node[anchor=west] {$X$};
  \node[anchor=north] at (1,0) {$1$};
  \node[anchor=east] at (-0.5,0.5) {$1$};
\end{tikzpicture}
\end{document}

enter image description here

It looks like I've made a mistake about the degree but you get the idea. Look at the manual for the explanation for each entry of cm key.

percusse
  • 157,807
  • There is no problem if the degree are wrong, I can fix it by myself. However can you please explain better the first possibility? I didn't understand it. – gvgramazio May 24 '18 at 13:53
  • @gvgramazio Added another example for it. – percusse May 24 '18 at 13:56
  • The mistake about degree is due to the fact that trigonometric functions in TikZ use radians, so it should be cos(2*pi/3). However there is still a point missing, the plot is not in scale now. – gvgramazio May 24 '18 at 13:57
  • I updated the question. Your first solution works fine with tikz but I'm not able to implement it with pgfplots. Your second solution change the scale of between the two axis instead. – gvgramazio May 24 '18 at 16:51
  • @gvgramazio In pgfplots you shouldn't be needing these. It has its own tools to handle these projections – percusse May 24 '18 at 17:07
  • Probably I wasn't clear enough but I'm looking for a solution in pgfplots, not in pure TikZ. – gvgramazio May 24 '18 at 17:19
  • @gvgramazio pgfplots implements its own coordinate system these are only for TikZ. I think you would have a direct result if you just ask your pgfplots question and TikZ trig functions work with degrees – percusse May 24 '18 at 18:12
  • I did it. The title of the question is about pgfplots, in the tags I put only pgfplots and not tikz, I stated in the question that I'm looking for a way to change the direction of axis in pgfplots. The only reference to tikz was my example but only because it was the only way to show what the visual result should look like. If you found something that suggest that the question is about tikz please let me know and I'll change it. – gvgramazio May 24 '18 at 19:23
  • @gvgramazio What is your goal inside pgfplots – percusse May 24 '18 at 20:45
  • plot some functions. what I really need (but I started from this question that I think is simpler) is a system on a plane with 3 axis, each axis at 120° from the other. – gvgramazio May 24 '18 at 21:01
  • I don't mean like a classical xyz projected on a plane in such a way that their projection have an angle of 120° between each other. – gvgramazio May 24 '18 at 21:03
  • @gvgramazio similar to this but with isometric projection then? https://tex.stackexchange.com/questions/84442/pgfplots-labels-and-width-issues-in-non-boxed-3d-plot-with-oblique-projection?rq=1 – percusse May 24 '18 at 21:03
  • visually maybe, but not really. In a classical xyz each axis has an angle of 90° with the others and it's defined on a 3D space. In my case, I need a system, let's call it abc, such that all the axis lie on a plane and have an angle of 120° between them. Of course is a little strange system and the same point can have different coordinates. – gvgramazio May 24 '18 at 21:09
  • @gvgramazio This is exactly what I mean by why don't you ask your pgfplots question. That is a completely different problem. I would suggest opening a new question and describing your axis specifications instead of this indirect way. – percusse May 24 '18 at 21:10
  • mmmh....ok, then I'll delete this question. However thank for your time. – gvgramazio May 24 '18 at 21:11
  • @gvgramazio My pleasure – percusse May 24 '18 at 21:12
  • Sorry, but I cannot delete it since it already has an answer. – gvgramazio May 24 '18 at 21:19
  • @gvgramazio No worries leave it as is then you can ask another one anyways – percusse May 24 '18 at 21:27