用X作为DateTime绘制多色线,并通过y更改颜色
在两天的时间里,我试图绘制一条多色系列。我的数据看起来像这样:
gradient result
date
2022-04-15 09:43:20 0.206947 E
2022-04-15 10:25:00 0.102620 E
2022-04-15 11:06:40 0.019450 C
2022-04-15 11:48:20 0.025945 D
2022-04-15 12:30:00 0.022455 D
... ...
2022-05-02 14:13:20 0.003770 A
2022-05-02 14:55:00 0.084120 E
2022-05-02 15:36:40 0.134970 E
2022-05-02 16:18:20 0.261385 E
2022-05-02 17:00:00 0.955833 NaN
我曾经经过以下脚本来绘制图表:
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
from matplotlib.collections import LineCollection
dates = data_646_mean.index
y = data_646_mean.gradient
s = pd.Series(y, index=dates)
fig, ax = plt.subplots()
#convert dates to numbers first
inxval = mdates.date2num(s.index.to_pydatetime())
points = np.array([inxval, s.values]).T.reshape(-1,1,2)
segments = np.concatenate([points[:-1],points[1:]], axis=1)
lc = LineCollection(segments, cmap="plasma", linewidth=3)
# set color to date values
#lc.set_array(inxval)
# note that you could also set the colors according to y values
lc.set_array(s.values())
# add collection to axes
ax.add_collection(lc)
ax.xaxis.set_major_locator(mdates.MonthLocator())
ax.xaxis.set_minor_locator(mdates.DayLocator())
monthFmt = mdates.DateFormatter("%b")
ax.xaxis.set_major_formatter(monthFmt)
ax.autoscale_view()
plt.show()
这已经有效,但是不同的颜色未设置为y:
结果列中的类别。如果不可能,我想给出一定的间隔:
blue = 0.1 - 0.2
red = 0.2 - 0.5
green = 0.5 - 0.9
black = 0.9 1.4
我希望某个人可以帮助我。
For 2 days I am trying to plot a multicolor line. My data looks like this:
gradient result
date
2022-04-15 09:43:20 0.206947 E
2022-04-15 10:25:00 0.102620 E
2022-04-15 11:06:40 0.019450 C
2022-04-15 11:48:20 0.025945 D
2022-04-15 12:30:00 0.022455 D
... ...
2022-05-02 14:13:20 0.003770 A
2022-05-02 14:55:00 0.084120 E
2022-05-02 15:36:40 0.134970 E
2022-05-02 16:18:20 0.261385 E
2022-05-02 17:00:00 0.955833 NaN
I have used to following script to plot the graph:
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
from matplotlib.collections import LineCollection
dates = data_646_mean.index
y = data_646_mean.gradient
s = pd.Series(y, index=dates)
fig, ax = plt.subplots()
#convert dates to numbers first
inxval = mdates.date2num(s.index.to_pydatetime())
points = np.array([inxval, s.values]).T.reshape(-1,1,2)
segments = np.concatenate([points[:-1],points[1:]], axis=1)
lc = LineCollection(segments, cmap="plasma", linewidth=3)
# set color to date values
#lc.set_array(inxval)
# note that you could also set the colors according to y values
lc.set_array(s.values())
# add collection to axes
ax.add_collection(lc)
ax.xaxis.set_major_locator(mdates.MonthLocator())
ax.xaxis.set_minor_locator(mdates.DayLocator())
monthFmt = mdates.DateFormatter("%b")
ax.xaxis.set_major_formatter(monthFmt)
ax.autoscale_view()
plt.show()
This has worked but the different colors are not set within a certain range for y:
And I would really like to make the colors depend on the category in the result column. And if that is not possible I would like to give certain intervals:
blue = 0.1 - 0.2
red = 0.2 - 0.5
green = 0.5 - 0.9
black = 0.9 1.4
I hope somewan could help me out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)