我一直在尝试使用 C# Visual Studio 执行以下 Python 代码(graphcreater.py)。我通过 NuGet 包管理器添加了 IronPyton 2.7.7 和 IronPython.StdLib 2.7.7。
一旦我运行程序,它就会给出一个异常,
没有名为 mpl_toolkits.mplot3d 的模块
我需要弄清楚如何在 Python 代码(graphcreater.py)中正确导入 mpl_toolkits 模块。
注意:graphcreater.py 仅在使用 Python 执行时运行。
Python 代码(graphcreater.py):
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
fig = plt.figure()
xs = np.array([ 0,1,2,2,1,1,0]);
ys = np.array([ 0,0,0,2,2,3,3]);
zs = np.array([3 ,0, -1, 6, 2, 1,4]);
ax=fig.add_subplot(1,1,1, projection='3d')
ax.grid(True)
ax.plot_trisurf(xs, ys, zs,cmap=cm.coolwarm,linewidth=0.2, antialiased=True)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
# Add a color bar which maps values to colors
# fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
C#代码:
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
namespace graphCreator
{
class Program
{
static void Main(string[] args)
{
ScriptEngine engine = Python.CreateEngine();
engine.ExecuteFile(@"graphcreater.py");
}
}
}