![Tikz/pgfplots 눈금 레이블 줄 바꿈 사이의 거리 줄이기](https://rvso.com/image/392322/Tikz%2Fpgfplots%20%EB%88%88%EA%B8%88%20%EB%A0%88%EC%9D%B4%EB%B8%94%20%EC%A4%84%20%EB%B0%94%EA%BF%88%20%EC%82%AC%EC%9D%B4%EC%9D%98%20%EA%B1%B0%EB%A6%AC%20%EC%A4%84%EC%9D%B4%EA%B8%B0.png)
나는 지금 내가 겪고 있는 문제로 인해 매우 절망적입니다. y축에 다양한 범주가 있고 x축에 항목이 있는 롤리팝 플롯을 만들려고 합니다. 아래 코드에 따르면. 참고로 제가 사용하는https://github.com/nschloe/matplotlib2tikzmatplotlib 플롯을 tikz/pgfplot으로 변환합니다. 이제 Y 눈금 레이블의 일부 범주에 대해 텍스트가 너무 길어서 끊어져야 한다는 문제가 있습니다. 그렇지 않으면 플롯이 문서 밖으로 도달하기 때문입니다(플롯의 너비는 플롯 자체만 변경하지만 텍스트 크기). 여기서는 줄을 나누기 위해 y 눈금 레이블 스타일 "텍스트 너비"를 사용합니다. 이 다이어그램 중 약 16개가 있으므로 높이가 약 5cm가 되도록 하고 싶습니다. 더 높으면 문서에서 너무 많은 공간을 차지하기 때문입니다. 보시다시피, 줄을 래핑할 때 새 줄 사이의 거리는 눈금 레이블과 위쪽 및 아래쪽 눈금 레이블 사이의 거리보다 큽니다.
이제 내 질문입니다. 눈금 레이블의 두 줄 사이의 거리를 줄일 수 있는 방법이 있습니까?
\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\definecolor{color0}{rgb}{0.529411764705882,0.807843137254902,0.92156862745098}
\definecolor{color1}{rgb}{0.12156862745098,0.466666666666667,0.705882352941177}
\begin{axis}[
height=5cm,
tick align=outside,
tick pos=left,
width=\textwidth,
x grid style={white!69.01960784313725!black},
xmajorgrids,
xmin=-0.4, xmax=8.4,
y grid style={white!69.01960784313725!black},
y tick label style ={anchor=east, text width={5cm}},
ymin=-0.45, ymax=5.45,
ytick={0,1,2,3,4,5},
yticklabels={This is some long text,This is some quite long text,This is some very long text that might break,This is some long text,This is some long text,Short text}
]
\path [draw=color0, semithick] (axis cs:0,0)
--(axis cs:8,0);
\path [draw=color0, semithick] (axis cs:0,1)
--(axis cs:6,1);
\path [draw=color0, semithick] (axis cs:0,2)
--(axis cs:3,2);
\path [draw=color0, semithick] (axis cs:0,3)
--(axis cs:3,3);
\path [draw=color0, semithick] (axis cs:0,4)
--(axis cs:3,4);
\path [draw=color0, semithick] (axis cs:0,5)
--(axis cs:3,5);
\addplot [semithick, color1, mark=*, mark size=3, mark options={solid}, only marks, forget plot]
table [row sep=\\]{%
8 0 \\
6 1 \\
3 2 \\
3 3 \\
3 4 \\
3 5 \\
};
\end{axis}
\end{tikzpicture}
\end{document}
답변1
순수한티케이Z에서는 코딩이 조금 더 어렵지만 훨씬 더 많은 것을 사용자 정의할 수 있습니다.
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[y=0.8cm] % <-- Change this to change spacing
\draw (-0.5,0) rectangle (8.5,6);
\foreach \i in {0,...,8} {
\draw[white!70!black] (\i,0)--(\i,6);
\draw (\i,0)--(\i,-.1) node[below] {\i};
}
\begin{scope}[every node/.style={left,text width=5cm,align=left}]
\draw (-.5,0.5)--(-.6,0.5) node {This is some long text};
\draw (-.5,1.5)--(-.6,1.5) node {This is some quite long text};
\draw (-.5,2.5)--(-.6,2.5) node {This is some very long text that that might break};
\draw (-.5,3.5)--(-.6,3.5) node {This is some long text};
\draw (-.5,4.5)--(-.6,4.5) node {This is some long text};
\draw (-.5,5.5)--(-.6,5.5) node {Short text};
\end{scope}
\foreach \i/\j in {0/8,1/6,2/3,3/3,4/3,5/3} {
\draw[blue!50!cyan] (0,\i+0.5)--(\j,\i+0.5);
\node[circle,fill=blue!50!cyan] at (\j,\i+0.5) {};
}
\end{tikzpicture}
\end{document}
문서 클래스를 사용하려면 article
다음 코드를 참조하세요.
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[y=0.8cm] % <-- Change this to change spacing
\draw (-0.5,0) rectangle (8.5,6);
\foreach \i in {0,...,8} {
\draw[white!70!black] (\i,0)--(\i,6);
\draw (\i,0)--(\i,-.1) node[below] {\i};
}
\begin{scope}[every node/.style={left,text width=5cm,align=left}]
\draw (-.5,0.5)--(-.6,0.5) node {This is some long text};
\draw (-.5,1.5)--(-.6,1.5) node {This is some quite long text};
\draw (-.5,2.5)--(-.6,2.5) node {This is some very long text that that might break};
\draw (-.5,3.5)--(-.6,3.5) node {This is some long text};
\draw (-.5,4.5)--(-.6,4.5) node {This is some long text};
\draw (-.5,5.5)--(-.6,5.5) node {Short text};
\end{scope}
\foreach \i/\j in {0/8,1/6,2/3,3/3,4/3,5/3} {
\draw[blue!50!cyan] (0,\i+0.5)--(\j,\i+0.5);
\node[circle,fill=blue!50!cyan] at (\j,\i+0.5) {};
}
\end{tikzpicture}
\end{document}
답변2
와 함께 pgfplots
:
\documentclass[a4paper]{article}% recent version of article use utf8 encoding
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepackage{pgfplotstable}
\pgfplotstableread[col sep=comma]{
Y, V
This is some long text, 8
This is some quite long text, 6
This is some very long text that might break, 3
This is some long text, 3
This is some long text, 3
Short text, 3
}\mydata
\begin{document}
\noindent%
\begin{tikzpicture}
\begin{axis}[
height=5cm, width=0.7\textwidth-2*\pgfkeysvalueof{/pgf/inner xsep},
scale only axis, % <-------------------
grid=major,
every axis plot post/.append style={very thick},
enlarge x limits=0.025,
enlarge y limits=0.05,
xmin=0, xmax=8,
xtick={0,...,8},
ytick={0,...,5},
yticklabels from table={\mydata}{Y}, % <-------------------
table/y expr = \coordindex, % <-------------------
y tick label style={text width=0.3\textwidth, % <---
font=\footnotesize\linespread{0.9}\selectfont, % <---
align=right, anchor=east}, % display label sideways
mark options={solid}, only marks, forget plot
]
\addplot +[xcomb] table [x=V] {\mydata};
\end{axis}
\end{tikzpicture}
\end{document}
(빨간색 선은 텍스트 테두리를 나타냅니다)