python - Django - save AxesSubplot via seaborn to django models.ImageField
问题描述
I'm trying to save graph from seaborn module as ImageField to django model.
models.py
class HeatmapFiles(models.Model):
username = models.ForeignKey(
'CustomUser',
verbose_name='Username',
on_delete=models.CASCADE,
blank=False
)
heatmap = models.ImageField(
verbose_name='Heatmap',
blank=False
)
I've tried to save via ImageFile:
example_file.py
import io
import seaborn as sns
import matplotlib.pyplot as plt
from django.core.files.images import ImageFile
sns.set()
flights_long = sns.load_dataset("flights")
flights = flights_long.pivot("month", "year", "passengers")
f, ax = plt.subplots(figsize=(9, 6))
new = sns.heatmap(flights, annot=True, fmt="d", linewidths=.5, ax=ax)
figure = io.BytesIO()
new.get_figure().savefig(figure, format='png')
image_file = ImageFile(figure.getvalue())
and image_file objects returns None here:
<ImageFile: None>
I can save it, but that doesn't make sense. How to save "new" heatmap object as django model?
UPD:
after new.get_figure().savefig(figure, format='png')
figure.getvalue() actually HAS data "b'\x89PNG\r\n\x1a\n\x00\x00...." inside.
解决方案
It seems you're defining figure
as empty bytesIO and put that in image_file = ImageFile(...)
, so image_file returns None. Besides, new.get_figure().savefig(...)
save png file separately.
To save "new" heapmap object as django model.
new.get_figure().savefig
save file as you like.Using that file path in
models.ImageField
, then a file will be uploaded to django.
Ps: you need () as get_figure(), I think.
推荐阅读
- swift - 如何使用 audioFileUrls 实现 OperationQueue - Swift - Xcode 11 beta 5
- javascript - 如何使用 Discord.js 限制或授予对 Discord 机器人的访问权限?
- selenium - getWindowHandle() 抛出错误消息“Nullpointerexception”
- javascript - 如何从另一个中间件获取数据?
- python - 用 mock_open 修补函数我哪里出错了?
- go - 有没有办法在切片的所有元素上应用函数?
- sql-server - 为什么 IsNull 会导致除以零错误?
- python-3.x - 我需要使用 spacy 从文本中推荐电影
- azure-devops - Azure - 在负载测试请求中使用动态生成的身份验证令牌
- spring-boot - 由于 jsse.alias_no_key_entry 使用带有 GoDaddy 签名证书的 JKS,SpringBoot 无法启动