我正在尝试绘制不同实体的年份函数中某个数字。在这个例子中,它是不同机构每年的月球任务数量。到目前为止,我得出的结论如下:
相关代码为:
\begin{tikzpicture}
\begin{axis}[
width=0.8\textwidth,
height=0.25\textheight,
stack plots=y,
area style,
xlabel={Year},
ylabel={Number of missions},
enlarge x limits=false,
enlarge y limits=upper,
legend pos=outer north east,
x tick label style={/pgf/number format/1000 sep=}, ]
% USA
\addplot coordinates
{(1958,4) (1959,3) (1960,2) (1962,3) (1963,0)
(1964,2) (1965,2) (1966,4) (1967,7) (1968,2)
(1969,3) (1970,2) (1971,3) (1972,3) (1973,0)
(1974,0) (1975,0) (1976,0)
(1977,0) (1978,0) (1979,0) (1980,0) (1981,0)
(1982,0) (1983,0) (1984,0) (1985,0) (1986,0)
(1987,0) (1988,0) (1989,0)
(1990,0) (1991,0) (1992,0) (1993,0) (1994,1)
(1995,0) (1996,0) (1997,0) (1998,1)
(1999,0) (2000,0) (2001,0) (2002,0)
(2003,0) (2004,0) (2005,0) (2006,0) (2007,0)
(2008,0) (2009,3) (2010,0)
(2011,1) } \closedcycle;
\addlegendentry{USA}
% Soviets/Russia
\addplot coordinates
{(1958,3) (1959,4) (1960,2) (1962,0) (1963,3)
(1964,2) (1965,7) (1966,6) (1967,0) (1968,4)
(1969,5) (1970,5) (1971,2) (1972,1) (1973,2)
(1974,2) (1975,0) (1976,1)
(1977,0) (1978,0) (1979,0) (1980,0) (1981,0)
(1982,0) (1983,0) (1984,0) (1985,0) (1986,0)
(1987,0) (1988,0) (1989,0)
(1990,0) (1991,0) (1992,0) (1993,0) (1994,0)
(1995,0) (1996,0) (1997,0) (1998,0)
(1999,0) (2000,0) (2001,0) (2002,0)
(2003,0) (2004,0) (2005,0) (2006,0) (2007,0)
(2008,0) (2009,0) (2010,0)
(2011,0) } \closedcycle;
\addlegendentry{Russia}
% Japan
\addplot coordinates
{(1958,0) (1959,0) (1960,0) (1962,0) (1963,0)
(1964,0) (1965,0) (1966,0) (1967,0) (1968,0)
(1969,0) (1970,0) (1971,0) (1972,0) (1973,0)
(1974,0) (1975,0) (1976,0)
(1977,0) (1978,0) (1979,0) (1980,0) (1981,0)
(1982,0) (1983,0) (1984,0) (1985,0) (1986,0)
(1987,0) (1988,0) (1989,0)
(1990,1) (1991,0) (1992,0) (1993,0) (1994,0)
(1995,0) (1996,0) (1997,0) (1998,0)
(1999,0) (2000,0) (2001,0) (2002,0)
(2003,0) (2004,0) (2005,0) (2006,0) (2007,1)
(2008,0) (2009,0) (2010,0)
(2011,0) } \closedcycle;
\addlegendentry{Japan}
% ESA
\addplot coordinates
{(1958,0) (1959,0) (1960,0) (1962,0) (1963,0)
(1964,0) (1965,0) (1966,0) (1967,0) (1968,0)
(1969,0) (1970,0) (1971,0) (1972,0) (1973,0)
(1974,0) (1975,0) (1976,0)
(1977,0) (1978,0) (1979,0) (1980,0) (1981,0)
(1982,0) (1983,0) (1984,0) (1985,0) (1986,0)
(1987,0) (1988,0) (1989,0)
(1990,0) (1991,0) (1992,0) (1993,0) (1994,0)
(1995,0) (1996,0) (1997,0) (1998,0)
(1999,0) (2000,0) (2001,0) (2002,0)
(2003,1) (2004,0) (2005,0) (2006,0) (2007,0)
(2008,0) (2009,0) (2010,0)
(2011,0) } \closedcycle;
\addlegendentry{Europe}
% China
\addplot coordinates
{(1958,0) (1959,0) (1960,0) (1962,0) (1963,0)
(1964,0) (1965,0) (1966,0) (1967,0) (1968,0)
(1969,0) (1970,0) (1971,0) (1972,0) (1973,0)
(1974,0) (1975,0) (1976,0)
(1977,0) (1978,0) (1979,0) (1980,0) (1981,0)
(1982,0) (1983,0) (1984,0) (1985,0) (1986,0)
(1987,0) (1988,0) (1989,0)
(1990,0) (1991,0) (1992,0) (1993,0) (1994,0)
(1995,0) (1996,0) (1997,0) (1998,0)
(1999,0) (2000,0) (2001,0) (2002,0)
(2003,0) (2004,0) (2005,0) (2006,0) (2007,1)
(2008,0) (2009,0) (2010,1)
(2011,0) } \closedcycle;
\addlegendentry{China}
% India
\addplot coordinates
{(1958,0) (1959,0) (1960,0) (1962,0) (1963,0)
(1964,0) (1965,0) (1966,0) (1967,0) (1968,0)
(1969,0) (1970,0) (1971,0) (1972,0) (1973,0)
(1974,0) (1975,0) (1976,0)
(1977,0) (1978,0) (1979,0) (1980,0) (1981,0)
(1982,0) (1983,0) (1984,0) (1985,0) (1986,0)
(1987,0) (1988,0) (1989,0)
(1990,0) (1991,0) (1992,0) (1993,0) (1994,0)
(1995,0) (1996,0) (1997,0) (1998,0)
(1999,0) (2000,0) (2001,0) (2002,0)
(2003,0) (2004,0) (2005,0) (2006,0) (2007,0)
(2008,2) (2009,0) (2010,0)
(2011,0) } \closedcycle;
\addlegendentry{India}
\end{axis}
\end{tikzpicture}
然而,我发现这种表示非常不合适,尽管这是我能想到的最好的表示。如果每年只有一个条目,就会形成这个奇怪的三角形。此外,有些部分看起来是错误的,比如印度的情节。
绘制此图的适当方法是什么?
答案1
对于像这样的小数字,每个事件使用一个符号可能是一个好主意:
我使用 Matlab 通过以下脚本将数据调整为合适的格式(data
假设包含表格数据,第一列为年份,连续列为任务数量):
numberOfCountries = size(data,2)-1;
Launches=data(:,2:end);
baseLevels = cumsum(Launches,2);
baseLevels = [ zeros(length(data),1) baseLevels];
maxLaunches = max(sum(Launches));
cumLaunches = [zeros(1,numberOfCountries); cumsum(Launches)];
Years=nan(maxLaunches,size(data,2)-1);
Levels=Years;
for country = 1:numberOfCountries
for j = 1:length(Launches)
Years(cumLaunches(j,country)+1:cumLaunches(j+1,country),country) = ...
repmat(data(j,1),Launches(j,country),1);
Levels(cumLaunches(j,country)+1:cumLaunches(j+1,country),country) = ...
baseLevels(j,country) + (1:Launches(j,country))';
end
end
a=Years';
b=Levels';
Output=reshape([a(:) b(:)]',2*size(a,1), [])';
然后可以将输出写入文件或直接用作文档中的表格.tex
:
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{decorations.shapes}
\begin{document}
\definecolor{Color1}{RGB}{55, 126, 184}
\definecolor{Color2}{RGB}{228, 26, 28}
\definecolor{Color3}{RGB}{77, 175, 74}
\definecolor{Color4}{RGB}{152, 78, 163}
\definecolor{Color5}{RGB}{255, 127, 0}
\definecolor{Color6}{RGB}{255, 255, 51}
\pgfplotstableread{
USAyear USAlevel Russiayear Russialevel Japanyear Japanlevel Europeyear Europelevel Chinayear Chinalevel Indiayear Indialevel
1958 1 1958 5 1989 1 2002 1 2006 2 2007 1
1958 2 1958 6 2006 1 NaN NaN 2009 1 2007 2
1958 3 1958 7 NaN NaN NaN NaN NaN NaN NaN NaN
1958 4 1959 4 NaN NaN NaN NaN NaN NaN NaN NaN
1959 1 1959 5 NaN NaN NaN NaN NaN NaN NaN NaN
1959 2 1959 6 NaN NaN NaN NaN NaN NaN NaN NaN
1959 3 1959 7 NaN NaN NaN NaN NaN NaN NaN NaN
1960 1 1960 3 NaN NaN NaN NaN NaN NaN NaN NaN
1960 2 1960 4 NaN NaN NaN NaN NaN NaN NaN NaN
1961 1 1962 1 NaN NaN NaN NaN NaN NaN NaN NaN
1961 2 1962 2 NaN NaN NaN NaN NaN NaN NaN NaN
1961 3 1962 3 NaN NaN NaN NaN NaN NaN NaN NaN
1963 1 1963 3 NaN NaN NaN NaN NaN NaN NaN NaN
1963 2 1963 4 NaN NaN NaN NaN NaN NaN NaN NaN
1964 1 1964 3 NaN NaN NaN NaN NaN NaN NaN NaN
1964 2 1964 4 NaN NaN NaN NaN NaN NaN NaN NaN
1965 1 1964 5 NaN NaN NaN NaN NaN NaN NaN NaN
1965 2 1964 6 NaN NaN NaN NaN NaN NaN NaN NaN
1965 3 1964 7 NaN NaN NaN NaN NaN NaN NaN NaN
1965 4 1964 8 NaN NaN NaN NaN NaN NaN NaN NaN
1966 1 1964 9 NaN NaN NaN NaN NaN NaN NaN NaN
1966 2 1965 5 NaN NaN NaN NaN NaN NaN NaN NaN
1966 3 1965 6 NaN NaN NaN NaN NaN NaN NaN NaN
1966 4 1965 7 NaN NaN NaN NaN NaN NaN NaN NaN
1966 5 1965 8 NaN NaN NaN NaN NaN NaN NaN NaN
1966 6 1965 9 NaN NaN NaN NaN NaN NaN NaN NaN
1966 7 1965 10 NaN NaN NaN NaN NaN NaN NaN NaN
1967 1 1967 3 NaN NaN NaN NaN NaN NaN NaN NaN
1967 2 1967 4 NaN NaN NaN NaN NaN NaN NaN NaN
1968 1 1967 5 NaN NaN NaN NaN NaN NaN NaN NaN
1968 2 1967 6 NaN NaN NaN NaN NaN NaN NaN NaN
1968 3 1968 4 NaN NaN NaN NaN NaN NaN NaN NaN
1969 1 1968 5 NaN NaN NaN NaN NaN NaN NaN NaN
1969 2 1968 6 NaN NaN NaN NaN NaN NaN NaN NaN
1970 1 1968 7 NaN NaN NaN NaN NaN NaN NaN NaN
1970 2 1968 8 NaN NaN NaN NaN NaN NaN NaN NaN
1970 3 1969 3 NaN NaN NaN NaN NaN NaN NaN NaN
1971 1 1969 4 NaN NaN NaN NaN NaN NaN NaN NaN
1971 2 1969 5 NaN NaN NaN NaN NaN NaN NaN NaN
1971 3 1969 6 NaN NaN NaN NaN NaN NaN NaN NaN
1993 1 1969 7 NaN NaN NaN NaN NaN NaN NaN NaN
1997 1 1970 4 NaN NaN NaN NaN NaN NaN NaN NaN
2008 1 1970 5 NaN NaN NaN NaN NaN NaN NaN NaN
2008 2 1971 4 NaN NaN NaN NaN NaN NaN NaN NaN
2008 3 1972 1 NaN NaN NaN NaN NaN NaN NaN NaN
2010 1 1972 2 NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN 1973 1 NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN 1973 2 NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN 1975 1 NaN NaN NaN NaN NaN NaN NaN NaN
}\datatable
\begin{tikzpicture}
\begin{axis}[
compat=1.6,
y=3mm,
x=3mm,
xmin=1957, xmax=2011,
ymin=0,
only marks, mark size=4, every axis plot post/.style={draw=black},
y filter/.code=\pgfmathparse{#1-0.5},
xlabel={Year},
ylabel={Number of missions},
enlarge y limits=upper,
legend pos=outer north east,
x tick label style={/pgf/number format/1000 sep=},
xtick align=outside,
]
% USA
\addplot [Color1] table [x=USAyear, y=USAlevel] {\datatable};
\addlegendentry{USA}
\addplot [Color2] table [x=Russiayear, y=Russialevel] {\datatable};
\addlegendentry{Russia}
\addplot [Color3] table [x=Japanyear, y=Japanlevel] {\datatable};
\addlegendentry{Japan}
\addplot [Color4] table [x=Europeyear, y=Europelevel] {\datatable};
\addlegendentry{Europe}
\addplot [Color5] table [x=Chinayear, y=Chinalevel] {\datatable};
\addlegendentry{China}
\addplot [Color6] table [x=Indiayear, y=Indialevel] {\datatable};
\addlegendentry{India}
\end{axis}
\end{tikzpicture}
\end{document}
答案2
我想到了这个,虽然我认为它远非完美,但还不错。
\begin{tikzpicture}
\begin{axis}[
width=0.8\textwidth,
height=0.25\textheight,
ybar stacked,
ytick={0,1,...,10},
%stack plots=y,
%area style,
xlabel={Year},
ylabel={Number of missions},
enlarge x limits=0.05,
enlarge y limits=upper,
legend pos=outer north east,
x tick label style={/pgf/number format/1000 sep=},
bar width=3pt, ]
% USA
\addplot coordinates
{(1958,4) (1959,3) (1960,2) (1962,3) (1963,0)
(1964,2) (1965,2) (1966,4) (1967,7) (1968,2)
(1969,3) (1970,2) (1971,3) (1972,3) (1973,0)
(1974,0) (1975,0) (1976,0)
(1977,0) (1978,0) (1979,0) (1980,0) (1981,0)
(1982,0) (1983,0) (1984,0) (1985,0) (1986,0)
(1987,0) (1988,0) (1989,0)
(1990,0) (1991,0) (1992,0) (1993,0) (1994,1)
(1995,0) (1996,0) (1997,0) (1998,1)
(1999,0) (2000,0) (2001,0) (2002,0)
(2003,0) (2004,0) (2005,0) (2006,0) (2007,0)
(2008,0) (2009,3) (2010,0)
(2011,1) } ;% \closedcycle;
\addlegendentry{USA}
% Soviets/Russia
\addplot coordinates
{(1958,3) (1959,4) (1960,2) (1962,0) (1963,3)
(1964,2) (1965,7) (1966,6) (1967,0) (1968,4)
(1969,5) (1970,5) (1971,2) (1972,1) (1973,2)
(1974,2) (1975,0) (1976,1)
(1977,0) (1978,0) (1979,0) (1980,0) (1981,0)
(1982,0) (1983,0) (1984,0) (1985,0) (1986,0)
(1987,0) (1988,0) (1989,0)
(1990,0) (1991,0) (1992,0) (1993,0) (1994,0)
(1995,0) (1996,0) (1997,0) (1998,0)
(1999,0) (2000,0) (2001,0) (2002,0)
(2003,0) (2004,0) (2005,0) (2006,0) (2007,0)
(2008,0) (2009,0) (2010,0)
(2011,0) } ;% \closedcycle;
\addlegendentry{Russia}
% Japan
\addplot coordinates
{(1958,0) (1959,0) (1960,0) (1962,0) (1963,0)
(1964,0) (1965,0) (1966,0) (1967,0) (1968,0)
(1969,0) (1970,0) (1971,0) (1972,0) (1973,0)
(1974,0) (1975,0) (1976,0)
(1977,0) (1978,0) (1979,0) (1980,0) (1981,0)
(1982,0) (1983,0) (1984,0) (1985,0) (1986,0)
(1987,0) (1988,0) (1989,0)
(1990,1) (1991,0) (1992,0) (1993,0) (1994,0)
(1995,0) (1996,0) (1997,0) (1998,0)
(1999,0) (2000,0) (2001,0) (2002,0)
(2003,0) (2004,0) (2005,0) (2006,0) (2007,1)
(2008,0) (2009,0) (2010,0)
(2011,0) } ;% \closedcycle;
\addlegendentry{Japan}
% ESA
\addplot coordinates
{(1958,0) (1959,0) (1960,0) (1962,0) (1963,0)
(1964,0) (1965,0) (1966,0) (1967,0) (1968,0)
(1969,0) (1970,0) (1971,0) (1972,0) (1973,0)
(1974,0) (1975,0) (1976,0)
(1977,0) (1978,0) (1979,0) (1980,0) (1981,0)
(1982,0) (1983,0) (1984,0) (1985,0) (1986,0)
(1987,0) (1988,0) (1989,0)
(1990,0) (1991,0) (1992,0) (1993,0) (1994,0)
(1995,0) (1996,0) (1997,0) (1998,0)
(1999,0) (2000,0) (2001,0) (2002,0)
(2003,1) (2004,0) (2005,0) (2006,0) (2007,0)
(2008,0) (2009,0) (2010,0)
(2011,0) } ;% \closedcycle;
\addlegendentry{Europe}
% China
\addplot coordinates
{(1958,0) (1959,0) (1960,0) (1962,0) (1963,0)
(1964,0) (1965,0) (1966,0) (1967,0) (1968,0)
(1969,0) (1970,0) (1971,0) (1972,0) (1973,0)
(1974,0) (1975,0) (1976,0)
(1977,0) (1978,0) (1979,0) (1980,0) (1981,0)
(1982,0) (1983,0) (1984,0) (1985,0) (1986,0)
(1987,0) (1988,0) (1989,0)
(1990,0) (1991,0) (1992,0) (1993,0) (1994,0)
(1995,0) (1996,0) (1997,0) (1998,0)
(1999,0) (2000,0) (2001,0) (2002,0)
(2003,0) (2004,0) (2005,0) (2006,0) (2007,1)
(2008,0) (2009,0) (2010,1)
(2011,0) } ;% \closedcycle;
\addlegendentry{China}
% India
\addplot coordinates
{(1958,0) (1959,0) (1960,0) (1962,0) (1963,0)
(1964,0) (1965,0) (1966,0) (1967,0) (1968,0)
(1969,0) (1970,0) (1971,0) (1972,0) (1973,0)
(1974,0) (1975,0) (1976,0)
(1977,0) (1978,0) (1979,0) (1980,0) (1981,0)
(1982,0) (1983,0) (1984,0) (1985,0) (1986,0)
(1987,0) (1988,0) (1989,0)
(1990,0) (1991,0) (1992,0) (1993,0) (1994,0)
(1995,0) (1996,0) (1997,0) (1998,0)
(1999,0) (2000,0) (2001,0) (2002,0)
(2003,0) (2004,0) (2005,0) (2006,0) (2007,0)
(2008,2) (2009,0) (2010,0)
(2011,0) } ;% \closedcycle;
\addlegendentry{India}
\end{axis}
\end{tikzpicture}