Sorry if this doesn't seem well-researched, but I couldn't find the answer anywhere.
Is there a way to reverse an axis in a TikZ \datavisualization
? Concretely, I want to reverse the y
axis in a cartesian system, so that positive points down.
Clarification: I didn't express myself correctly. Basically I only want to get a y
axis in which positive is down and negative is
up. But that's not simply putting the $y$
label at the bottom; I
actually want positive to be down and the arrow to point down. The
"aiming for" image is confusing because there are no tick marks. But
if I added them, the y axis would show 1 (and not -1). Of course, then
the whole graph would appear on the opposite side. But I could easily
change the csv
files to reverse all of the y coordinates. In fact, the
only reason I put negative y coordinates in the csv files was
precisely because I didn't know how to "properly" flip the axis, but
the original data is all positive (both in x
and y
). Sorry for the confusion.
@whatisit gave the solution straight away, which was yscale=-1
.
Now the only problem is getting the correct anchoring of tick
marks/labels. Again, sorry for the confusion.
From what I've gathered from this question, when manually specifying an axis
you can use the key y dir=reverse
. But I haven't found any equivalent way using the \datavisualization
library. I'm using the school book axes
system.
Here's what I've tried:
\datavisualization [school book axes,
y dir=reverse]
\datavisualization [school book axes,
all axes={y dir=reverse}]
\datavisualization [school book axes,
y axis=reverse]
\datavisualization [school book axes,
y={dir=reverse}]
MWE (updated)
This is what I'm working on. I've stripped down the preamble to a minimum.
\documentclass[11pt, a4paper]{article}
\usepackage[margin=2.5cm]{geometry}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{datavisualization}
\usepackage{filecontents}
\begin{filecontents}{1.csv}
0, 0
0.100000, 0.102985
0.200000, 0.284078
0.300000, 0.431294
0.800000, 0.886640
0.900000, 0.946767
1.000000, 1.000000
\end{filecontents}
\begin{filecontents}{2.csv}
0, 0
0.104298, 0.280000
0.213643, 0.440000
0.300496, 0.540000
0.709127, 0.860000
0.820097, 0.920000
0.904455, 0.960000
1.000000, 1.000000
\end{filecontents}
\begin{filecontents}{3.csv}
0, 0
0.105170, 0.292368
0.205373, 0.440170
0.325606, 0.575855
0.435812, 0.676699
0.502248, 0.729587
0.663074, 0.838455
0.760352, 0.893359
0.871594, 0.947572
1.000000, 1.000000
\end{filecontents}
\begin{document}
\begin{tikzpicture}[>=stealth]
\datavisualization data group {methods} = {
data [set=m1, read from file={1.csv}]
data [set=m2, read from file={2.csv}]
data [set=m3, read from file={3.csv}]
};
\datavisualization [school book axes,
visualize as scatter/.list={m1,m3,m2},
all axes={unit length=8cm},
x axis={label=$x$},
y axis={label=$y$},
yscale=-1,
style sheet={vary hue},
style sheet={cross marks},
data/headline={x,y}]
data group {methods};
\end{tikzpicture}
\end{document}
The complete CSV files can be found here.
Current output (updated)
Note: as @whatisit suggested, I'm using yscale=-
. The problem now is how to change the label/tick anchoring.
What I'm aiming for
Something like this:
\begin{tikzpicture}[>=stealth, scale=6]
\coordinate (X) at (1.3, 0);
\coordinate (Y) at (0, -1.1);
\draw[black!30, very thin] (X) grid (Y);
\draw[black!5, very thin, step=0.2] (X) grid (Y);
\draw[->, very thick] (0, 0) -- node[very near end, anchor=south west] {\Large$x$} (X);
\draw[->, very thick] (0, 0) -- node[very near end, anchor=north east] {\Large$y$} (Y);
\draw[blue, loosely dotted, very thick] plot file {1.data};
\draw[purple, loosely dotted, ultra thick] plot file {2.data};
\draw[red, loosely dotted, very thick] plot file {3.data};
\end{tikzpicture}
Here 1.data
, 2.data
and 3.data
are exactly like the CSV files but separated by spaces instead of commas.
Best Answer
Ok, this was a little tricky, but I was able to figure it out with
data visualization
. The approach scales the y-axis by -1 (as mentioned in my comment). As you mentioned, however, this also flips the axis values and not only the data.Updated Version
Based on the updated description/question, here is a version that looks the closest to the desired image. Honestly, there are no tick labels in that version...so my previous answers are largely unnecessary (flip the scale and remove the ticks!).
The axes labels aren't positioned vary nicely by default, so I included a small shift for each, to look like your image. Added a grid which appears in the image. Color coded them...because too many X's from different data sets is confusing. Finally, you'll notice that the x and y
min value
are set to0.045
; this was because the grids and axes overlapped for some reason...this was just a tweak so that the intersect without any overlap (really, just a hack for appearance purposes).Last note: I removed the negative sign from the filecontents. So, what you are see is positive y values on the negative y-axis, as request from the original question.
Original approach:
Perhaps this is not the most elegant approach, but you can forcibly "re-calculate" the y-value tick labels with:
And then update the
ticks
values within\datavisualization
parameters:Here is a complete version, which I put into @marmot's answer above (except for the color coding):
You'll notice that the axis arrow is now pointing down. The axes visualizations are customizable, but I do not know exactly what you need...so I left it this way for now.
Second approach: (image is the same as approach #1)
I found a slightly different way that doesn't require a new command and re-calculating the y-axis ticks. It's not automatic, however, and requires using the same length from
all axes={length=6cm}
(in my example). You need these three options in\datavisualization
:all axes={length=6cm}
yscale=-1
y axis={min value=-1,max value=1,ticks={step=0.5,minor steps between steps=4,rotate=180,yshift=-6cm}}
Same code as above, but here is the
tikzpicture
code for version #2:The approach takes advantage of simply rotating the y-axis 180 degrees. The problem is that the pivot point is not as 0, but at the maximum value on the y-axis. Therefore, you need to shift it downwards by the length of the y-axis.