Thursday, November 23, 2023

Using matplotlib in Python for AutoCAD

 

There’s a couple of options when showing a plot from matplotlib,

1, is to use wxPython. This is nice because you can embed the plot with other controls, you also have the option of making the window modal, or put the plot in a palette.  There’s samples here and here

2, Is you just want to show the plot. Maybe you want to save the plot as a PNG and paste in into AutoCAD as a Raster image.

In this case, all you really need to do is make sure you call show with block=False

import PyRx as Rx
import PyGe as Ge
import PyGi as Gi
import PyDb as Db
import PyAp as Ap
import PyEd as Ed
import traceback

import matplotlib.pyplot as plt
import numpy as np


def PyRxCmd_doit():
    try:
        x = np.linspace(0.1, 2 * np.pi, 41)
        y = np.exp(np.sin(x))
       
        plt.stem(x, y)
        plt.show(block=False) #don't block

    except Exception as err:
        traceback.print_exception(err)
 
 
 



 

No comments:

Post a Comment

TraceBoundary sample in Python for AutoCAD

    import traceback from pyrx_imp import Rx from pyrx_imp import Ge from pyrx_imp import Gi from pyrx_imp import Db from pyrx_imp...