背景:我正在使用tikzplotlib
将图转换plt
为tikz
。我将其应用于随机图网络,如下所示。
考虑以下 tikz 代码:
\documentclass{article}
\usepackage{tikz}
\usepackage{tkz-graph}
\usetikzlibrary{fit}
\usetikzlibrary{backgrounds}
\usepackage{pgfplots}
\begin{document}
% This file was created with tikzplotlib v0.10.1.
\begin{tikzpicture}
\definecolor{darkgray176}{RGB}{176,176,176}
\definecolor{steelblue31120180}{RGB}{31,120,180}
\begin{axis}[
hide x axis,
hide y axis,
scaled x ticks=manual:{}{\pgfmathparse{##1}},
scaled y ticks=manual:{}{\pgfmathparse{##1}},
tick align=outside,
x grid style={darkgray176},
xmajorticks=false,
xmin=-0.56233709013339, xmax=0.562444553413885,
xtick style={color=black},
xticklabels={},
y grid style={darkgray176},
ymajorticks=false,
ymin=-1.20947471305574, ymax=1.20995008585598,
ytick style={color=black},
yticklabels={}
]
\path [draw=black]
(axis cs:0.464839534758957,0.108082528289179)
--(axis cs:-0.464732071478461,-0.108557901089417);
\path [draw=black]
(axis cs:0.464839534758957,0.108082528289179)
--(axis cs:0.232917848079166,-0.999524627199763);
\path [draw=black]
(axis cs:0.464839534758957,0.108082528289179)
--(axis cs:-0.233025311359662,1);
\path [draw=black]
(axis cs:0.232917848079166,-0.999524627199763)
--(axis cs:-0.464732071478461,-0.108557901089417);
\path [draw=black]
(axis cs:-0.464732071478461,-0.108557901089417)
--(axis cs:-0.233025311359662,1);
\addplot [draw=steelblue31120180, fill=steelblue31120180, mark=*, only marks,mark options={scale=3}]
table{%
x y
0.464839534758957 0.108082528289179
0.232917848079166 -0.999524627199763
-0.464732071478461 -0.108557901089417
-0.233025311359662 1
};
\draw (axis cs:0.464839534758957,0.108082528289179) node[
scale=1,
text=black,
rotate=0.0
]{0};
\draw (axis cs:0.232917848079166,-0.999524627199763) node[
scale=1,
text=black,
rotate=0.0
]{1};
\draw (axis cs:-0.464732071478461,-0.108557901089417) node[
scale=1,
text=black,
rotate=0.0
]{2};
\draw (axis cs:-0.233025311359662,1) node[
scale=1,
text=black,
rotate=0.0
]{3};
\end{axis}
\end{tikzpicture}
\end{document}
这应该会产生以下内容:
代替:
我们如何才能使节点标签出现?(在 Python 代码中,我确实指出要使节点标签如第二张图所示出现)
答案1
尝试此代码,在绘制完成后添加节点的内容。
\documentclass{article}
\usepackage{tikz}
\usepackage{tkz-graph}
\usetikzlibrary{fit}
\usetikzlibrary{backgrounds}
\usepackage{pgfplots}
\begin{document}
% This file was created with tikzplotlib v0.10.1.
\begin{tikzpicture} [plotstyle/.style={ scale=1,text=black,rotate=0.0}] % changed <<<<<<<<<<<
\definecolor{darkgray176}{RGB}{176,176,176}
\definecolor{steelblue31120180}{RGB}{31,120,180}
\begin{axis}[
hide x axis,
hide y axis,
scaled x ticks=manual:{}{\pgfmathparse{##1}},
scaled y ticks=manual:{}{\pgfmathparse{##1}},
tick align=outside,
x grid style={darkgray176},
xmajorticks=false,
xmin=-0.56233709013339, xmax=0.562444553413885,
xtick style={color=black},
xticklabels={},
y grid style={darkgray176},
ymajorticks=false,
ymin=-1.20947471305574, ymax=1.20995008585598,
ytick style={color=black},
yticklabels={}
]
\path [draw=black]
(axis cs:0.464839534758957,0.108082528289179)
--(axis cs:-0.464732071478461,-0.108557901089417);
\path [draw=black]
(axis cs:0.464839534758957,0.108082528289179)
--(axis cs:0.232917848079166,-0.999524627199763);
\path [draw=black]
(axis cs:0.464839534758957,0.108082528289179)
--(axis cs:-0.233025311359662,1);
\path [draw=black]
(axis cs:0.232917848079166,-0.999524627199763)
--(axis cs:-0.464732071478461,-0.108557901089417);
\path [draw=black]
(axis cs:-0.464732071478461,-0.108557901089417)
--(axis cs:-0.233025311359662,1);
\addplot [draw=steelblue31120180, fill=steelblue31120180, mark=*, only marks,mark options={scale=3}]
table{%
x y
0.464839534758957 0.108082528289179
0.232917848079166 -0.999524627199763
-0.464732071478461 -0.108557901089417
-0.233025311359662 1
};
\draw (axis cs:0.464839534758957,0.108082528289179) node(0){}; % changed <<<<<<<<<
\draw (axis cs:0.232917848079166,-0.999524627199763) node(1){};
\draw (axis cs:-0.464732071478461,-0.108557901089417) node(2){};
\draw (axis cs:-0.233025311359662,1) node(3) {};
\end{axis}
\draw node[plotstyle] at (0) {0} node[plotstyle] at (1) {1} node[plotstyle] at (2) {2} node[plotstyle] at (3) {3};% added <<<<
\end{tikzpicture}
\end{document}