서수형 x축을 사용하여 막대 차트를 생성하시겠습니까?

서수형 x축을 사용하여 막대 차트를 생성하시겠습니까?

x축의 값이 관련되지 않은 막대 차트를 만들려고 합니다. 내 현재 코드는 다음과 같습니다.

\documentclass{article}
\beging{document}
\begin{figure}
\centering
     \begin{tikzpicture}
        \begin{axis}[ylabel=Similarity,
                    ybar]
            \addplot table[y=Pref, col sep=comma] {5_Results/evalresults.csv};
            \addplot table[y=Rec, col sep=comma] {5_Results/evalresults.csv};

        \end{axis}
    \end{tikzpicture}
    \caption{A bar chart of user and course similarity}
    \label{evalresultsbar}
\end{figure}
\end{document}

그러나 이렇게 하면 이 차트가 생성됩니다. x축을 따라 값을 제거할 뿐만 아니라 막대가 서로 쌓이는 것을 방지하고 싶습니다.

내가 원하는 출력은 Google 시트에서 생성된 다음 차트와 유사합니다. 여기에 이미지 설명을 입력하세요

evalresults.csv의 내용

Pref,Rec,label
0.00,14.29,a
0.00,16.67,a
0.00,16.67,a
0.00,20.00,a
0.00,20.00,a
0.00,25.00,a
0.00,33.33,a
0.00,33.33,a
0.00,33.33,a
0.00,50.00,a
0.00,50.00,a
0.00,50.00,a
0.00,50.00,a
0.00,50.00,a
0.00,60.00,a
0.00,60.00,a
0.00,60.00,a
33.33,25.00,a
33.33,25.00,a
33.33,33.33,a
33.33,33.33,a
33.33,60.00,a
33.33,60.00,a
33.33,66.67,a
33.33,75.00,a
33.33,75.00,a
33.33,75.00,a
33.33,80.00,a
33.33,80.00,a
33.33,100.00,a
66.67,75.00,a
66.67,80.00,a
66.67,100.00,a
66.67,100.00,a
66.67,100.00,a
66.67,100.00,a
100.00,100.00,a

답변1

어떻게든 x 좌표를 지정해야 하므로 아래와 같이 x expr=\coordindex옵션을 추가하세요.table

코드에 몇 가지 주석을 추가했습니다.

여기에 이미지 설명을 입력하세요

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\usepackage{pgfplotstable} % for pgfplotstableread, but you can use the filename in the \addplot as you did instead
\pgfplotstableread[col sep=comma]{
Pref,Rec,label
0.00,14.29,a
0.00,16.67,a
0.00,16.67,a
0.00,20.00,a
0.00,20.00,a
0.00,25.00,a
0.00,33.33,a
0.00,33.33,a
0.00,33.33,a
0.00,50.00,a
0.00,50.00,a
0.00,50.00,a
0.00,50.00,a
0.00,50.00,a
0.00,60.00,a
0.00,60.00,a
0.00,60.00,a
33.33,25.00,a
33.33,25.00,a
33.33,33.33,a
33.33,33.33,a
33.33,60.00,a
33.33,60.00,a
33.33,66.67,a
33.33,75.00,a
33.33,75.00,a
33.33,75.00,a
33.33,80.00,a
33.33,80.00,a
33.33,100.00,a
66.67,75.00,a
66.67,80.00,a
66.67,100.00,a
66.67,100.00,a
66.67,100.00,a
66.67,100.00,a
100.00,100.00,a
}\data
\begin{document}    
\begin{tikzpicture}
\begin{axis}[
  ylabel=Similarity,
  ybar,
  % reduce bar width
  bar width=1pt,
  % to remove whitespace below bars
  ymin=0,
  % only want the x-axis on the bottom
  axis x line=bottom,
  % add some horizontal space between bars and axis limits
  enlarge x limits=0.05,
  % don't draw the ticks
  tick style={draw=none},
  % remove x ticks
  xtick=\empty,
  % enable grid
  grid=major,
  % don't draw the vertical lines for the y-axes
  every outer y axis line/.style={draw=none},
  %position legend outside the axis, top right
  legend pos=outer north east,
  % don't draw box around legend
  legend style={draw=none}
]
    \addplot [fill=red!50,draw=none] table[x expr=\coordindex,y=Pref, col sep=comma] {\data};
    \addplot [fill=blue!30,draw=none] table[x expr=\coordindex,y=Rec, col sep=comma] {\data};

   \addlegendentry{Pref}
   \addlegendentry{Rec}

\end{axis}
\end{tikzpicture}
\end{document}

관련 정보