python - 基于 DataFrame 正确标记条形图段
问题描述
我绘制了一个堆积条形图(参见此处:https ://imgur.com/a/ESJeHuF ),由下面的数据框组成。
condition1 condition2 condition3
timestamp
2019-10-30 01:41:43 1.0 4.0 0.0
2019-10-30 01:50:11 1.0 2.0 4.0
2019-10-30 01:50:59 1.0 2.0 4.0
2019-10-30 01:51:36 1.0 2.0 4.0
2019-10-30 01:52:27 1.0 3.0 4.0
2019-10-30 01:53:10 2.0 4.0 0.0
2019-10-31 02:25:14 5.0 0.0 0.0
2019-10-31 04:15:54 5.0 0.0 0.0
我希望条形图中的颜色通过此颜色列表与数据框中的相应值匹配:
color_list = ['r', 'g', 'b', 'm', 'k', 'k']
(例如,如果第二个到最后一个时间步的值为 5,则将堆积条形图的段着色为'k'
,并为堆积条形图列的所有段重复该行为。
下面的代码绘制了堆叠的条形图,但是颜色错误(上面的链接显示了这一点)。它仅将前三种颜色分配给所有值,其中 Dataframe 中有更多对应的颜色/值。正确的绘图应该在 x 轴上有时间戳,并且每个条件的条形段都有正确的颜色。
import matplotlib.pyplot as plt
from matplotlib.cm import ScalarMappable
data_color = [0.,1.,2.,3.,4.,5.] #data range from conditions columns
data_color = [x / max(data_color) for x in data_color]
print(data_color)
custom_map = plt.cm.get_cmap('Accent') #one of the color schemas stored
custom = custom_map(data_color) #mapping the color info to the variable custom
print(custom)
fig = plt.figure()
ax = fig.add_subplot(111)
df.plot.bar(stacked=True, rot=1, legend=False, ax=fig.gca(), color=custom)
我将不胜感激任何帮助,在此先感谢您。
解决方案
推荐阅读
- c# - .Net Microsoft 团队会议 API 生成访问被拒绝令牌
- c++ - 当我把它放在标题中时功能变慢
- java - Selenium 无法根据属性通过 XPath 选择元素
- django - “用户”对象没有属性“get_all_permissions”
- php - Json 和 foreach + 限制为 4 导致循环
- c - 我需要从用户那里获取参数,而不是在命令中硬编码以使用 fork() 同时执行
- python - 如何从没有空格的字符串中删除某些单词?
- parsing - 处理 EBNF 中的空格
- java - Spring Boot 和 Spring 集成。问题
- php - 在同一页面上具有文件输入的多个表单