![如何移动条形图中的条形?](https://linux22.com/image/436942/%E5%A6%82%E4%BD%95%E7%A7%BB%E5%8A%A8%E6%9D%A1%E5%BD%A2%E5%9B%BE%E4%B8%AD%E7%9A%84%E6%9D%A1%E5%BD%A2%EF%BC%9F.png)
我想将绿色条向右移动,因此它们在蓝色条的右侧相邻。但是绿色和蓝色条具有相同的垂直轴,因此我不知道如何仅移动绿色条(蓝色条已经移动,如第 33 行所示,用 表示%<-----
)。
有什么帮助吗?提前致谢!
图例代码:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepackage{upgreek}
\pgfplotsset{compat=1.16}
\usepackage{tikz}
\usepackage{siunitx}
\begin{document}
\begin{tikzpicture}
\centering
\begin{axis}[
ybar,
symbolic x coords={Carreau,Carreau-Yasuda,Casson,Cross,Gen. power-law,Power-law,Newtonian},
x tick label style={rotate=45},
bar width=0.5cm, bar shift=-0.285cm,
xtick=data,
axis y line*=left,
ylabel={\textit{C\textsubscript{D}}}, ymin=0.0422, ymax=0.0447,
ytick={0.0425, 0.0430, 0.0435, 0.0440, 0.0445},
xlabel={},
width=12cm, height=6cm,
legend cell align={left}
]
\addplot[draw=red,fill=red] coordinates {
(Carreau,0.043242) (Carreau-Yasuda,0.043149) (Casson,0.042620) (Cross,0.043006) (Gen. power-law,0.044280) (Power-law,0.043266) (Newtonian,0.042715)
}; \label{plot_CD}
\end{axis}
\begin{axis}[
ybar,
symbolic x coords={Carreau,Carreau-Yasuda,Casson,Cross,Gen. power-law,Power-law,Newtonian},
xticklabels={ , , , , , , },
x tick label style={rotate=45},
bar width=0.5cm, bar shift=0.25cm, %<-----
xtick=data,
axis y line*=right,
ylabel={\textit{$\Delta$p}, 10$\cdot$\textit{$\uptau$\textsubscript{wall}} [Pa]}, ymin=40, ymax=140,
ytick={50, 70, 90, 110, 130},
width=12cm, height=6cm,
legend cell align={left}
]
\addlegendimage{/pgfplots/refstyle=plot_CD}\addlegendentry{\textit{C\textsubscript{D}}},
\addplot[draw=blue,fill=blue] coordinates {
(Carreau,84.30) (Carreau-Yasuda,80.58) (Casson,56.03) (Cross,74.95) (Gen. power-law,129.74) (Power-law,84.45) (Newtonian,72.1)
}; \label{plot_delta_p} \addlegendentry{\textit{$\Delta$p} [Pa]}
\addplot[draw=green,fill=green] coordinates {
(Carreau,81.4942) (Carreau-Yasuda,79.5077) (Casson,44.9415) (Cross,77.7879) (Gen. power-law,77.8555) (Power-law,76.9145) (Newtonian,65.2255)
}; \label{plot_tau_wall_times_10} \addlegendentry{10$\cdot$\textit{$\uptau$\textsubscript{wall}} [Pa]}
\legend{\textit{C\textsubscript{D}}, \textit{$\Delta$p} [Pa], 10$\cdot$\textit{$\uptau$\textsubscript{wall}} [Pa]}
\end{axis}
\end{tikzpicture}
\end{document}
答案1
我对你的代码做了一些小改动,在每个图中添加了bar shift
,尺寸略大于bar width
,蓝色条位于中心。
还将 x 轴标签移近刻度。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepackage{upgreek}
\pgfplotsset{compat=1.16}
\usepackage{tikz}
\usepackage{siunitx}
\begin{document}
\begin{tikzpicture}
\centering
\begin{axis}[
ybar,
symbolic x coords={Carreau,Carreau-Yasuda,Casson,Cross,Gen. power-law,Power-law,Newtonian},
x tick label style={xshift=-15pt, yshift=5pt, rotate=45}, % changed <<<<<<<<
bar width=0.35cm, % changed <<<<<<<<
xtick=data,
axis y line*=left,
ylabel={\textit{C\textsubscript{D}}}, ymin=0.0422, ymax=0.0447,
ytick={0.0425, 0.0430, 0.0435, 0.0440, 0.0445},
xlabel={},
width=12cm, height=6cm,
legend cell align={left}
]
\addplot[draw=red,fill=red, bar shift=-0.4cm] coordinates { % changed <<<<<<<<
(Carreau,0.043242) (Carreau-Yasuda,0.043149) (Casson,0.042620) (Cross,0.043006) (Gen. power-law,0.044280) (Power-law,0.043266) (Newtonian,0.042715)
}; \label{plot_CD}
\end{axis}
\begin{axis}[
ybar,
symbolic x coords={Carreau,Carreau-Yasuda,Casson,Cross,Gen. power-law,Power-law,Newtonian},
xticklabels={ , , , , , , },
x tick label style={rotate=45},
bar width=0.35cm, % changed <<<<<<<<
xtick=data,
axis y line*=right,
ylabel={\textit{$\Delta$p}, 10$\cdot$\textit{$\uptau$\textsubscript{wall}} [Pa]}, ymin=40, ymax=140,
ytick={50, 70, 90, 110, 130},
width=12cm, height=6cm,
legend cell align={left}
]
\addlegendimage{/pgfplots/refstyle=plot_CD}\addlegendentry{\textit{C\textsubscript{D}}};
\addplot[draw=blue,fill=blue, bar shift=0.0cm,] coordinates { % changed <<<<<<<<
(Carreau,84.30) (Carreau-Yasuda,80.58) (Casson,56.03) (Cross,74.95) (Gen. power-law,129.74) (Power-law,84.45) (Newtonian,72.1)
}; \label{plot_delta_p} \addlegendentry{\textit{$\Delta$p} [Pa]}
\addplot[draw=green,fill=green, bar shift=0.4cm,] coordinates { % changed <<<<<<<<
(Carreau,81.4942) (Carreau-Yasuda,79.5077) (Casson,44.9415) (Cross,77.7879) (Gen. power-law,77.8555) (Power-law,76.9145) (Newtonian,65.2255)
}; \label{plot_tau_wall_times_10} \addlegendentry{10$\cdot$\textit{$\uptau$\textsubscript{wall}} [Pa]}
\end{axis}
\end{tikzpicture}
\end{document}