交換繪圖的顏色

交換繪圖的顏色

我想交換圖中的兩種繪圖顏色。請看一下這個:

在此輸入影像描述

正如您所看到的,第二張圖中省略了「短期」圖表。因此,為了減少研究這兩個圖時的混亂,我想將圖1中「短期」的顏色改為棕色,將「長期」的顏色改為紅色。這是我的程式碼:

\documentclass[12pt]{article}
\usepackage[left=3cm,top=3.5cm,right=2.5cm,bottom=2.5cm]{geometry}
\usepackage[english]{babel}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{filecontents}
\usepackage[margin=1cm]{caption}
\captionsetup[figure]{skip=18pt}

\begin{filecontents}{\jobname Data1.csv} 
Price;OneSided;ShortRun;LongRun
5;8.7;4;20.3 
10;16;11.7;20.7 
15;22.2;18.2;21 
20;27.6;23.9;21.3 
30;36.3;33.1;21.7
40;43.2;40.3;22.1
50;48.8;46.2;22.4
60;53.3;50.9;22.6
70;57.1;54.9;22.8
80;60.4;58.3;23
90;63.1;61.3;23.1
100;65.6;63.8;23.3
\end{filecontents}

\begin{filecontents}{\jobname Data3.csv} 
Price;OneSided;LongRun
5;8.7;14.9 
10;16;17.5 
15;22.2;20.1 
20;27.6;22.7 
30;36.3;28
40;43.2;33.2
50;48.8;38.4
60;53.3;43.7
70;57.1;48.9
80;60.4;54.1
90;63.1;59.3
100;65.6;64.6
\end{filecontents}

\begin{document}
\begin{figure}
\caption[Comparison of critical diversion ratios over different prices]
{Comparison of critical diversion ratios over different prices.}
\label{PlotSymmetrischDR}
\centering
\begin{tikzpicture} 
\begin{axis}[style={auto},legend pos=north west,ymin=0, 
%        xtick={5,10,15,20,25,30,40,50,60,70,80,90,100}, 
     x tick label style={/pgf/number format/1000 sep=}, 
     xlabel={Price increase, in \%}, 
     y tick label style={/pgf/number format/1000 sep=}, 
     ylabel={Critical diversion ratio, in \%} 
     ] 
\addplot table [y=OneSided,col sep=semicolon]  {\jobname Data1.csv}; 
\addlegendentry{One-sided} 
\addplot table [y=ShortRun,col sep=semicolon]  {\jobname Data1.csv}; 
\addlegendentry{Short-run} 
\addplot table [y=LongRun,col sep=semicolon]  {\jobname Data1.csv}; 
\addlegendentry{Long-run}
\end{axis} 
\end{tikzpicture}
\end{figure}

\begin{figure}
\caption[Comparison of critical diversion ratios over different prices]
 {Comparison of critical diversion ratios over different prices.}
\label{PlotASymmetrischDR}
\centering
\begin{tikzpicture} 
\begin{axis}[style={auto},legend pos=north west,ymin=0, 
%        xtick={5,10,15,20,25,30,40,50,60,70,80,90,100}, 
     x tick label style={/pgf/number format/1000 sep=}, 
     xlabel={Price increase, in \%}, 
     y tick label style={/pgf/number format/1000 sep=}, 
     ylabel={Critical diversion ratio, in \%} 
     ] 
\addplot table [y=OneSided,col sep=semicolon]  {\jobname Data3.csv}; 
\addlegendentry{One-sided} 
\addplot table [y=LongRun,col sep=semicolon]  {\jobname Data3.csv}; 
\addlegendentry{Long-run}
\end{axis} 
\end{tikzpicture}
\end{figure}
\end{document} 

我嘗試按照建議插入redbrown\addplot table [y=ShortRun,col sep=semicolon]這裡,但它不會改變任何東西。誰能幫我弄清楚我做錯了什麼嗎?

另外,是否可以將這些數字並排顯示?我嘗試改變整個人物的大小,使它們能夠彼此貼合,但這看起來不太好。也許我可以嘗試其他一些解決方案?

謝謝您的幫忙!

答案1

解決方案1:只需交換第一張圖中的繪圖順序即可。

\documentclass[12pt]{article}
\usepackage[left=3cm,top=3.5cm,right=2.5cm,bottom=2.5cm]{geometry}
\usepackage[english]{babel}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{filecontents}
\usepackage[margin=1cm]{caption}
\captionsetup[figure]{skip=18pt}

\begin{filecontents}{\jobname Data1.csv} 
Price;OneSided;ShortRun;LongRun
5;8.7;4;20.3 
10;16;11.7;20.7 
15;22.2;18.2;21 
20;27.6;23.9;21.3 
30;36.3;33.1;21.7
40;43.2;40.3;22.1
50;48.8;46.2;22.4
60;53.3;50.9;22.6
70;57.1;54.9;22.8
80;60.4;58.3;23
90;63.1;61.3;23.1
100;65.6;63.8;23.3
\end{filecontents}

\begin{filecontents}{\jobname Data3.csv} 
Price;OneSided;LongRun
5;8.7;14.9 
10;16;17.5 
15;22.2;20.1 
20;27.6;22.7 
30;36.3;28
40;43.2;33.2
50;48.8;38.4
60;53.3;43.7
70;57.1;48.9
80;60.4;54.1
90;63.1;59.3
100;65.6;64.6
\end{filecontents}

\begin{document}
\begin{figure}
\caption[Comparison of critical diversion ratios over different prices]
{Comparison of critical diversion ratios over different prices.}
\label{PlotSymmetrischDR}
\centering
\begin{tikzpicture} 
\begin{axis}[style={auto},legend pos=north west,ymin=0, 
%        xtick={5,10,15,20,25,30,40,50,60,70,80,90,100}, 
     x tick label style={/pgf/number format/1000 sep=}, 
     xlabel={Price increase, in \%}, 
     y tick label style={/pgf/number format/1000 sep=}, 
     ylabel={Critical diversion ratio, in \%} 
     ] 
\addplot table [y=OneSided,col sep=semicolon]  {\jobname Data1.csv}; 
\addlegendentry{One-sided} 
\addplot table [y=LongRun,col sep=semicolon]  {\jobname Data1.csv}; 
\addlegendentry{Long-run}
\addplot table [y=ShortRun,col sep=semicolon]  {\jobname Data1.csv}; 
\addlegendentry{Short-run} 
\end{axis} 
\end{tikzpicture}
\end{figure}

\begin{figure}
\caption[Comparison of critical diversion ratios over different prices]
 {Comparison of critical diversion ratios over different prices.}
\label{PlotASymmetrischDR}
\centering
\begin{tikzpicture} 
\begin{axis}[style={auto},legend pos=north west,ymin=0, 
%        xtick={5,10,15,20,25,30,40,50,60,70,80,90,100}, 
     x tick label style={/pgf/number format/1000 sep=}, 
     xlabel={Price increase, in \%}, 
     y tick label style={/pgf/number format/1000 sep=}, 
     ylabel={Critical diversion ratio, in \%} 
     ] 
\addplot table [y=OneSided,col sep=semicolon]  {\jobname Data3.csv}; 
\addlegendentry{One-sided} 
\addplot table [y=LongRun,col sep=semicolon]  {\jobname Data3.csv}; 
\addlegendentry{Long-run}
\end{axis} 
\end{tikzpicture}
\end{figure}
\end{document}

解決方案2:手動更改圖2中的繪圖顏色:

\documentclass[12pt]{article}
\usepackage[left=3cm,top=3.5cm,right=2.5cm,bottom=2.5cm]{geometry}
\usepackage[english]{babel}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{filecontents}
\usepackage[margin=1cm]{caption}
\captionsetup[figure]{skip=18pt}

\begin{filecontents}{\jobname Data1.csv} 
Price;OneSided;ShortRun;LongRun
5;8.7;4;20.3 
10;16;11.7;20.7 
15;22.2;18.2;21 
20;27.6;23.9;21.3 
30;36.3;33.1;21.7
40;43.2;40.3;22.1
50;48.8;46.2;22.4
60;53.3;50.9;22.6
70;57.1;54.9;22.8
80;60.4;58.3;23
90;63.1;61.3;23.1
100;65.6;63.8;23.3
\end{filecontents}

\begin{filecontents}{\jobname Data3.csv} 
Price;OneSided;LongRun
5;8.7;14.9 
10;16;17.5 
15;22.2;20.1 
20;27.6;22.7 
30;36.3;28
40;43.2;33.2
50;48.8;38.4
60;53.3;43.7
70;57.1;48.9
80;60.4;54.1
90;63.1;59.3
100;65.6;64.6
\end{filecontents}

\begin{document}
\begin{figure}
\caption[Comparison of critical diversion ratios over different prices]
{Comparison of critical diversion ratios over different prices.}
\label{PlotSymmetrischDR}
\centering
\begin{tikzpicture} 
\begin{axis}[style={auto},legend pos=north west,ymin=0, 
%        xtick={5,10,15,20,25,30,40,50,60,70,80,90,100}, 
     x tick label style={/pgf/number format/1000 sep=}, 
     xlabel={Price increase, in \%}, 
     y tick label style={/pgf/number format/1000 sep=}, 
     ylabel={Critical diversion ratio, in \%} 
     ] 
\addplot table [y=OneSided,col sep=semicolon]  {\jobname Data1.csv}; 
\addlegendentry{One-sided}
\addplot table [y=ShortRun,col sep=semicolon]  {\jobname Data1.csv}; 
\addlegendentry{Short-run}  
\addplot table [y=LongRun,col sep=semicolon]  {\jobname Data1.csv}; 
\addlegendentry{Long-run}
\end{axis} 
\end{tikzpicture}
\end{figure}

\begin{figure}
\caption[Comparison of critical diversion ratios over different prices]
 {Comparison of critical diversion ratios over different prices.}
\label{PlotASymmetrischDR}
\centering
\begin{tikzpicture} 
\begin{axis}[style={auto},legend pos=north west,ymin=0, 
%        xtick={5,10,15,20,25,30,40,50,60,70,80,90,100}, 
     x tick label style={/pgf/number format/1000 sep=}, 
     xlabel={Price increase, in \%}, 
     y tick label style={/pgf/number format/1000 sep=}, 
     ylabel={Critical diversion ratio, in \%} 
     ] 
\addplot table [y=OneSided,col sep=semicolon]  {\jobname Data3.csv}; 
\addlegendentry{One-sided} 
\addplot+[brown,mark=*,every mark/.append style={solid, fill=brown}] table [y=LongRun,col sep=semicolon]  {\jobname Data3.csv}; 
\addlegendentry{Long-run}
\end{axis} 
\end{tikzpicture}
\end{figure}
\end{document}

請注意,格式化位於\addplot[],而不是... table [],其中+表示追加。


解決方案3:在圖 2 中,向前跳一格循環清單(套用於每個連續繪圖的樣式順序)。這可以透過\pgfplotsset{cycle list shift=1}如下方式完成。

\documentclass[12pt]{article}
\usepackage[left=3cm,top=3.5cm,right=2.5cm,bottom=2.5cm]{geometry}
\usepackage[english]{babel}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{filecontents}
\usepackage[margin=1cm]{caption}
\captionsetup[figure]{skip=18pt}

\begin{filecontents}{\jobname Data1.csv} 
    Price;OneSided;ShortRun;LongRun
    5;8.7;4;20.3 
    10;16;11.7;20.7 
    15;22.2;18.2;21 
    20;27.6;23.9;21.3 
    30;36.3;33.1;21.7
    40;43.2;40.3;22.1
    50;48.8;46.2;22.4
    60;53.3;50.9;22.6
    70;57.1;54.9;22.8
    80;60.4;58.3;23
    90;63.1;61.3;23.1
    100;65.6;63.8;23.3
\end{filecontents}

\begin{filecontents}{\jobname Data3.csv} 
    Price;OneSided;LongRun
    5;8.7;14.9 
    10;16;17.5 
    15;22.2;20.1 
    20;27.6;22.7 
    30;36.3;28
    40;43.2;33.2
    50;48.8;38.4
    60;53.3;43.7
    70;57.1;48.9
    80;60.4;54.1
    90;63.1;59.3
    100;65.6;64.6
\end{filecontents}

\begin{document}
    \begin{figure}
        \caption[Comparison of critical diversion ratios over different prices]
        {Comparison of critical diversion ratios over different prices.}
        \label{PlotSymmetrischDR}
        \centering
        \begin{tikzpicture} 
        \begin{axis}[style={auto},legend pos=north west,ymin=0, 
        %        xtick={5,10,15,20,25,30,40,50,60,70,80,90,100}, 
        x tick label style={/pgf/number format/1000 sep=}, 
        xlabel={Price increase, in \%}, 
        y tick label style={/pgf/number format/1000 sep=}, 
        ylabel={Critical diversion ratio, in \%} 
        ] 
        \addplot table [y=OneSided,col sep=semicolon]  {\jobname Data1.csv}; 
        \addlegendentry{One-sided} 
        \addplot table [y=ShortRun,col sep=semicolon]  {\jobname Data1.csv}; 
        \addlegendentry{Short-run} 
        \addplot table [y=LongRun,col sep=semicolon]  {\jobname Data1.csv}; 
        \addlegendentry{Long-run}
        \end{axis} 
        \end{tikzpicture}
    \end{figure}

    \begin{figure}
        \caption[Comparison of critical diversion ratios over different prices]
        {Comparison of critical diversion ratios over different prices.}
        \label{PlotASymmetrischDR}
        \centering
        \begin{tikzpicture} 
        \begin{axis}[style={auto},legend pos=north west,ymin=0, 
        %        xtick={5,10,15,20,25,30,40,50,60,70,80,90,100}, 
        x tick label style={/pgf/number format/1000 sep=}, 
        xlabel={Price increase, in \%}, 
        y tick label style={/pgf/number format/1000 sep=}, 
        ylabel={Critical diversion ratio, in \%} 
        ] 
        \addplot table [y=OneSided,col sep=semicolon]  {\jobname Data3.csv}; 
        \addlegendentry{One-sided} 
        \pgfplotsset{cycle list shift=1}
        \addplot table [y=LongRun,col sep=semicolon]  {\jobname Data3.csv}; 
        \addlegendentry{Long-run}
        \end{axis} 
        \end{tikzpicture}
    \end{figure}
\end{document} 

另外,是否可以將這些數字並排顯示?我嘗試改變整個人物的大小,使它們能夠彼此貼合,但這看起來不太好。也許我可以嘗試其他一些解決方案?

你見過這個問題嗎:兩個人影並排

同樣,在縮放地塊方面,有多種選擇

以下解決方案使用並排圖形的迷你頁和調整大小框來相應地縮放繪圖:

\documentclass[12pt]{article}
\usepackage[left=3cm,top=3.5cm,right=2.5cm,bottom=2.5cm]{geometry}
\usepackage[english]{babel}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{filecontents}
\usepackage[margin=1cm]{caption}
\captionsetup[figure]{skip=18pt}

\begin{filecontents}{\jobname Data1.csv} 
Price;OneSided;ShortRun;LongRun
5;8.7;4;20.3 
10;16;11.7;20.7 
15;22.2;18.2;21 
20;27.6;23.9;21.3 
30;36.3;33.1;21.7
40;43.2;40.3;22.1
50;48.8;46.2;22.4
60;53.3;50.9;22.6
70;57.1;54.9;22.8
80;60.4;58.3;23
90;63.1;61.3;23.1
100;65.6;63.8;23.3
\end{filecontents}

\begin{filecontents}{\jobname Data3.csv} 
Price;OneSided;LongRun
5;8.7;14.9 
10;16;17.5 
15;22.2;20.1 
20;27.6;22.7 
30;36.3;28
40;43.2;33.2
50;48.8;38.4
60;53.3;43.7
70;57.1;48.9
80;60.4;54.1
90;63.1;59.3
100;65.6;64.6
\end{filecontents}

\begin{document}
\begin{figure}
\centering
\begin{minipage}{0.49\textwidth}
\caption[Comparison of critical diversion ratios over different prices]
{Comparison of critical diversion ratios over different prices.}
\label{PlotSymmetrischDR}
\centering
\resizebox{\textwidth}{!}{
\begin{tikzpicture} 
\begin{axis}[style={auto},legend pos=north west,ymin=0, 
%        xtick={5,10,15,20,25,30,40,50,60,70,80,90,100}, 
     x tick label style={/pgf/number format/1000 sep=}, 
     xlabel={Price increase, in \%}, 
     y tick label style={/pgf/number format/1000 sep=}, 
     ylabel={Critical diversion ratio, in \%} 
     ] 
\addplot table [y=OneSided,col sep=semicolon]  {\jobname Data1.csv}; 
\addlegendentry{One-sided}
\addplot table [y=ShortRun,col sep=semicolon]  {\jobname Data1.csv}; 
\addlegendentry{Short-run}  
\addplot table [y=LongRun,col sep=semicolon]  {\jobname Data1.csv}; 
\addlegendentry{Long-run}
\end{axis} 
\end{tikzpicture}
}
\end{minipage}
\hfill
\begin{minipage}{0.49\textwidth}
\caption[Comparison of critical diversion ratios over different prices]
 {Comparison of critical diversion ratios over different prices.}
\label{PlotASymmetrischDR}
\centering
\resizebox{\textwidth}{!}{
\begin{tikzpicture} 
\begin{axis}[style={auto},legend pos=north west,ymin=0, 
%        xtick={5,10,15,20,25,30,40,50,60,70,80,90,100}, 
     x tick label style={/pgf/number format/1000 sep=}, 
     xlabel={Price increase, in \%}, 
     y tick label style={/pgf/number format/1000 sep=}, 
     ylabel={Critical diversion ratio, in \%} 
     ] 
\addplot table [y=OneSided,col sep=semicolon]  {\jobname Data3.csv}; 
\addlegendentry{One-sided} 
\addplot+[brown,mark=*,every mark/.append style={solid, fill=brown}] table [y=LongRun,col sep=semicolon]  {\jobname Data3.csv}; 
\addlegendentry{Long-run}
\end{axis} 
\end{tikzpicture}
}
\end{minipage}
\end{figure}
\end{document}

相關內容