当 xticklabels 旋转时如何修复它?

当 xticklabels 旋转时如何修复它?

xticks 的标签太长,应该像这样

在此处输入图片描述

然而,我制作出来的效果是这样的在此处输入图片描述

应该怎么做才能将第二个单词放在标签上第一个单词的旁边? 这是我制作的代码:

\begin{center}
\definecolor{uququq}{rgb}{0.25098039215686274,0.25098039215686274,0.25098039215686274}
\begin{tikzpicture}[line cap=round,line join=round,>=stealth,x=1.411764705882353cm,y=1.0cm]
\begin{axis}[
x=1.411764705882353cm,y=1.0cm,
axis lines=middle,
ymajorgrids=true,
xmajorgrids=true,
xmin=0.0,
xmax=8.5,
ymin=0.0,
ymax=8.1,
  x label style={at={(axis description cs:0.5,-0.5)},anchor=north},
 y label style={at={(axis description cs:-0.09,.5)},rotate=90,anchor=south},
  xlabel={$\textbf{Cause of Accident}$},
 ylabel={$\textbf{Frequency}$},
major grid style={gray!80},
minor y tick num = 9,
grid=both,
xtick={0.0,1.0,...,8.0},
    xticklabels={,Trauma Injury,Abdominal Pain, Respiratory Distress, Chest Pain, Behavioural Disorder, Loss of Consciousness, Seizure,Poison/Drug Ingestion},
    x tick label style={rotate=90,anchor=east},
ytick={0.0,2.0,...,8.0},
yticklabels={,20,40,60,80},]
\clip(0.,0.) rectangle (8.5,8.1);
\fill[line width=0.pt,color=uququq,fill=uququq,fill opacity=1.0] (0.6,0.) -- (0.6,7.5) -- (1.4,7.5) -- (1.4,0.) -- cycle;
\fill[line width=0.pt,color=uququq,fill=uququq,fill opacity=1.0] (1.6,0.) -- (1.6,5.) -- (2.4,5.) -- (2.4,0.) -- cycle;
\fill[line width=0.pt,color=uququq,fill=uququq,fill opacity=1.0] (2.6,0.) -- (2.6,4.5) -- (3.4,4.5) -- (3.4,0.) -- cycle;
\fill[line width=0.pt,color=uququq,fill=uququq,fill opacity=1.0] (3.6,0.) -- (3.6,3.8) -- (4.4,3.8) -- (4.4,0.) -- cycle;
\fill[line width=0.pt,color=uququq,fill=uququq,fill opacity=1.0] (4.6,0.) -- (4.6,3.2) -- (5.4,3.2) -- (5.4,0.) -- cycle;
\fill[line width=0.pt,color=uququq,fill=uququq,fill opacity=1.0] (5.6,0.) -- (5.6,2.9) -- (6.4,2.9) -- (6.4,0.) -- cycle;
\fill[line width=0.pt,color=uququq,fill=uququq,fill opacity=1.0] (6.6,0.) -- (6.6,2.3) -- (7.4,2.3) -- (7.4,0.) -- cycle;
\fill[line width=0.pt,color=uququq,fill=uququq,fill opacity=1.0] (7.6,0.) -- (7.6,2.) -- (8.4,2.) -- (8.4,0.) -- cycle;
\draw [line width=0.8pt] (1.,1.9)-- (2.,3.2);
\draw [line width=0.8pt] (2.,3.2)-- (3.,4.3);
\draw [line width=0.8pt] (3.,4.3)-- (4.,5.3);
\draw [line width=0.8pt] (4.,5.3)-- (5.,6.15);
\draw [line width=0.8pt] (5.,6.15)-- (6.,6.9);
\draw [line width=0.8pt] (6.,6.9)-- (7.,7.5);
\draw [line width=0.8pt] (7.,7.5)-- (8.,8.);
\begin{scriptsize}
\draw [fill=black] (1.,1.9) circle (1.5pt);
\draw [fill=black] (2.,3.2) circle (1.5pt);
\draw [fill=black] (3.,4.3) circle (1.5pt);
\draw [fill=black] (4.,5.3) circle (1.5pt);
\draw [fill=black] (5.,6.15) circle (1.5pt);
\draw [fill=black] (6.,6.9) circle (1.5pt);
\draw [fill=black] (7.,7.5) circle (1.5pt);
\draw [fill=black] (8.,8.) circle (1.5pt);
\end{scriptsize}
\end{axis}
\begin{axis}[
x=1.411764705882353cm,y=1.0cm,
    axis y line*=right,
    axis x line=none,
    ymin=0, ymax=8.1,,
    xmin=0.0,
xmax=8.5,
ymin=0.0,
ymax=8.1,
ylabel = {$\textbf{Cumulative Percentage}$},
ytick=\empty,
            extra y ticks={0,1.6,...,8},
            extra y tick labels={,20,40,60,80,100},]
  ]
\end{axis}
\end{tikzpicture}
\end{center}

答案1

将代码替换为以下内容:

xticklabels={
    ,Trauma\\ Injury,Abdominal\\ Pain,Respiratory\\ Distress,Chest\\ Pain,Behavioural\\ Disorder,Loss of\\ Consciousness,Seizure,Poison/Drug\\ Ingestion
},
x tick label style={
    align=center, % Aligns text to the center
    text width=2cm, % Adjust the width to your preference
},

旧输出: 在此处输入图片描述 新输出: 在此处输入图片描述\\您可以在 xticklabels 中的所需位置 手动插入换行符 ( )。指定标签的文本宽度有助于 TikZ 正确处理文本换行。

相关内容