Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Python中创建的绘图导出SVG文件_Python_Matplotlib_Svg_Plot - Fatal编程技术网

从Python中创建的绘图导出SVG文件

从Python中创建的绘图导出SVG文件,python,matplotlib,svg,plot,Python,Matplotlib,Svg,Plot,所以我尝试导出一些我使用matplotlib和seaborn创建的绘图 我使用以下内容创建绘图: import pandas as pd import matplotlib as plt import matplotlib.pyplot as plt from matplotlib import pyplot as plty import seaborn as sns %matplotlib inline from IPython.display import set_matplotlib_fo

所以我尝试导出一些我使用matplotlib和seaborn创建的绘图

我使用以下内容创建绘图:

import pandas as pd
import matplotlib as plt
import matplotlib.pyplot as plt
from matplotlib import pyplot as plty
import seaborn as sns
%matplotlib inline
from IPython.display import set_matplotlib_formats
set_matplotlib_formats('png', 'pdf')

df = pd.read_excel('test.xlsx', sheetname='IvT')

sns.set_style("white")
plt.figure(figsize=(12,10))
plt.xlabel('Test', fontsize=18)
plt.title ('Test', fontsize=22)
#sns.boxplot(df[['Short total']])
sns.boxplot(df[['Short total']])
plt.show()
如果我尝试用

matplotlib.pyplot.savefig("test.svg", format="svg")
我收到一条错误消息说

---------------------------------------------------------------------------NameError回溯(最近的调用) 最后)在() 1. ---->2 matplotlib.pyplot.savefig(“test.svg”,format=“svg”)

NameError:未定义名称“matplotlib”


您似乎使模块的导入过于复杂。在代码中,您已将
matplotlib
matplotlib.pyplot
导入为
plt
。此外,已经导入了
matplotlib.pyplot
,您可以使用matplotlib import pyplot中的
再次尝试导入

当您尝试保存文件时,您已经完成了
matplotlib.pyplot.savefig
,但您已经将
matplotlib.pyplot
导入为
plt

显示的具体错误是,当您导入
matplotlib
本身时,您将其导入为
plt
,这就是为什么错误显示未定义
matplotlib

为了解决此问题,您需要清理导入,如下所示:

import pandas as pd
import matplotlib # if abbreviating this, use "as mpl"
import matplotlib.pyplot as plt

import seaborn as sns
%matplotlib inline
from IPython.display import set_matplotlib_formats
set_matplotlib_formats('png', 'pdf')

df = pd.read_excel('test.xlsx', sheetname='IvT')

sns.set_style("white")
plt.figure(figsize=(12,10))
plt.xlabel('Test', fontsize=18)
plt.title ('Test', fontsize=22)
#sns.boxplot(df[['Short total']])
sns.boxplot(df[['Short total']])
plt.show()
然后,为了保存您的体形,请使用:

plt.savefig("test.svg", format="svg")

请记住在导入模块之前调用此函数。在代码中,您已将
matplotlib
matplotlib.pyplot
导入为
plt
。此外,已经导入了
matplotlib.pyplot
,您可以使用matplotlib import pyplot
中的
再次尝试导入

当您尝试保存文件时,您已经完成了
matplotlib.pyplot.savefig
,但您已经将
matplotlib.pyplot
导入为
plt

显示的具体错误是,当您导入
matplotlib
本身时,您将其导入为
plt
,这就是为什么错误显示未定义
matplotlib

为了解决此问题,您需要清理导入,如下所示:

import pandas as pd
import matplotlib # if abbreviating this, use "as mpl"
import matplotlib.pyplot as plt

import seaborn as sns
%matplotlib inline
from IPython.display import set_matplotlib_formats
set_matplotlib_formats('png', 'pdf')

df = pd.read_excel('test.xlsx', sheetname='IvT')

sns.set_style("white")
plt.figure(figsize=(12,10))
plt.xlabel('Test', fontsize=18)
plt.title ('Test', fontsize=22)
#sns.boxplot(df[['Short total']])
sns.boxplot(df[['Short total']])
plt.show()
然后,为了保存您的体形,请使用:

plt.savefig("test.svg", format="svg")

记住在
plt.show()

之前调用该函数。它是plt.savefig()。此外,您已经将matplotlib和matplotlib.pyplot导入为plt。通常只有后者缩写为pltIt的plt.savefig()。此外,还将matplotlib和matplotlib.pyplot作为plt导入。通常只有后者缩写为PLT感谢您的帮助!我能够导出svg文件,但不幸的是它是空的。你知道为什么会这样吗?我知道这可能有点离题,但你知道我怎样才能得到我的箱线图,用seaborn创建,从水平到垂直?谢谢你的帮助!我能够导出svg文件,但不幸的是它是空的。你知道为什么会这样吗?我知道这可能有点离题,但你知道我怎样才能得到我的箱线图,用seaborn创建,从水平到垂直?