how to use plt.plot in Yade?

Asked by ehsan benabbas on 2020-01-29

Hello,

I am using Ubuntu 18.04, and Yade 2019-08-08.git-775ae74

I coded a triaxial test which works properly now (thanks to all of you for your helps) and I can get the plots for macro variables such as e22, s22, etc. successfully. However, the plot.plot() function in YADE it is not as flexible as matplotlib(pyplot) in python and I could not find what I want as I wen through the YADE documentation (BTW: the yade-dem.org does not open for me now).

My main problem with plot.plot is I cannot make multiple figures for multiple plots (not subplots).

So, that is why I tried to use plt.plot() to plot e22, s22,... in the way I want but I give error as these variables are defined in a function(addplot history function).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
My code for this part is as follows:

from matplotlib import pyplot as plt

fig1, ax1=plt.subplots(figsize=(15, 10))
fig2, ax2=plt.subplots(figsize=(15, 10))
fig3, ax4=plt.subplots(figsize=(15, 10))
fig3, ax4=plt.subplots(figsize=(15, 10))

ax1.plot(e22,ev,color='k',linestyle='--',marker='D',linewidth=1,label='harchi')
ax1.legend()
ax1.set_title('sample graph')
ax1.set_xlabel('age')
ax1.set_ylabel('salary')
ax1.grid()
fig1.tight_layout()
fig1.savefig('plot1.eps', format='eps')

ax2.plot(i,s22,color='k',linestyle='--',marker='D',linewidth=1,label='harchi')
ax2.legend()
ax2.set_title('sample graph')
ax2.set_xlabel('age')
ax2.set_ylabel('salary')
ax2.grid()
fig2.tight_layout()
fig2.savefig('plot2.eps', format='eps')

ax3.plot(e22,s22,color='k',linestyle='--',marker='D',linewidth=1,label='harchi')
ax3.legend()
ax3.set_title('sample graph')
ax3.set_xlabel('age')
ax3.set_ylabel('salary')
ax3.grid()
fig3.tight_layout()
fig3.savefig('plot3.eps', format='eps')

ax4.plot(i,s11,color='k',linestyle='--',marker='D',linewidth=1,label='harchi')
ax4.legend()
ax4.set_title('sample graph')
ax4.set_xlabel('age')
ax4.set_ylabel('salary')
ax4.grid()
fig4.tight_layout()
fig4.savefig('plot1.eps', format='eps')

plt.show()

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

The error is as follows:

============ RECORD AND PLOT DATA ============
Traceback (most recent call last):
  File "/home/ehsan/yade/install/bin/yade-2019-08-08.git-775ae74", line 336, in runScript
    execfile(script,globals())
  File "/usr/lib/python3/dist-packages/past/builtins/misc.py", line 82, in execfile
    exec_(code, myglobals, mylocals)
  File "test.py", line 295, in <module>
    print(e)
NameError: name 'e22' is not defined

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

P.S: I developed my code based on [1]
[1] https://gitlab.com/yade-dev/trunk/blob/master/examples/triax-tutorial/script-session1.py

Thank you,
Ehsan

Question information

Language:
English Edit question
Status:
Solved
For:
Yade Edit question
Assignee:
No assignee Edit question
Solved by:
ehsan benabbas
Solved:
2020-01-30
Last query:
2020-01-30
Last reply:
2020-01-30
Robert Caulk (rcaulk) said : #1

>> I give error as these variables are defined in a function(addplot history function).
>>NameError: name 'e22' is not defined

addplot is a yade function, not a matplotlib function. Matplotlib does not know e22 exists in a different function in a different library. Hence: 'e22' is not defined. Try defining e22 :-)

Jan Stránský (honzik) said : #2

Hello,

> how to use plt.plot in Yade?

the same way as in any other python script

> My main problem with plot.plot is I cannot make multiple figures for multiple plots (not subplots).

see [1]. a MWE:
###
from yade import plot
plot.addData(a=1,b=2,c=3)
plot.addData(a=2,b=3,c=1)
plot.plots = {
   "a":"b",
   "b": ("a","c"),
   "a ": ("b",None,"c"),
}
plot.plot()
###

> File "test.py", line 295, in <module>

next time please post complete script, here we have no idea why e22 is not defined or what could be wrong

> as these variables are defined in a function(addplot history function).

if you mean something like
plot.addData(e22=something)
then please note that e22 is NOT a variable nor variable name, merely parameter name for addData function.

However, what is passed to plot.addData is stored in plot.data dictionary, so you can access it as
e22 = plot.data["e22"]

cheers
Jan

[1] https://yade-dev.gitlab.io/trunk/user.html#multiple-figures

ehsan benabbas (ehsanben) said : #3

Thank you Jan. You're awesome. That solved my problem and my code is kind of finalized now. I tried to make it nicer and make README file to publish it in Github for others. Thanks again.