版本:JMP 18.0.1
我在下面包含了 2 个 Python 脚本。这两个脚本都创建了一个图形,该图形被包装在函数内并保存为图像。如果我直接使用 Python Get () 调用图像文件,我可以使用 jmp.run_jsl 打开一个带有图表的窗口。但是,如果我做同样的事情,但出于某种原因将 jmp.run_jsl 包装在 main() 函数中,Python Get() 不起作用,我无法将文件位置的字符串传递到新的图片窗口中。任何帮助都将不胜感激。
你好!
我猜这与变量范围有关。在 testcode_v1.txt 中, plot变量是在函数main()内部定义的,而在 testcode_v2.txt 中, plot变量是在任何限制范围之外定义的,因此是全局变量。显然,尽管在某个内部范围内执行 JSL 脚本,但并不意味着您可以访问在该特定范围内定义的变量。
解决这个问题的最小办法是将绘图变量定义为全局变量。请参见用红色突出显示的部分。
import matplotlib.pyplot as plt
import jmp
plot = None
def show_plot():
fig, ax = plt.subplots()
fruits = ['apple', 'blueberry', 'cherry', 'orange']
counts = [40, 100, 30, 55]
bar_labels = ['red', 'blue', '_red', 'orange']
bar_colors = ['tab:red', 'tab:blue', 'tab:red', 'tab:orange']
ax.bar(fruits, counts, label=bar_labels, color=bar_colors)
ax.set_ylabel('fruit supply')
ax.set_title('Fruit supply by kind and color')
ax.legend(title='Fruit color')
plot = "your_file_path.png"
plt.savefig(plot,format='png')
return plot
def main():
global plot
plot = show_plot()
print(plot)
jmp.run_jsl('''
model = Python Get (plot);
show (model);
plot_model = Open(model, png);
pngJMP = New Window( "Test Model Representation", Picture Box( plot_model ),
<< bring window to front;
);
Python Submit( "plt.close()" );
''')
if __name__ == "__main__":
main()