如何使用plotlyexpress在x轴上绘制一些事件随时间变化的图表?
我正在尝试使用plotly dash构建一个仪表板,我的数据如下所示:
这里是文本数据:
data={'SECTOR':['KHN','KHN','KHN','KHN','KHN','KHN'],
"NAME": ["ELSILATE","ELSILATE","ELSILATE","ELSILATE","ELSILATE","ELSILATE"],
"TIME" : ["4:00", "4:25","4:45", "5:03", "6:00","7:00"],
"POINT_NAME":["ZERAEIN","ZERAEIN","ZERAEIN","ZERAEIN","ZERAEIN","ZERAEIN"],
"MESSAGE":["Change Status","Operator Control","Return to Normal",
"Operator Control", "Return to Normal","Return to Normal"],
"VALUE":["OPEN","CLOSE","NORMAL","OPEN","NORMAL","CLOSE"],
"ch_open":[1,0,0,0,0,0],
"ch_close":[0,2,0,0,0,0],
"normal_open":[0,0,3,0,0,0],
"command_open":[0,0,0,4,0,0],
"command_close":[0,0,0,0,5,0],
"normal_close":[0,0,0,0,0,6]}
df_cb=pd.DataFrame(data)
我用 pandas 来显示每个事件的数字。 我想显示时间与打开/关闭/正常/控制、关闭等事件的关系。对于每个扇区,名称,adn point_name !
我设法从互联网上的代码中得到这样的
但我想不出一种显示时间的方法在 x 轴
这里是代码:
import matplotlib.pyplot as plt
import numpy as np
#for a specific line cb :
df_ex = df_cb.loc[df_cb['POINT_NAME'].str.contains('ZERAEIN')]
ch_open=list(df_ex["ch_open"])
ch_close=list(df_ex["ch_close"])
normal_open=list(df_ex["normal_open"])
normal_close=list(df_ex["normal_close"])
command_open=list(df_ex["command_open"])
command_close=list(df_ex["command_close"])
data = [ch_open,
ch_close,
normal_open,
normal_close,
command_open,
command_close]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.axes.get_yaxis().set_visible(False)
ax.set_aspect(1)
def avg(a, b):
return (a + b) / 2.0
for y, row in enumerate(data):
for x, col in enumerate(row):
x1 = [x, x+1]
y1 = [0, 0]
y2 = [1, 1]
if col == 1:
plt.fill_between(x1, y1, y2=y2, color='yellow')
plt.text(avg(x1[0], x1[1]), avg(y1[0], y2[0]), "A",
horizontalalignment='center',
verticalalignment='center')
if col == 2:
plt.fill_between(x1, y1, y2=y2, color='red')
plt.text(avg(x1[0], x1[0]+1), avg(y1[0], y2[0]), "B",
horizontalalignment='center',
verticalalignment='center')
if col == 3:
plt.fill_between(x1, y1, y2=y2, color='orange')
plt.text(avg(x1[0], x1[0]+1), avg(y1[0], y2[0]), "C",
horizontalalignment='center',
verticalalignment='center')
if col == 4:
plt.fill_between(x1, y1, y2=y2, color='brown')
plt.text(avg(x1[0], x1[0]+1), avg(y1[0], y2[0]), "D",
horizontalalignment='center',
verticalalignment='center')
if col == 5:
plt.fill_between(x1, y1, y2=y2, color='green')
plt.text(avg(x1[0], x1[0]+1), avg(y1[0], y2[0]), "E",
horizontalalignment='center',
verticalalignment='center')
if col == 6:
plt.fill_between(x1, y1, y2=y2, color='black')
plt.text(avg(x1[0], x1[0]+1), avg(y1[0], y2[0]), "F",
horizontalalignment='center',
verticalalignment='center')
plt.ylim(1, 0)
plt.show()
如果时间显示为 x 轴,那就太好了:
I'm trying to build a dashboard using plotly dash and I have data that looks like this :
Here the text data:
data={'SECTOR':['KHN','KHN','KHN','KHN','KHN','KHN'],
"NAME": ["ELSILATE","ELSILATE","ELSILATE","ELSILATE","ELSILATE","ELSILATE"],
"TIME" : ["4:00", "4:25","4:45", "5:03", "6:00","7:00"],
"POINT_NAME":["ZERAEIN","ZERAEIN","ZERAEIN","ZERAEIN","ZERAEIN","ZERAEIN"],
"MESSAGE":["Change Status","Operator Control","Return to Normal",
"Operator Control", "Return to Normal","Return to Normal"],
"VALUE":["OPEN","CLOSE","NORMAL","OPEN","NORMAL","CLOSE"],
"ch_open":[1,0,0,0,0,0],
"ch_close":[0,2,0,0,0,0],
"normal_open":[0,0,3,0,0,0],
"command_open":[0,0,0,4,0,0],
"command_close":[0,0,0,0,5,0],
"normal_close":[0,0,0,0,0,6]}
df_cb=pd.DataFrame(data)
I used pandas to show a number for every event.
I want to show the time versus the event of open/close/normal/control,close,etc. for every sector, name, adn point_name !
I manage to get it like this
from a code on the internet but I can't think of a way to show time in x-axis
Here is the code:
import matplotlib.pyplot as plt
import numpy as np
#for a specific line cb :
df_ex = df_cb.loc[df_cb['POINT_NAME'].str.contains('ZERAEIN')]
ch_open=list(df_ex["ch_open"])
ch_close=list(df_ex["ch_close"])
normal_open=list(df_ex["normal_open"])
normal_close=list(df_ex["normal_close"])
command_open=list(df_ex["command_open"])
command_close=list(df_ex["command_close"])
data = [ch_open,
ch_close,
normal_open,
normal_close,
command_open,
command_close]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.axes.get_yaxis().set_visible(False)
ax.set_aspect(1)
def avg(a, b):
return (a + b) / 2.0
for y, row in enumerate(data):
for x, col in enumerate(row):
x1 = [x, x+1]
y1 = [0, 0]
y2 = [1, 1]
if col == 1:
plt.fill_between(x1, y1, y2=y2, color='yellow')
plt.text(avg(x1[0], x1[1]), avg(y1[0], y2[0]), "A",
horizontalalignment='center',
verticalalignment='center')
if col == 2:
plt.fill_between(x1, y1, y2=y2, color='red')
plt.text(avg(x1[0], x1[0]+1), avg(y1[0], y2[0]), "B",
horizontalalignment='center',
verticalalignment='center')
if col == 3:
plt.fill_between(x1, y1, y2=y2, color='orange')
plt.text(avg(x1[0], x1[0]+1), avg(y1[0], y2[0]), "C",
horizontalalignment='center',
verticalalignment='center')
if col == 4:
plt.fill_between(x1, y1, y2=y2, color='brown')
plt.text(avg(x1[0], x1[0]+1), avg(y1[0], y2[0]), "D",
horizontalalignment='center',
verticalalignment='center')
if col == 5:
plt.fill_between(x1, y1, y2=y2, color='green')
plt.text(avg(x1[0], x1[0]+1), avg(y1[0], y2[0]), "E",
horizontalalignment='center',
verticalalignment='center')
if col == 6:
plt.fill_between(x1, y1, y2=y2, color='black')
plt.text(avg(x1[0], x1[0]+1), avg(y1[0], y2[0]), "F",
horizontalalignment='center',
verticalalignment='center')
plt.ylim(1, 0)
plt.show()
would be nice to have it like this with time shows as x-axis:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将
TIME
转换为datetime
并稍后使用
shift(-1)
将当前行中下一行的时间设为TIME_END.
它还需要在最后一个
'TIME_END'
而不是NaT
添加一些值,这样我就有了
start
和end
在一排中,我可以用它们来绘制矩形。我还使用
if/else
为不同的VALUE
设置不同的颜色。完整的工作代码:
在 X 轴上,它显示带有日期/日期的时间(因为
TIME
可能位于不同的日期),并且需要更改xticks< /code> 设置不同的文本 - 但是我跳过这个问题。
如果您对不同的
VALUE
使用不同的值y1
y2
那么您可以获得顺便说一句:
我意识到这种类型的图表可以被称为 gantt 并在 Google 中使用这个词我发现了一些有趣的结果
barh< /code> 或
broken_barh
但示例需要将时间转换为天数或秒数并进行更多其他计算。请参阅一些文章 - 但他们可能需要登录门户。
使用 Python 的 Matplotlib 绘制甘特图 |蒂亚戈·卡瓦略 | 作者:蒂亚戈·卡瓦略迈向数据科学
完整代码:https://gist.github.com/Thiagobc23/ad0f228dd8a6b1c9a9e148f17de5b4b0
在 Python 中创建高级甘特图 |通过 Abhijith Chandradas |极客文化|中等
I convert
TIME
todatetime
And late use
shift(-1)
to have time from next row in current row asTIME_END
.It needs also to add some value in last
'TIME_END'
instead ofNaT
This way I have
start
andend
in one row and I can use them to draw rectangles.I also use
if/else
to set different color for differentVALUE
.Full working code:
On X-axis it displays time with date/day (because
TIME
can be in different days) and it would need to changesxticks
to set different text - but I skip this problem.If you use different values
y1
y2
for differentVALUE
then you can getBTW:
Meanwhile I realized that this type of chart can be called gantt and using this word in Google I found some interesting results with
barh
orbroken_barh
but examples needed to convert time to number of days or seconds and make more other calculations.See some articles - but they may need to login to portal.
Gantt charts with Python’s Matplotlib | by Thiago Carvalho | Towards Data Science
Full code: https://gist.github.com/Thiagobc23/ad0f228dd8a6b1c9a9e148f17de5b4b0
Create an Advanced Gantt Chart in Python | by Abhijith Chandradas | Geek Culture | Medium