
我想製作一個散點圖並mark size
根據變數改變依賴關係。此外,我希望標記顏色取決於文件中給出的元值(在給定程式碼中工作)。例如,我想要一個非線性依賴項,例如mark size = sqrt(y value of point)
,或使用 for 迴圈中的變數。由於標記大小似乎需要一個長度,我只是找不到方法進行一些計算,因為我對\pgfmathresult
某些\edef...
巨集的嘗試沒有成功。
我設法用程式碼更改標記大小
scatter/@pre marker code/.style={/tikz/mark size={4-\pgfkeysvalueof{/data point/y}/4}},%\pgfmathparse{1}\pgfmathresult},
scatter/@post marker code/.style={}
然而,到元值的顏色映射就會遺失。
\documentclass[tikz, border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{plotmarks}
\usepackage{amsmath}
\usepackage{filecontents}
\begin{filecontents*}{temp.dat}
1 1 100
2 2 200
3 3 300
4 4 400
5 5 500
6 6 600
7 7 700
8 8 800
9 9 900
10 10 1000
\end{filecontents*}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
width=4.5in,
height=3.5in,
scale only axis,
xmin=0,
xmax=10,
ymin=0,
ymax=10,
axis x line*=bottom,
axis y line*=left,
colorbar
]
\addplot[%
scatter=true,
only marks,
mark=*,
color=blue,
point meta=explicit symbolic,
%scatter/@pre marker code/.style={/tikz/mark size= f (yvalue)?},
%scatter/@post marker code/.style={}
] table [meta index=2] {temp.dat};
\end{axis}
\end{tikzpicture}%
\end{document}
範例程式碼是從這裡複製的:使用元資料作為標記大小時如何保持相同的標記顏色?
答案1
編輯:
我發現我第一次提交的答案存在一些問題。我認為問題在於缺失\usepackage{filecontents}
,但正如傑克指出的那樣,這應該不重要。無論如何,我添加了第二個解決方案,其中標記大小和色條比例實際上代表了值y
。
\documentclass[tikz, border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{plotmarks}
\usepackage{amsmath}
\usepackage{filecontents}
\begin{filecontents*}{temp.dat}
x y
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100
\end{filecontents*}
\begin{document}
\pgfplotstableread{temp.dat}{\tempdat}
\begin{tikzpicture}
\begin{axis}[%
width=4.5in,
height=3.5in,
scale only axis,
xmin=0,
xmax=10,
ymin=0,
axis x line*=bottom,
axis y line*=left,
colorbar
]
\addplot[%
scatter=true,
only marks,
mark=*,
color=blue,
visualization depends on = {y/2 \as \perpointmarksize},
scatter/@pre marker code/.append style={/tikz/mark size=\perpointmarksize},
] table [x={x},y={y}] {\tempdat};
\end{axis}
\end{tikzpicture}
\end{document}
原答案:
如果將註解行替換為以下內容(請參閱手冊中的第 4.25 節),它就會起作用:
visualization depends on = {y \as \perpointmarksize},
scatter/@pre marker code/.append style={/tikz/mark size=\perpointmarksize},
完整程式碼:
\documentclass[tikz, border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{plotmarks}
\usepackage{amsmath}
\begin{filecontents*}{temp.dat}
1 1 100
2 2 200
3 3 300
4 4 400
5 5 500
6 6 600
7 7 700
8 8 800
9 9 900
10 10 1000
\end{filecontents*}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
width=4.5in,
height=3.5in,
scale only axis,
xmin=0,
xmax=10,
ymin=0,
ymax=10,
axis x line*=bottom,
axis y line*=left,
colorbar
]
\addplot[%
scatter=true,
only marks,
mark=*,
color=blue,
point meta=explicit symbolic,
visualization depends on = {y \as \perpointmarksize},
scatter/@pre marker code/.append style={/tikz/mark size=\perpointmarksize},
] table [meta index=2] {temp.dat};
\end{axis}
\end{tikzpicture}%
\end{document}