如何制作条形图,其中框位于给定线的两侧?

如何制作条形图,其中框位于给定线的两侧?

我想制作一个图表来显示敏感度,以及不同参数(AG)的值变化将如何影响总成本。首先,我制作了一个图表来显示改变在 x 轴上,但现在我想制作一个图表,在 x 轴上显示总成本,这似乎有点困难。

有谁知道如何做到这一点?

这是我制作的第一张图表的图片(它们都在下面的代码中)。它显示了生产成本的变化。如您所见,方框位于垂直线的每一侧。

第一张图表

这是我尝试制作的现在想要的图表的图片。在这里,我不会显示生产成本的变化,而是显示每个参数的实际生产成本。垂直线两侧的方框并不相互关联,就像第一张图表一样。例如,C 处的方框应该从 74 到 80(而不是一个方框从 67 到 74,另一个方框从 77 到 80)。我该如何解决这个问题?

第二张图表

我希望我的问题已经清楚了。可能这是一个快速解决方案,但我已经挣扎了很长时间,所以我看不到解决方案。

\documentclass{article}
\usepackage{pgfplots}\pgfplotsset{compat=1.10}
\usepackage{pgfplotstable}

\begin{document}
\begin{figure}
\begin{footnotesize}
\pgfplotstableread[col sep=comma, header=true]{
unit,min,max
A,0,5.27
B,0,1.07
C,-0.55,0.54
D,-0.58,0.48
E,0,0.14
F,-0.1,0.09
G,-0.04,0.03
}\loadedtable
{
\begin{tikzpicture}
\begin{axis}[
    name=popaxis,
    scale only axis,
    xbar,
    xmin=0,
    xmax=6,
    %xmajorgrids=true,
    %xbar=2pt,
    width=0.4*\textwidth, height= 0.2*\textheight,
   y dir = reverse,
   nodes near coords = {\pgfmathprintnumber\pgfplotspointmeta},
   every node near coord/.append style={/pgf/number format/fixed, 
   font=\footnotesize, 
   color=black},
   xticklabel= {\pgfmathprintnumber\tick},
    y tick label style={text width=3cm,align=center},
    axis x line=left,
    axis y line=none,
%
    enlarge x limits = {value=0.15,upper},
    axis line style={-},
    clip=false
]
\addplot[gray,fill=lightgray!50] table[y expr =\coordindex, x expr={\thisrow{max}}] \loadedtable;
\pgfplotstablegetrowsof{\loadedtable}
\pgfmathsetmacro{\lastrow}{\pgfplotsretval-1}  
\end{axis}

\begin{axis}[ 
at={(popaxis.north west)},anchor=north east, xshift=0.325cm,
scale only axis,
    xbar = 0,
    xmin = -3,
    xmax = 0,
    %xmajorgrids=true,
    width=0.2*\textwidth,
    height= 0.2*\textheight,
    %x dir=reverse,
    y dir=reverse,
    nodes near coords = {\pgfmathprintnumber\pgfplotspointmeta},
    every node near coord/.append style={/pgf/number format/fixed, rotate = 0, anchor = east, font=\footnotesize,
    color=black},
    xticklabel= {\pgfmathprintnumber\tick},
    axis x line=left,
    axis y line*=left,
%
    ytick = data,
    yticklabels from table = {\loadedtable}{unit},
    ytick align=center,
    ytick pos=left,
    enlarge x limits = {value=0.15,upper},
    axis line style={-}
]
\addplot[gray,fill=lightgray!50] table[y expr =\coordindex, x expr={\thisrow{min}}] \loadedtable;
\end{axis}
\draw (0,0) -- (0,4);
\end{tikzpicture}
}

\end{footnotesize}
\end{figure}

\begin{center}
\begin{figure}
\pgfplotstableread[col sep=comma, header=true]{
unit,min,max
A,77,86
B,77,82
C,74,80
D,75,79
E,77,78
F,76,78
G,76,78
}\loadedtable

\resizebox{10cm}{!}{%
\begin{tikzpicture}
\begin{axis}[
    name=popaxis,
    scale only axis,
    xbar,
    xmin=77,
    xmax=87,
    %xmajorgrids=true,
    %xbar=2pt,
    %xlabel={\%},
    width=10cm, height=8cm,
   y dir = reverse,
   nodes near coords = {\pgfmathprintnumber\pgfplotspointmeta},
   every node near coord/.append style={/pgf/number format/fixed, 
   font=\small, 
   color=black},
   xticklabel= {\pgfmathprintnumber\tick},
    y tick label style={text width=3cm,align=center},
    axis x line=left,
    axis y line=none,
    enlarge x limits = {value=0,upper},
    axis line style={-},
    clip=false
]
\addplot[gray,fill=lightgray!50] table[y expr =\coordindex, x expr={\thisrow{max}}] \loadedtable;
\pgfplotstablegetrowsof{\loadedtable}
\pgfmathsetmacro{\lastrow}{\pgfplotsretval-1}  

\end{axis}
\begin{axis}[ 
at={(popaxis.north west)},anchor=north east, xshift=0cm,
scale only axis,
    xbar,
    allow reversal of rel axis cs=true,
    y dir=reverse,
 %  x dir=reverse,
    %axis y line*=right, 
    xmin=67,
    xmax=77,
    %xmajorgrids=true,
    width=10cm,
    height=8cm,
    nodes near coords = {\pgfmathprintnumber\pgfplotspointmeta},
    every node near coord/.append style={/pgf/number format/fixed, rotate = 0, anchor = east, font=\footnotesize,
    color=black},
    xticklabel= {\pgfmathprintnumber\tick},
    axis x line=left,
    axis y line*=right,
    ytick = data,
    yticklabels from table = {\loadedtable}{unit},
    ytick align=center,
    ytick pos=left,
    enlarge x limits = {value=0,upper},
    axis line style={-}
]
\addplot[gray,fill=lightgray!50] table[y expr =\coordindex, x expr={\thisrow{min}}] \loadedtable;
\end{axis}
\draw (0,0) -- (0,8);
\end{tikzpicture}
}
\end{figure}
\end{center}

\end{document}

答案1

像这样吗?

\documentclass{article}
\usepackage{pgfplots}\pgfplotsset{compat=1.10}
\usepackage{pgfplotstable}

\begin{document}
\begin{figure}
\begin{footnotesize}
\pgfplotstableread[col sep=comma, header=true]{
unit,min,max
A,0,5.27
B,0,1.07
C,-0.55,0.54
D,-0.58,0.48
E,0,0.14
F,-0.1,0.09
G,-0.04,0.03
}\loadedtable
{
\begin{tikzpicture}
\begin{axis}[
    name=popaxis,
    scale only axis,
    xbar,
    xmin=0,
    xmax=6,
    %xmajorgrids=true,
    %xbar=2pt,
    width=0.4*\textwidth, height= 0.2*\textheight,
   y dir = reverse,
   nodes near coords = {\pgfmathprintnumber\pgfplotspointmeta},
   every node near coord/.append style={/pgf/number format/fixed, 
   font=\footnotesize, 
   color=black},
   xticklabel= {\pgfmathprintnumber\tick},
    y tick label style={text width=3cm,align=center},
    axis x line=left,
    axis y line=none,
%
    enlarge x limits = {value=0.15,upper},
    axis line style={-},
    clip=false
]
\addplot[gray,fill=lightgray!50] table[y expr =\coordindex, x expr={\thisrow{max}}] \loadedtable;
\pgfplotstablegetrowsof{\loadedtable}
\pgfmathsetmacro{\lastrow}{\pgfplotsretval-1}  
\end{axis}

\begin{axis}[ 
at={(popaxis.north west)},anchor=north east, xshift=0.325cm,
scale only axis,
    xbar = 0,
    xmin = -3,
    xmax = 0,
    %xmajorgrids=true,
    width=0.2*\textwidth,
    height= 0.2*\textheight,
    %x dir=reverse,
    y dir=reverse,
    nodes near coords = {\pgfmathprintnumber\pgfplotspointmeta},
    every node near coord/.append style={/pgf/number format/fixed, rotate = 0, anchor = east, font=\footnotesize,
    color=black},
    xticklabel= {\pgfmathprintnumber\tick},
    axis x line=left,
    axis y line*=left,
%
    ytick = data,
    yticklabels from table = {\loadedtable}{unit},
    ytick align=center,
    ytick pos=left,
    enlarge x limits = {value=0.15,upper},
    axis line style={-}
]
\addplot[gray,fill=lightgray!50] table[y expr =\coordindex, x expr={\thisrow{min}}] \loadedtable;
\end{axis}
\draw (0,0) -- (0,4);
\end{tikzpicture}
}

\end{footnotesize}
\end{figure}

\begin{center}
\begin{figure}
\pgfplotstableread[col sep=comma, header=true]{
unit,min,max
A,77,86
B,77,82
C,74,80
D,75,79
E,77,78
F,76,78
G,76,78
}\loadedtable

\resizebox{10cm}{!}{%
\begin{tikzpicture}
\begin{axis}[
    name=popaxis,
    scale only axis,
    xbar,
    xmin=77,
    xmax=87,
    %xmajorgrids=true,
    %xbar=2pt,
    %xlabel={\%},
    width=10cm, height=8cm,
   y dir = reverse,
   nodes near coords = {\pgfmathprintnumber\pgfplotspointmeta},
   every node near coord/.append style={/pgf/number format/fixed, 
   font=\small, 
   color=black},
   xticklabel= {\pgfmathprintnumber\tick},
    y tick label style={text width=3cm,align=center},
    axis x line=left,
    axis y line=none,
    enlarge x limits = {value=0,upper},
    axis line style={-},
    clip=false
]
\addplot[gray,fill=lightgray!50] table[y expr =\coordindex, x expr={\thisrow{max}}] \loadedtable;
\pgfplotstablegetrowsof{\loadedtable}
\pgfmathsetmacro{\lastrow}{\pgfplotsretval-1}  

\end{axis}
\begin{axis}[ 
at={(popaxis.north west)},anchor=north east, xshift=0.6pt,
scale only axis,
    xbar,
    allow reversal of rel axis cs=true,
    y dir=reverse,
 %  x dir=reverse,
    %axis y line*=right, 
     xmin=-84,
     xmax=-77,
    %xmajorgrids=true,
    width=10cm,
    height=8cm,
    point meta=x,
    nodes near coords={\pgfmathfloatparsenumber{\pgfplotspointmeta}
    \pgfmathfloattomacro{\pgfmathresult}{\F}{\M}{\E}
    \pgfmathparse{77+77-\M*pow(10,\E)}
    \pgfmathprintnumber\pgfmathresult},
    every node near coord/.append style={/pgf/number format/fixed, rotate = 0, anchor = east, font=\footnotesize,
    color=black},
    %xticklabel= {\pgfmathprintnumber\tick},
    axis x line=left,
    axis y line*=right,
    ytick = data,
    yticklabels from table = {\loadedtable}{unit},
    ytick align=center,
    ytick pos=left,
    enlarge x limits = {value=0,upper},
    axis line style={-},
    xtick={-84,...,-78},
    xticklabel=\pgfmathparse{77+77+\tick}\pgfmathprintnumber{\pgfmathresult}
]
\addplot[gray,fill=lightgray!50] table[y expr =\coordindex, x expr={-2*77+\thisrow{min}}] \loadedtable;
\end{axis}
\draw (0,0) -- (0,8);
\end{tikzpicture}
}
\end{figure}
\end{center}
\end{document}

在此处输入图片描述

相关内容