簡體   English   中英

如何從 matplotlib 中獲取原始 plot 圖像數據? 而不是保存到文件或顯示

[英]How do get the raw plot image data from matplotlib? Instead of saving to file, or displaying

我希望能夠將原始圖像數據發送到其他地方,而無需保存到文件或顯示 plot。 感謝您的幫助!

編輯

字節是我感興趣的。我正在嘗試將字節圖像發送到 web 客戶端站點並從 js/html 顯示它

您可以嘗試使用 canvas 如下:

import matplotlib.pyplot as plt

fig, ax = plt.subplots(1, figsize=(4, 4), dpi=300)
ax.plot([1, 3, 5, 8, 4, 2])

fig.canvas.draw()
temp_canvas = fig.canvas
plt.close()

此時, temp_canvas包含“原始”matplotlib plot。 您可以將其視為原始圖像並在 PIL 等其他庫中使用它,例如您可以 plot 它:

import PIL
pil_image = PIL.Image.frombytes('RGB', temp_canvas.get_width_height(),  temp_canvas.tostring_rgb())
plt.imshow(pil_image)

output 圖像

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM