如何根据价值为绘制曲线的不同段着色

发布于 2025-02-06 12:00:10 字数 627 浏览 0 评论 0原文

我正在尝试弄清楚如何根据该线的值绘制数据框列在其中变化,但我一直坚持如何获取这些值。

例如,我有此数据框架:

    Timestamp    Value
-------------------------
0       5:00         4
1       5:01         5
2       5:02         8
3       5:03        12
4       5:04        13
5       5:05         2
...

我简单地运行某种形式的形式:

df.plot(x="Timestamp", y=["Value"], figsize=(9, 8)) 

以随着时间的推移而变化。

现在,我的值分为不同的类别/垃圾箱,其中:0-5 =“ low”,6-15 =“中”和16+ =“高”。我想拥有相同的图,但是现在曲线上的所有值都对应于“低”为蓝色,对应于“中间”为绿色,并且对应于“高”为红色。因此,我会看到绘制了一条曲线,由“值”列组成,但根据价值分割为不同的颜色。我该如何在Python中实现这一目标?我对如何设置一个索引/键感到困惑,以将值转换为颜色以及.plot()中需要哪些参数才能根据值实际具有颜色。

I am trying to figure out how to plot a dataframe column where the color of the line changes based on its value, but I am stuck on how to get these values referenced.

For example, I have this dataframe:

    Timestamp    Value
-------------------------
0       5:00         4
1       5:01         5
2       5:02         8
3       5:03        12
4       5:04        13
5       5:05         2
...

I simple run some form of:

df.plot(x="Timestamp", y=["Value"], figsize=(9, 8)) 

to get a plot of the values changing over time.

Now, I have my values separated into different categories/bins, where: 0-5 = "Low", 6-15 = "Mid" and 16+ = "High". I want to have my same plot, but now with all values on the curve corresponding to "Low" to be blue, corresponding to "Mid" to be green, and corresponding to "High" to be red. So I will see see a single curve plotted, made up from the "Value" column, but segmented into different colors based on value. How can I accomplish this in python? I am confused about how to set up an index/key for translating values to colors and what argument is needed in .plot() to actually have color based on value.

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

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

发布评论

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

评论(1

征﹌骨岁月お 2025-02-13 12:00:10

给定的:

索引时间戳
05:003
15:014
25:025
35:0313
45:0414 14
55:0515
65:0617
75:07 5:0718
85:08 5:0819

然后:

x = df.Timestamp
y = df.Value

for x1,x2, y1,y2 in zip(x, x[1:], y, y[1:]):
  if (0<=y1) & (y1<=5):
    plt.plot([x1, x2], [y1, y2], 'b')
  elif (6<=y1) & (y1<=15):
    plt.plot([x1, x2], [y1, y2], 'g')
  else:
    plt.plot([x1, x2], [y1, y2], 'r')

plt.show()

输出:

“在此处输入图像描述”

Given this:

indexTimestampValue
05:003
15:014
25:025
35:0313
45:0414
55:0515
65:0617
75:0718
85:0819

Then:

x = df.Timestamp
y = df.Value

for x1,x2, y1,y2 in zip(x, x[1:], y, y[1:]):
  if (0<=y1) & (y1<=5):
    plt.plot([x1, x2], [y1, y2], 'b')
  elif (6<=y1) & (y1<=15):
    plt.plot([x1, x2], [y1, y2], 'g')
  else:
    plt.plot([x1, x2], [y1, y2], 'r')

plt.show()

Output:

enter image description here

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