Pyplot 颜色不符合预期

发布于 2024-10-25 22:44:34 字数 1211 浏览 1 评论 0原文

有谁知道为什么这段代码不能正确绘制框颜色。我希望每个组件都有不同的颜色,但它们都是黑色的,带有蓝色图例。

from numpy import array, zeros
import matplotlib.pyplot as plt

# Components: Useage times (start, stop), wattage, detail

COMPONENTS = {"fridge": ( [(0.0,24.0)], 35, " Litres"),
              "kettle": ([(7.3,7.33), (19.0,19.3)], 2500, ""),
              "netbook": ([(8.0,9.3),(12.0,15.0)], 12.5, ""),
              "light bulb": ([(18.0,22.0)], 20, "")
              }
COLORS = ('b','g','r','c','m','y','k','w')
PLOT = []
TIME = range(24*60)

Powers = [] # list of array of power for each component
for key in COMPONENTS.keys(): # each useage
    p = zeros(len(TIME))
    for j in COMPONENTS[key][0]: # start and stop
        start = round(j[0]*60)
        end = round(j[1]*60)
        p[start:end] = COMPONENTS[key][1]
    Powers.append(p)

b=zeros(len(TIME))
for i in range(len(COMPONENTS.keys())):
    PLOT.append(plt.bar(TIME,Powers[i],width = 1, color=COLORS[i], bottom=b))
    b+=Powers[i]


plt.ylabel('Power (W)')
plt.xlabel('Time (h)') ###
plt.title('Power Cycle')
plt.xticks(range(0,25*60,60) ,[str(t) for t in range(25)])
plt.legend( tuple([i for i in PLOT]), tuple([c for c in COMPONENTS.keys()]) )

plt.show()

Does anyone know why this code doesn't plot the boxes colors correctly. I want each component to be a different color but they all come out black with a blue legend.

from numpy import array, zeros
import matplotlib.pyplot as plt

# Components: Useage times (start, stop), wattage, detail

COMPONENTS = {"fridge": ( [(0.0,24.0)], 35, " Litres"),
              "kettle": ([(7.3,7.33), (19.0,19.3)], 2500, ""),
              "netbook": ([(8.0,9.3),(12.0,15.0)], 12.5, ""),
              "light bulb": ([(18.0,22.0)], 20, "")
              }
COLORS = ('b','g','r','c','m','y','k','w')
PLOT = []
TIME = range(24*60)

Powers = [] # list of array of power for each component
for key in COMPONENTS.keys(): # each useage
    p = zeros(len(TIME))
    for j in COMPONENTS[key][0]: # start and stop
        start = round(j[0]*60)
        end = round(j[1]*60)
        p[start:end] = COMPONENTS[key][1]
    Powers.append(p)

b=zeros(len(TIME))
for i in range(len(COMPONENTS.keys())):
    PLOT.append(plt.bar(TIME,Powers[i],width = 1, color=COLORS[i], bottom=b))
    b+=Powers[i]


plt.ylabel('Power (W)')
plt.xlabel('Time (h)') ###
plt.title('Power Cycle')
plt.xticks(range(0,25*60,60) ,[str(t) for t in range(25)])
plt.legend( tuple([i for i in PLOT]), tuple([c for c in COMPONENTS.keys()]) )

plt.show()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一曲爱恨情仇 2024-11-01 22:44:34

就图表而言,您的代码很好。问题是条形太多,所以你只能看到黑色边框。这是放大时的样子:
在此处输入图像描述

要获得正确的图例,请在绘图时传递 label 参数,然后只需完成后,调用不带参数的 plt.legend()

Your code, as far as the graph is concerned, is fine. The problem is that there are too many bars, so you only see the black borders. This is what looks when zooming in:
enter image description here

To get the legend right, pass the label argument while plotting, then just call plt.legend() without arguments when you're done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文