Matplotlib svg as string and not a file

后端 未结 2 1036
无人及你
无人及你 2020-12-14 19:39

I\'d like to use Matplotlib and pyplot to generate an svg image to be used in a Django framework. as of now I have it generating image files that are link to by the page, bu

2条回答
  •  误落风尘
    2020-12-14 19:52

    Here is python3 version

    import matplotlib.pyplot as plt
    import numpy as np
    import io
    
    f = io.BytesIO()
    a = np.random.rand(10)
    plt.bar(range(len(a)), a)
    plt.savefig(f, format = "svg")
    
    print(f.getvalue()) # svg string
    

提交回复
热议问题