pgf-tikz / pgfplots

pgfplots - A TeX package to draw normal and/or logarithmic plots directly in TeX in two and three dimensions with a user-friendly interface and pgfplotstable - a TeX package to round and format numerical tables. Examples in manuals and/or on web site.
http://pgfplots.sourceforge.net/
198 stars 34 forks source link

ylabel in two y-axis plot #324

Closed pgf-tikz-bot closed 4 years ago

pgf-tikz-bot commented 12 years ago

Migrated from SourceForge https://sourceforge.net/p/pgfplots/support-requests/6/ Author: sigmun Timestamp: 2012-02-07 09:22:32

4 down vote favorite 1 share [g+] share [fb] share [tw]

I am trying to draw a pgfplot with to different y-axis with their respective label in a full line width and I can't get an alignment of the whole tikzpicture within the margins. The problem seems to come from the ylabel which, on the left axis shift the plot to the right, and on the right axis is plotted out of the picture, ie in the margin. I try to add the ylabel style={overlay} so I get the plot centered but both ylabel are this time in the margins. For sure, I can put a 0.95\linewidth but I don't find this solution good enough since it isn't generalist.

Does anyone knows how can I fix this small example in an automatically way (meaning that pgfplot or tikz finds the right width alone) which produces this badly aligned and too wide plot wich is correct if no ylabel are specified ?

\documentclass{article} \usepackage[T1]{fontenc}

\usepackage{pgfplots} \pgfplotsset{compat=newest}

\setlength\fboxsep{0pt}

\begin{document} \centering{} \hrule{} |\hfill{}|\hfill{}|\\ \fbox{\begin{tikzpicture} \begin{axis}[ width=\linewidth, xmin=-5,xmax=5, axis y line*=left, ytick={}, axis x line*=top, ylabel={$y=x^{2}$ label}, ] \addplot {x^2}; \end{axis} \begin{axis}[ width=\linewidth, xmin=0,xmax=15, domain=0:15, axis y line*=right, axis x line*=bottom, ylabel={$y=x$ label}, ] \addplot[red] {x}; \end{axis} \end{tikzpicture}} |\hfill{}|\hfill{}|\\ \hrule{} \end{document}

pgf-tikz-bot commented 12 years ago

Migrated from SourceForge https://sourceforge.net/p/pgfplots/support-requests/6/#9f13 Author: sigmun Timestamp: 2012-02-07 09:22:37

https://sourceforge.net/p/pgfplots/support-requests/_discuss/thread/1c5b5944/9f13/attachment/ylabel.png plots with ylabel specified

pgf-tikz-bot commented 12 years ago

Migrated from SourceForge https://sourceforge.net/p/pgfplots/support-requests/6/#21eb Author: sigmun Timestamp: 2012-02-07 09:23:42

https://sourceforge.net/p/pgfplots/support-requests/_discuss/thread/1c5b5944/21eb/attachment/noylabel.png plots without ylabel specified

pgf-tikz-bot commented 12 years ago

Migrated from SourceForge https://sourceforge.net/p/pgfplots/support-requests/6/#2a93 Author: cfeuersaenger Timestamp: 2012-08-27 18:22:25

Hi Sigmun,

pgfplots/tikz offer a solution to get the axis rectangle centered properly - but it is quite close to what you did not like, namely to exclude axis descriptions of the bounding box. However, this leads to good results since the vertical bars typically "catch the eye" - and the axis labels are attached to them. Consequently, the solution still requires some adjustments to the width. The fully automatic scaling to a specific width is only supported for the axis rectangle; the size of axis rectangle plus descriptions is only a coarse grained estimated.

A complete solution could be

1. to use \begin{tikzpicture}[trim axis left, trim axis right] ....

which is better than overlay because it is applied to all axis descriptions to the left and right of the axis.

2. to make sure that the \centering command is in effect (it is not in your example): make sure that there is a paragraph after the material which is to be centered.

3. reduce the width *manually* using 0.95\linewidth as you said.

Here is the modified example:

\documentclass{article} \usepackage[T1]{fontenc}

\usepackage{pgfplots} \pgfplotsset{compat=newest}

\setlength\fboxsep{0pt}

\begin{document}

\centering{} \hrule{} |\hfill{}|\hfill{}|\\ \fbox{\begin{tikzpicture}[trim axis left,trim axis right] \begin{axis}[ width=0.95\linewidth, xmin=-5,xmax=5, axis y line*=left, ytick={}, axis x line*=top, ylabel={$y=x^{2}$ label}, ] \addplot {x^2}; \end{axis} \begin{axis}[ width=0.95\linewidth, xmin=0,xmax=15, domain=0:15, axis y line*=right, axis x line*=bottom, ylabel={$y=x$ label}, ] \addplot[red] {x}; \end{axis} \end{tikzpicture}}

|\hfill{}|\hfill{}|\\ \hrule{} \end{document}

I thought about a fully controlled width some time ago - but it seems to be quite involved since the scaling factors need to be computed *after* the complete axis has been drawn, then the axis needs to be redrawn - and that might need to be iterated. For the time being, this is out of scope. I consider your support as "feature request", though.

I apologize for the late answer - this request somehow slipped our attention :-(

Best regards

Christian

pgf-tikz-bot commented 12 years ago

Migrated from SourceForge https://sourceforge.net/p/pgfplots/support-requests/6/#3e4e Author: cfeuersaenger Timestamp: 2012-08-27 18:23:07

pgf-tikz-bot commented 12 years ago

Migrated from SourceForge https://sourceforge.net/p/pgfplots/support-requests/6/#65c6 Author: cfeuersaenger Timestamp: 2012-08-27 18:23:07

I close this request. I keep the feature request in mind.