版本:JMP 18.0.1
我在下麵包含了 2 個 python 腳本。兩者都創建了一個包含在函數內的圖形並將其保存為圖像。如果我直接使用 Python Get () 呼叫圖像文件,我可以使用 jmp.run_jsl 打開包含圖表的視窗。但是,如果我做同樣的事情,但由於某種原因將jmp.run_jsl 包裝在main() 函數中,則Python Get() 不起作用,並且我無法將文件位置的字串傳遞到新圖片視窗中。任何幫助將不勝感激。
你好!
我想這與變數範圍有關。在 testcode_v1.txt 中,繪圖變數在函數main()內定義,而在 testcode_v2.txt 中,繪圖變數在任何限制範圍之外定義,使其成為全域變數。顯然,儘管在某些內部作用域內執行 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()