Hi!
I guess it has to do with variable scopes. In testcode_v1.txt plot variable is defined inside the function main() whereas in testcode_v2.txt plot variable is defined outside of any limiting scopes, making it global. Apparently, despite executing JSL script inside some inner scope does not mean you have access to the variables defined in that particular scope.
The minimal effort workaround for this is to define plot variable as a global. See the parts highlighted with red.
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()