我想使用matplotlib的条形图功能绘制以下形式的数据:
data = {'Room A':
{'Shelf 1':
{'Milk': 10,
'Water': 20},
'Shelf 2':
{'Sugar': 5,
'Honey': 6}
},
'Room B':
{'Shelf 1':
{'Wheat': 4,
'Corn': 7},
'Shelf 2':
{'Chicken': 2,
'Cow': 1}
}
}
Run Code Online (Sandbox Code Playgroud)
条形图应该看起来像这样.应从x轴上的标签可见条形图组.有没有办法用matplotlib做到这一点?
我有以下SQLAlchemy设置:
Base = declarative_base()
class Post(Base):
__tablename__ = 'post'
id = Column(Integer, primary_key=True)
title = Column(String(30))
comments = relationship('Comment', cascade='all')
class Comment(Base):
__tablename__ = 'comment'
id = Column(Integer, primary_key=True)
post_id = Column(Integer, ForeignKey(Post.id, ondelete='CASCADE'), nullable=False)
text = Column(Text)
Run Code Online (Sandbox Code Playgroud)
有了这个,我可以用注释的一对多关系创建post对象.我想处理创建和删除帖子的评论而不引用会话.在帖子中添加评论就可以了:
post = Post(title='some post')
comment = Comment(text='some comment')
post.comments.append(comment)
Run Code Online (Sandbox Code Playgroud)
我的会话处理程序只知道帖子,因此它会执行一个操作session.add(post),注释会自动放入会话中,并在下一个数据库中与数据库同步session.commit().但是,删除评论的情况也是如此.我希望能够通过以下方式删除评论:
post.comments.remove(comment)
Run Code Online (Sandbox Code Playgroud)
但是,这会在下一个产生以下错误session.commit():
sqlalchemy.exc.OperationalError: (OperationalError) (1048, "Column 'post_id' cannot be null") 'UPDATE comment SET post_id=%s WHERE comment.id = %s' (None, 1L)
Run Code Online (Sandbox Code Playgroud)
如何告诉SQLAlchemy不使用NULLpost_id 的值更新注释(由于列上的非空约束而不允许),但是删除注释?我知道我可以做session.delete(comment) …