多色和多條形分組圖表

多色和多條形分組圖表

我有一個 Python 腳本,可以自動產生繪製長條圖的 LaTeX 程式碼。產生的程式碼範例如下所示:

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.8}
\usepackage{pgfplotstable}
\renewcommand*{\familydefault}{\sfdefault}
\usepackage{sfmath}
\begin{document}
\begin{tikzpicture}
  \centering
  \begin{axis}[
        ybar, axis on top,
        title={Performance charts},
        height=8cm, width=15.5cm,
        bar width=1.5cm,
        ymajorgrids, tick align=inside,
        major grid style={draw=white},
        enlarge y limits={value=.1,upper},
        ymin=0, ymax=0.01,
        axis x line*=bottom,
        axis y line*=right,
        y axis line style={opacity=0},
        tickwidth=0pt,
        enlarge x limits=true,
        legend style={
            at={(0.5,-0.2)},
            anchor=north,
            legend columns=-1,
            /tikz/every even column/.append style={column sep=0.5cm}
        },
        ylabel={Time (seconds)},
        symbolic x coords={
           10,
           20,
       },
       xtick=data,
       nodes near coords={
        \pgfmathprintnumber[precision=3]{\pgfplotspointmeta}
       }
    ]
    \addplot+[ybar, fill=blue!50] plot coordinates {
      (10, 0.001223850250244141)
      (20, 0.001497483253479004)
    };
    \addplot+[ybar, fill=blue!25] plot coordinates {
      (10, 0.00045402050018310557)
      (20, 0.001987481117248536)
    };
    \addplot+[ybar, fill=red!50] plot coordinates {
      (10, 0.0008006999999999999)
      (20, 0.0010588)
    };
    \addplot+[ybar, fill=red!25] plot coordinates {
      (10, 0.0002661999999999997)
      (20, 0.0012075)
    };
    \legend{Real Time (Loading), Real-Time (Querying), CPU Time (Loading), CPU Time (Querying)}
  \end{axis}
\end{tikzpicture}
\end{document}

它產生了這個圖表:目前情節

但是,我希望兩個實時欄自行堆疊。與CPU時間相同。這樣每個 x 座標就有兩個條形。產生此 Latex 程式碼的 Python 程式碼是:

def generate_latex_files(data, env_name, output_dir: Path) -> None:
    for key, values in data.items():
        if key[0] == env_name:
            # Sort values by graph size
            values.sort(key=lambda x: x[0])

            # Calculate maximum value for ymax
            max_value = max(sum(val['loading'] + val['querying']) for _, val in values) * 1.1

            file_name = f'{key[1]}_{key[2]}.tex'
            full_file_name = output_dir / env_name / file_name
            full_file_name.parent.mkdir(exist_ok=True, parents=True)
            with open(full_file_name, 'w') as f:
                f.write('\\documentclass[border=10pt]{standalone}\n')
                f.write('\\usepackage{pgfplots}\n')
                f.write('\\pgfplotsset{width=7cm,compat=1.8}\n')
                f.write('\\usepackage{pgfplotstable}\n')
                f.write('\\renewcommand*{\\familydefault}{\\sfdefault}\n')
                f.write('\\usepackage{sfmath}\n')
                f.write('\\begin{document}\n')
                f.write('\\begin{tikzpicture}\n')
                f.write('  \\centering\n')
                f.write('  \\begin{axis}[\n')
                f.write('        ybar, axis on top,\n')
                f.write(f'        title={{Performance charts}},\n')
                f.write('        height=8cm, width=15.5cm,\n')
                f.write('        bar width=1.5cm,\n')
                f.write('        ymajorgrids, tick align=inside,\n')
                f.write('        major grid style={draw=white},\n')
                f.write('        enlarge y limits={value=.1,upper},\n')
                f.write(f'        ymin=0, ymax={max_value:.2f},\n')
                f.write('        axis x line*=bottom,\n')
                f.write('        axis y line*=right,\n')
                f.write('        y axis line style={opacity=0},\n')
                f.write('        tickwidth=0pt,\n')
                f.write('        enlarge x limits=true,\n')
                f.write('        legend style={\n')
                f.write('            at={(0.5,-0.2)},\n')
                f.write('            anchor=north,\n')
                f.write('            legend columns=-1,\n')
                f.write('            /tikz/every even column/.append style={column sep=0.5cm}\n')
                f.write('        },\n')
                f.write('        ylabel={Time (seconds)},\n')
                f.write('        symbolic x coords={\n')
                for value in values:
                    f.write(f'           {value[0]},\n')
                f.write('       },\n')
                f.write('       xtick=data,\n')
                f.write('       nodes near coords={\n')
                f.write('        \\pgfmathprintnumber[precision=3]{\\pgfplotspointmeta}\n')
                f.write('       }\n')
                f.write('    ]\n')

                # Real time plots
                f.write('    \\addplot+[ybar, fill=blue!50] plot coordinates {\n')
                for value in values:
                    f.write(f'      ({value[0]}, {value[1]["loading"][0]})\n')
                f.write('    };\n')
                f.write('    \\addplot+[ybar, fill=blue!25] plot coordinates {\n')
                for value in values:
                    f.write(f'      ({value[0]}, {value[1]["querying"][0]})\n')
                f.write('    };\n')

                # CPU time plots
                f.write('    \\addplot+[ybar, fill=red!50] plot coordinates {\n')
                for value in values:
                    f.write(f'      ({value[0]}, {value[1]["loading"][1]})\n')
                f.write('    };\n')
                f.write('    \\addplot+[ybar, fill=red!25] plot coordinates {\n')
                for value in values:
                    f.write(f'      ({value[0]}, {value[1]["querying"][1]})\n')
                f.write('    };\n')

                f.write('    \\legend{Real-Time (Loading), Real-Time (Querying), CPU Time (Loading), CPU Time (Querying)}\n')
                f.write('  \\end{axis}\n')
                f.write('\\end{tikzpicture}\n')
                f.write('\\end{document}\n')

正在處理的資料具有以下結構:

{
   (<env_name>, <graph_type>, <mode>): [(<graph_size>, {'loading': (<real_time>, <cpu_time>), 'querying': (<real_time>, <cpu_time>)}),...]
}

有什麼方法可以達成這個目標嗎?我一直只能用一根棒來堆疊。

更新:我希望實時數據被堆疊(在底部加載,在頂部查詢)。這同樣適用於 CPU 時間,如此草圖所示。

所需情節的草圖

答案1

將兩個堆疊的條形圖並排放置似乎需要一些努力。參見例如傑克的這個解決方案或者來自湯姆·邦巴迪爾

因此,除非您想花費精力和時間進行調試,否則我建議簡單地從同一資料來源繪製兩個圖表。對於有興趣的人來說,這個概念的改變遵循了發明原則的模式——至少少做一點,複製一份。

一些評論。

資料檔案

我假設這樣的資料結構:

 \begin{filecontents}{data2.dat}
 time  lrt lct  qrt qct
 10     5   4   3   3
 20     7   5   4   3
 30     4   7   5   2
 \end{filecontents}

這裡的數據僅包含在 .tex 文件中,而您可以data3.dat在目錄中只包含 etc 並加載該文件,請參見下文。

我假設前兩列屬於在一起,就像最後兩列一樣。如果這是錯誤的,只需更改y=下面的分配。

\addplot-s

逐列繪製,例如這裡第二個名為lrt。將檔案名稱替換為您的檔案名稱。告訴程序,資料包含一個標頭。

    \addplot table[header=true,x=time,y=lrt]{data2.dat};% i.e. your data file

僅用於演示,如果您的標題包含逗號,請將所有內容都放在{ }.需要這兩個 bar-statement。貼上有用的標籤。圖例條目只是虛擬的:使用更好的名稱。

   \begin{axis}[
        title={Real-time data, Load},   
        ybar stacked,
        stack plots=y,
        xmin=0, xmax=50,
        xlabel=time (s),
        ylabel=percent,
        legend entries={lrt, lct},% replace by better names
    ]

未來的改進建議

  • 將您的資料檔案名稱移至 a 中\newcommand,替換 s 中\addplot,僅在一個位置變更它
  • 更改兩張圖的配色方案(請參閱手冊)
  • 移動圖例,例如到外面(參見手冊)
  • 根據您的需求調整圖表的寬度和高度
  • article如果您適合的話,將他們放入一個班級或類似班級;獨立版僅可建立 2 個影像(請參閱其手冊)
  • 在 ctan 上尋找手冊,例如透過搜尋引擎中的搜尋字詞ctan pgfplots
  • 確保您的時間間隔不會改變,以避免錯誤的資料視覺化

結果

\documentclass[10pt,border=3mm,tikz]{standalone}
\usepackage{pgfplots}

\begin{document}
 % ~~~ pretending you have said file in your directory
 %     assuming lrt= load real timeetc.
 %     assuming, this is your data structure

 \begin{filecontents}{data2.dat}
 time  lrt lct  qrt qct
 10     5   4   3   3
 20     7   5   4   3
 30     4   7   5   2
 \end{filecontents}

 % ~~~ Concept: Draw two diagrams
 \begin{tikzpicture}    % LOAD
   \begin{axis}[
        title={Real-time data, Load},   
        ybar stacked,
        stack plots=y,
        xmin=0, xmax=50,
        xlabel=time (s),
        ylabel=percent,
        legend entries={lrt, lct},% replace by better names
    ]
    \addplot table[header=true,x=time,y=lrt]{data2.dat};% i.e. your data file
    \addplot table[header=true,x=time,y=lct]{data2.dat};    
   \end{axis}
 \end{tikzpicture}
 
 % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 \begin{tikzpicture}    % CPU
   \begin{axis}[
        title={Real-time data, CPU},    
        ybar stacked,
        stack plots=y,
        xmin=0, xmax=50,
        xlabel=time (s),
        ylabel=percent,
        legend entries={qrt,qct},
    ]
    \addplot table[header=true,x=time,y=qrt]{data2.dat};
    \addplot table[header=true,x=time,y=qct]{data2.dat};
    
   \end{axis}
 \end{tikzpicture}

\end{document}

答案2

MS-SPO 答案向我指出傑克的解決方案用於產生此乳膠代碼:

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\makeatletter
\newcommand\resetstackedplots{
   \pgfplots@stacked@isfirstplottrue
   \addplot [forget plot,draw=none] coordinates{(10,0) (20,0)};
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
   ybar stacked,
   title={Performance charts},
   height=0.019\textheight, width=1.5\textwidth,
   bar width=0.8cm,
   ymajorgrids, tick align=inside,
   major grid style={draw=gray!20},
   xtick=data,
   ymin=0,
   axis x line*=bottom,
   axis y line*=left,
   enlarge x limits=0.4,
   legend entries={
       Real Time (Loading),
       Real Time (Querying),
       CPU Time (Loading),
       CPU Time (Querying),
   },
   legend style={
       at={(0.5,-0.2)},
       anchor=north,
       legend columns=-1,
   },
   ylabel={Time (seconds)},
   xlabel={Graph Size},
   symbolic x coords={
           10,
           20,
       },
]
\addplot +[bar shift=-.5cm] coordinates {
    (10, 0.001223850250244141)
    (20, 0.001497483253479004)
};
\addplot +[bar shift=-.5cm] coordinates {
    (10, 0.00045402050018310557)
    (20, 0.001987481117248536)
};
\resetstackedplots
\addplot +[bar shift=.5cm] coordinates {
    (10, 0.0008006999999999999)
    (20, 0.0010588)
};
\addplot +[bar shift=.5cm] coordinates {
    (10, 0.0002661999999999997)
    (20, 0.0012075)
};
\end{axis}
\end{tikzpicture}
\end{document}

程式碼產生了這個圖表:

圖表

它是由此動態生成的Python函數

相關內容