Graphing matplotlib with Python code in a R Markdown document

前端 未结 4 1347
半阙折子戏
半阙折子戏 2021-01-11 09:14

Is it possible to use Python matplotlib code to draw graph in RStudio?

e.g. below Python matplotlib code:

import numpy as np
import matplotlib.pyplot         


        
4条回答
  •  一生所求
    2021-01-11 09:38

    One possible solution is save the plot as a image, then load the file to markdown.

    ### Call python code sample
    ```{r,engine='python'}
    import numpy as np
    import matplotlib.pyplot as plt
    
    n = 256
    X = np.linspace(-np.pi,np.pi,n,endpoint=True)
    Y = np.sin(2*X)
    
    fig, ax = plt.subplots( nrows=1, ncols=1 )
    ax.plot (X, Y+1, color='blue', alpha=1.00)
    ax.plot (X, Y-1, color='blue', alpha=1.00)
    #plt.show()
    fig.savefig('foo.png', bbox_inches='tight')
    print "finished"
    ```
    Output image:
    ![output](foo.png)
    
    #### The End
    

    Output:

提交回复
热议问题