此处创建一个直方图。
\documentclass{article}
\usepackage{pst-plot}
\savedata{\data}[{{0.5,1},{1,7},{1.5,1},{2,3},{2.5,1},{3,2},{3.5,1}}]
\begin{document}
\psset{xunit = 2}
\begin{pspicture}(-0.23,-0.53)(4,7.5)
\listplot[
plotstyle = bar,
barwidth = 0.8,
linecolor = red,
fillstyle = solid,
fillcolor = blue!70
]{\data}
\psaxes[
Dx = 0.5,
xticksize = -4pt 0
]{->}(4,7.5)
\end{pspicture}
\end{document}
我正在寻找类似的东西,但我想导入我创建的数据文件。我不知道如何修改它,以便它能够绘制我创建的数据文件的点。
在我的数据文件中,有一个从 -40 到 40 的数字列表(x 值),对应的 y 值是某个 x 值出现的次数。因此,数据文件的一个例子是。
(-40,0) (-39,0) (-38,0) (-37,0) (-36,0) (-35,0) (-34,0) (-33,0) (-32,0) (-31,0) (-30,0) (-29,0) (-28,0) (-27,0) (-26,0) (-25,0) (-24,0) (-23,0) (-22,0) (-21,0) (-20,0) (-19,0) (-18,0) (-17,0) (-16,0) (-15,0) (-14,1) (-13,0) (-12,2) (-11,0) (-10,4) (-9,0) (-8,6) (-7,0) (-6,8) (-5,0) (-4,13) (-3,0) (-2,9) (-1,0) (0,12) (1,0) (2,12) (3,0) (4,7) (5,0) (6,11) (7,0) (8,7) (9,0) (10,2) (11,0) (12,3) (13,0) (14,1) (15,0) (16,0) (17,0) (18,2) (19,0) (20,0) (21,0) (22,0) (23,0) (24,0) (25,0) (26,0) (27,0) (28,0) (29,0) (30,0) (31,0) (32,0) (33,0) (34,0) (35,0) (36,0) (37,0) (38,0) (39,0) (40,0)
另外,我用 LaTeX 写了这个,然后在 dvi 中显示它,但之后我需要将其转换为 PDF。
这可能吗?
PDF 部分是重要的部分,我不太在意图形是否没有出现在 dvi 文件中。
我能做到我想做的事吗?任何帮助都非常好,谢谢
答案1
您的数据文件格式有些奇怪。通常,自动生成的数据将是两列数据,以制表符/分号/空格等分隔。在此特定情况下,您可以简单地复制粘贴数据,如本例所示:
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot+[ycomb] coordinates {%
(-40,0) (-39,0) (-38,0) (-37,0) (-36,0) (-35,0) (-34,0) (-33,0) (-32,0) (-31,0) (-30,0) (-29,0)
(-28,0) (-27,0) (-26,0) (-25,0) (-24,0) (-23,0) (-22,0) (-21,0) (-20,0) (-19,0) (-18,0) (-17,0)
(-16,0) (-15,0) (-14,1) (-13,0) (-12,2) (-11,0) (-10,4) (-9,0) (-8,6) (-7,0) (-6,8) (-5,0)
(-4,13) (-3,0) (-2,9) (-1,0) (0,12) (1,0) (2,12) (3,0) (4,7) (5,0) (6,11) (7,0) (8,7) (9,0)
(10,2) (11,0) (12,3) (13,0) (14,1) (15,0) (16,0) (17,0) (18,2) (19,0) (20,0) (21,0) (22,0)
(23,0) (24,0) (25,0) (26,0) (27,0) (28,0) (29,0) (30,0) (31,0) (32,0) (33,0) (34,0) (35,0)
(36,0) (37,0) (38,0) (39,0) (40,0)
};
\end{axis}
\end{tikzpicture}
\end{document}
用 编译此文件pdflatex
可获得 pdf 文件。如果您想要条形图,ycomb
则更改为。ybar
另一方面,如果您的数据具有这种形式:
-40 0
-39 0
-38 0
-37 0
-36 0
-35 0
-34 0
-33 0
-32 4
-31 0
-30 0
-29 6
-28 0
-27 0
-26 9
-25 0
-24 0
-23 0
-22 0
-21 2
-20 0
-19 0
保存在名为的文件中mydata.txt
,然后您可以通过以下方式读取该文件:
\documentclass{article}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.12}
\usepackage{filecontents} % just for demo
\begin{filecontents*}{mydata.txt} %% to create the file mydata.txt, just for demo
-40 0
-39 0
-38 0
-37 0
-36 0
-35 0
-34 0
-33 0
-32 4
-31 0
-30 0
-29 6
-28 0
-27 0
-26 9
-25 0
-24 0
-23 0
-22 0
-21 2
-20 0
-19 0
\end{filecontents*}
\begin{document}
\begin{tikzpicture}
\begin{axis}[ybar]
\addplot table[x index=0,y index=1] {mydata.txt};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
\documentclass{article}
\usepackage{pst-plot}
\usepackage{filecontents}
\begin{filecontents*}{mydata.dat}% external data in file madata.dat
(-40,0) (-39,0) (-38,0) (-37,0) (-36,0) (-35,0) (-34,0) (-33,0) (-32,0) (-31,0) (-30,0)
(-29,0) (-28,0) (-27,0) (-26,0) (-25,0) (-24,0) (-23,0) (-22,0) (-21,0) (-20,0) (-19,0)
(-18,0) (-17,0) (-16,0) (-15,0) (-14,1) (-13,0) (-12,2) (-11,0) (-10,4) (-9,0) (-8,6)
(-7,0) (-6,8) (-5,0) (-4,13) (-3,0) (-2,9) (-1,0) (0,12) (1,0) (2,12) (3,0) (4,7) (5,0)
(6,11) (7,0) (8,7) (9,0) (10,2) (11,0) (12,3) (13,0) (14,1) (15,0) (16,0) (17,0) (18,2)
(19,0) (20,0) (21,0) (22,0) (23,0) (24,0) (25,0) (26,0) (27,0) (28,0) (29,0) (30,0)
(31,0) (32,0) (33,0) (34,0) (35,0) (36,0) (37,0) (38,0) (39,0) (40,0)
\end{filecontents*}
\begin{document}
\psset{xunit=0.15,yunit=0.75}
\readdata\data{mydata.dat}
\noindent
\begin{pspicture}(-45,-1)(45,14)
\listplot[plotstyle=bar,barwidth=0.3,
linecolor=blue,fillstyle=solid,fillcolor=blue!40]{\data}
\psaxes[Dx=10,Ox=-40,ticksize = -4pt 0,
linewidth=0.4pt]{->}(-40,0)(-42,-0.5)(42,13.5)
\end{pspicture}
\end{document}