我需要将我的x
-labels设置pgfplots
为矢量图形。如何防止图形x
-labels 拉伸?
下面是一个可以重现我的问题的 MWE。绘制的矩形和圆形在 -x
轴以下被拉伸和扭曲(圆形变成了椭圆)。
我正在寻找一种方法,使图形标签的纵横比不受轴尺寸的影响,类似于数字作为轴标签打印的方式,即xticklabels={0, 90, 180, 270, 360}
。无论轴的尺寸是多少,如果xticklabels
只是数字,结果都会非常漂亮,并且具有正确且合适的纵横比。我希望当我使用图形而不是纯数字/文本时能够得到同样的东西xticklabels
。
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usepackage{tkz-euclide}
\usetikzlibrary{patterns}
\begin{filecontents*}{data.csv}
a,b,c
0,4,1
90,3,2
180,5,3
270,1,4
360,3,5
\end{filecontents*}
\newcommand{\mL}{5}
\newcommand{\mW}{30}
\pgfplotsset{compat=newest,height = 10cm, width = 20cm}
\pgfplotsset{minor grid style={dotted}}
\newcommand{\myGfxLabels}[1]
{
\begin{tikzpicture}
\tkzDefPoint( {\mL},-{\mW}){B};
\foreach \xDelta in {0}
{
\draw[->,line width=1pt,rotate={0},red] ({\mL+\xDelta}, -{1.5*\mW}) -- ({\mL+\xDelta},-{\mW});
}
\draw[fill=red!50!yellow,pattern=north east lines,shift={(0 cm,0 cm)},rotate around={-#1:(B)},xscale=1,yscale=1,] ({\mL}, {\mW}) -- ({\mL},-{\mW}) -- (-{\mL},-{\mW}) -- (-{\mL},{\mW}) -- ({\mL}, {\mW});
\draw[] (B) circle ({\mL});
\end{tikzpicture}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[
xlabel={$\phi$},
ylabel={$dL_{\rm cavity}$},
xtick={0, 90, 180, 270, 360},
xticklabels={\myGfxLabels{0}, \myGfxLabels{90}, \myGfxLabels{180}, \myGfxLabels{270}, \myGfxLabels{360}},
smooth,
grid=both,
minor tick num=2,
title={Title},
]
\addplot+ table [x=a, y=b,col sep=comma] {data.csv};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
将图形和图表结合起来的最佳方法是将坐标保存在轴环境内,然后在外面绘制图形。我调整了\mL
、\mW
和yshift
以适应。
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
%\usepackage{tkz-euclide}
\usetikzlibrary{patterns}
\usepackage{filecontents}
\begin{filecontents*}{data.csv}
a,b,c
0,4,1
90,3,2
180,5,3
270,1,4
360,3,5
\end{filecontents*}
\newcommand{\mL}{.1}
\newcommand{\mW}{.6}
\newlength{\xshift}
\pgfplotsset{compat=newest,height = 10cm, width = 20cm}
\pgfplotsset{minor grid style={dotted}}
\newcommand{\myGfxLabels}[2]% #1=x, #2=anchor
{
\pgfextractx{\xshift}{\pgfpointanchor{#2}{center}}%
\begin{scope}[xshift=\xshift,yshift={-\mW cm}]
\coordinate (B) at ( {\mL},-{\mW});
\foreach \xDelta in {0}
{
\draw[->,line width=1pt,rotate={0},red] ({\mL+\xDelta}, -{1.5*\mW}) -- ({\mL+\xDelta},-{\mW});
}
\draw[fill=red!50!yellow,pattern=north east lines,shift={(0 cm,0 cm)},rotate around={-#1:(B)}] ({\mL}, {\mW}) -- ({\mL},-{\mW}) -- (-{\mL},-{\mW}) -- (-{\mL},{\mW}) -- ({\mL}, {\mW});
\draw[] (B) circle ({\mL});
\end{scope}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[
xlabel={$\phi$},
ylabel={$dL_{\rm cavity}$},
xtick={0, 90, 180, 270, 360},
xticklabels=\empty,
smooth,
grid=both,
minor tick num=2,
title={Title},
]
\addplot+ table [x=a, y=b,col sep=comma] {data.csv};
\coordinate (tick0) at (0,0);
\coordinate (tick90) at (90,0);
\coordinate (tick180) at (180,0);
\coordinate (tick270) at (270,0);
\coordinate (tick360) at (360,0);
\end{axis}
\myGfxLabels{0}{tick0}
\myGfxLabels{90}{tick90}
\myGfxLabels{180}{tick180}
\myGfxLabels{270}{tick270}
\myGfxLabels{360}{tick360}
\end{tikzpicture}
\end{document}