2 y 轴线和 ybar 图上的图例问题

2 y 轴线和 ybar 图上的图例问题

我似乎无法找到一种方法来将图例符号切换为双 y 轴图中的 ybar 符号。以下是我的问题的一个最小工作示例

结果是:

在此处输入图片描述

因此,该图的唯一问题是我想将 Plot 1 图例图像切换为默认 ybar 图表的图像。我尝试摆弄命令,\addlegendimage但似乎不起作用。有人有解决方案吗?使用的代码如下。

\documentclass[a4paper]{scrartcl}
    \usepackage{pgfplots} 
    \pgfplotsset{compat=1.10}
    \usepackage{tikz}
    \usepackage{filecontents}
    \usepgfplotslibrary{dateplot}

\begin{filecontents*}{data.csv}
date,rate,install
2012-01-01,48.84,24.4299855
2012-02-01,48.84,173.5773958
2012-03-01,48.84,110.7117086
2012-04-01,22.59,17.4220445
2012-05-01,22.59,36.01732503
2012-06-01,22.59,64.42770708
2012-07-01,22.59,197.822088
2012-08-01,17.22,14.8125984
2012-09-01,17.22,18.78518356
2012-10-01,17.22,44.10470415
2012-11-01,16.11,25.922787
2012-12-01,16.11,21.39109775
2013-01-01,16.11,24.72410623
2013-02-01,16.11,27.6975024
2013-03-01,16.11,33.65679443
2013-04-01,16.11,51.21122378
2013-05-01,16.11,36.79503848
2013-06-01,16.11,67.02365677
2013-07-01,15.54,33.01713989
2013-08-01,15.54,40.61720797
2013-09-01,15.54,38.26365551
2013-10-01,15.54,44.52759475
2013-11-01,15.54,44.36376502
2013-12-01,15.54,42.85926874
\end{filecontents*}

\begin{document}


\begin{figure}[H]
\centering
\begin{tikzpicture}
\pgfplotsset{
    scale only axis,
    date coordinates in=x,
    x tick label style={/pgf/number format/1000 sep=},
    xticklabel={\year},
    date ZERO=2010-01-01,
    xmin=2012-01-01, xmax=2014-01-01,
    width  = 0.9*\textwidth,
}

\begin{axis}[
  axis y line*=right,
  axis x line=none,
  ymin=0, ymax=200,
  ybar=1*\pgflinewidth,
  bar width=4pt,
  ylabel=Y-axis 2
]
\addplot[red,fill=red,opacity=1]
table [x=date, y=install, col sep=comma] {data.csv}; \label{plot1}
\addlegendentry{Plot 1}
\end{axis}

\begin{axis}[
  axis y line*=left,
  ytick style={draw=none}
  ymin=0, ymax=50,
  ymajorgrids = true,
  xlabel=,
  ylabel=Y-axis 1,
]
\addlegendimage{/pgfplots/refstyle=plot1}\addlegendentry{Plot 1}
\addplot[blue]
table [x=date, y=rate, col sep=comma] {data.csv};
\addlegendentry{Plot 2}
\end{axis}
\end{tikzpicture}
\end{figure}  
\end{document}

答案1

问题是,插入到第二个图中时pgfplots,图例使用了默认的绘图样式。因此,您应该在图plot1中指定图例的样式。请注意,有一个\addplotplot1错误已报告在这个使用机制中,因此必须在选项中指定ybar和。该错误还要求在行中指定。这些建议来自 Christian 对该错误报告的建议。ybar legend\addplotxshift=0.5em\addlegendimage

我还改变了此代码的两处:

  1. 切换到compat=1.12,最新版本
  2. 删除\usepackage{tikz},因为无论如何pgfplots都会加载tikz

\documentclass{standalone}
    \usepackage{pgfplots} %graaphien tekoon
    \pgfplotsset{compat=1.12}
    \usepackage{filecontents} %tiedostonhallinta? littyy pgfplotsiin
    \usepgfplotslibrary{dateplot}

\begin{filecontents*}{data.csv}
date,rate,install
2012-01-01,48.84,24.4299855
2012-02-01,48.84,173.5773958
2012-03-01,48.84,110.7117086
2012-04-01,22.59,17.4220445
2012-05-01,22.59,36.01732503
2012-06-01,22.59,64.42770708
2012-07-01,22.59,197.822088
2012-08-01,17.22,14.8125984
2012-09-01,17.22,18.78518356
2012-10-01,17.22,44.10470415
2012-11-01,16.11,25.922787
2012-12-01,16.11,21.39109775
2013-01-01,16.11,24.72410623
2013-02-01,16.11,27.6975024
2013-03-01,16.11,33.65679443
2013-04-01,16.11,51.21122378
2013-05-01,16.11,36.79503848
2013-06-01,16.11,67.02365677
2013-07-01,15.54,33.01713989
2013-08-01,15.54,40.61720797
2013-09-01,15.54,38.26365551
2013-10-01,15.54,44.52759475
2013-11-01,15.54,44.36376502
2013-12-01,15.54,42.85926874
\end{filecontents*}

\begin{document}

\begin{tikzpicture}
\pgfplotsset{
    scale only axis,
    date coordinates in=x,
    x tick label style={/pgf/number format/1000 sep=},
    xticklabel={\year},
    date ZERO=2010-01-01,
    xmin=2012-01-01, xmax=2014-01-01,
    width  = 0.9*\textwidth,
}

\begin{axis}[
  axis y line*=right,
  axis x line=none,
  ymin=0, ymax=200,
  ylabel=Y-axis 2
]
\addplot[red,fill=red,opacity=1, ybar=1*\pgflinewidth, bar width=4pt, ybar legend]
table [x=date, y=install, col sep=comma] {data.csv}; \label{plot1}
% \addlegendentry{Plot 1}
\end{axis}

\begin{axis}[
  axis y line*=left,
  ytick style={draw=none}, %poistaa ytickit joten toi grid näyttää paremmalta
  ymin=0, ymax=50,
  ymajorgrids = true,
  xlabel=,
  ylabel=Y-axis 1,
]
\addlegendimage{refstyle=plot1, xshift=0.5em}
\addlegendentry{Plot 1}
\addplot[blue]
table [x=date, y=rate, col sep=comma] {data.csv};
\addlegendentry{Plot 2}
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容