NetworkX、Scatter、TikzPlotLib.save >>> 意外结果

NetworkX、Scatter、TikzPlotLib.save >>> 意外结果

我使用 python 制作 TEX 文件。python 图形和 TexStudio 生成的 PDF 中的图形不同。为什么?如何解决?

这段 python 代码返回一个图形……

import networkx as nx
import matplotlib.pyplot as plt
import tikzplotlib

f = plt.figure()
G=nx.dodecahedral_graph()
edges=nx.draw_networkx_edges(G,pos=nx.spring_layout(G))

plt.xlim(left = -5)
plt.ylim(bottom = -5)
plt.xlim(right = 5)
plt.ylim(top = 5)

plt.scatter(4,4,10)

filename = "../04_paper/v2/figures/test_0001.tex"
tikzplotlib.save(filename, figurewidth = '\\textwidth')


with open(filename, 'r') as file :
filedata = file.read()
filedata = filedata.replace('marker=', 'mark=')
with open(filename, 'w') as file:
file.write(filedata)

在此处输入图片描述

python 代码还会生成一个 tex 文件。我有一个小文档,其中使用了这个 tex 文件。这是文档...

\documentclass[10pt]{article}
\usepackage{graphicx}
\usepackage{pgf}
\usepackage{tikz}
\usepackage{tikzscale}
\usepackage{pgfplots}
\begin{document}
\begin{center}
    \input{figures/test_0001.tex}
\end{center}
\end{document}

PDF 看起来像这样...

在此处输入图片描述

TEX 文件看起来像这样....

% This file was created by tikzplotlib v0.8.5.
\begin{tikzpicture}

\definecolor{color0}{rgb}{0.12156862745098,0.466666666666667,0.705882352941177}

\begin{axis}[
tick align=outside,
tick pos=left,
width=\textwidth,
x grid style={white!69.01960784313725!black},
xmin=-5, xmax=5,
xtick style={color=black},
y grid style={white!69.01960784313725!black},
ymin=-5, ymax=5,
ytick style={color=black}
]
\path [draw=black]
(axis cs:-0.449869419045689,-0.340116789785537)
--(axis cs:0.166604320932147,-0.461802304323172);

\path [draw=black]
(axis cs:-0.449869419045689,-0.340116789785537)
--(axis cs:-0.778421879619552,-0.661338963091823);

\path [draw=black]
(axis cs:-0.449869419045689,-0.340116789785537)
--(axis cs:-0.490933132460653,0.31086179305115);

\path [draw=black]
(axis cs:0.166604320932147,-0.461802304323172)
--(axis cs:0.367402087957061,-0.917746512767419);

\path [draw=black]
(axis cs:0.166604320932147,-0.461802304323172)
--(axis cs:0.500170347058356,0.0617035911381695);

\path [draw=black]
(axis cs:0.367402087957061,-0.917746512767419)
--(axis cs:-0.249244806794993,-1);

\path [draw=black]
(axis cs:0.367402087957061,-0.917746512767419)
--(axis cs:0.862438543757522,-0.525764709650988);

\path [draw=black]
(axis cs:-0.249244806794993,-1)
--(axis cs:-0.106828666321425,-0.574636278402222);

\path [draw=black]
(axis cs:-0.249244806794993,-1)
--(axis cs:-0.778421879619552,-0.661338963091823);

\path [draw=black]
(axis cs:-0.106828666321425,-0.574636278402222)
--(axis cs:0.489836859592617,-0.310396080328135);

\path [draw=black]
(axis cs:-0.106828666321425,-0.574636278402222)
--(axis cs:-0.493334975922122,-0.0668474818886589);

\path [draw=black]
(axis cs:0.489836859592617,-0.310396080328135)
--(axis cs:0.862438543757522,-0.525764709650988);

\path [draw=black]
(axis cs:0.489836859592617,-0.310396080328135)
--(axis cs:0.451257849459473,0.339898321728359);

\path [draw=black]
(axis cs:0.862438543757522,-0.525764709650988)
--(axis cs:0.988548909684843,0.086531346873156);

\path [draw=black]
(axis cs:0.988548909684843,0.086531346873156)
--(axis cs:0.500170347058356,0.0617035911381695);

\path [draw=black]
(axis cs:0.988548909684843,0.086531346873156)
--(axis cs:0.771524093628576,0.670403541143043);

\path [draw=black]
(axis cs:0.500170347058356,0.0617035911381695)
--(axis cs:0.110458558654231,0.567120826603463);

\path [draw=black]
(axis cs:0.110458558654231,0.567120826603463)
--(axis cs:-0.490933132460653,0.31086179305115);

\path [draw=black]
(axis cs:0.110458558654231,0.567120826603463)
--(axis cs:0.244193107421853,0.997908147098971);

\path [draw=black]
(axis cs:-0.490933132460653,0.31086179305115)
--(axis cs:-0.86537538524157,0.531439446332259);

\path [draw=black]
(axis cs:-0.86537538524157,0.531439446332259)
--(axis cs:-0.36833089642205,0.91611475961209);

\path [draw=black]
(axis cs:-0.86537538524157,0.531439446332259)
--(axis cs:-0.985473024134406,-0.0831182352731083);

\path [draw=black]
(axis cs:-0.36833089642205,0.91611475961209)
--(axis cs:0.244193107421853,0.997908147098971);

\path [draw=black]
(axis cs:-0.36833089642205,0.91611475961209)
--(axis cs:-0.164622492184216,0.459785581930401);

\path [draw=black]
(axis cs:0.244193107421853,0.997908147098971)
--(axis cs:0.771524093628576,0.670403541143043);

\path [draw=black]
(axis cs:0.771524093628576,0.670403541143043)
--(axis cs:0.451257849459473,0.339898321728359);

\path [draw=black]
(axis cs:0.451257849459473,0.339898321728359)
--(axis cs:-0.164622492184216,0.459785581930401);

\path [draw=black]
(axis cs:-0.164622492184216,0.459785581930401)
--(axis cs:-0.493334975922122,-0.0668474818886589);

\path [draw=black]
(axis cs:-0.493334975922122,-0.0668474818886589)
--(axis cs:-0.985473024134406,-0.0831182352731083);

\path [draw=black]
(axis cs:-0.985473024134406,-0.0831182352731083)
--(axis cs:-0.778421879619552,-0.661338963091823);

\addplot [draw=none, mark=*, draw=color0, fill=color0, colormap/viridis]
table{%
x                      y
0 -0.5
0.13260155 -0.5
0.259789935392427 -0.447316845794121
0.353553390593274 -0.353553390593274
0.447316845794121 -0.259789935392427
0.5 -0.13260155
0.5 0
0.5 0.13260155
0.447316845794121 0.259789935392427
0.353553390593274 0.353553390593274
0.259789935392427 0.447316845794121
0.13260155 0.5
0 0.5
-0.13260155 0.5
-0.259789935392427 0.447316845794121
-0.353553390593274 0.353553390593274
-0.447316845794121 0.259789935392427
-0.5 0.13260155
-0.5 0
-0.5 -0.13260155
-0.447316845794121 -0.259789935392427
-0.353553390593274 -0.353553390593274
-0.259789935392427 -0.447316845794121
-0.13260155 -0.5
0 -0.5
0 -0.5
};
\end{axis}

\end{tikzpicture}

为什么 Python 图和 PDF 不同?我该如何解决这个问题?

答案1

如果您替换以下行中的选项:

\addplot [draw=none, mark=*, draw=color0, fill=color0, colormap/viridis]

经过:

\addplot [draw=none]

结果是一样的:

在此处输入图片描述

您还可以调整轴:xmin, xmax, ymin, ymax以获得相同的视图。

相关内容