[英]Plotly: Plot multiple pandas DataFrames in one graph with conditions
I have 4 data frames for 4 different machines(df1,df2,df3,df4).我有 4 个数据帧用于 4 台不同的机器(df1、df2、df3、df4)。 All of the data frame's columns are the same(same column names/features).
数据框的所有列都相同(相同的列名/特征)。
sample of the df1 is given below.下面给出了 df1 的示例。
Day-Shift Brand Production
Day 06-26 A 438
Night 06-26 A 215
Day 06-27 B 120
Night 06-27 B 180
Day 06-28 B 500
sample of the df2 is given below.下面给出了 df2 的示例。
Day-Shift Brand Production
Day 06-26 B 138
Night 06-26 B 205
Day 06-27 E 200
Night 06-27 E 190
Day 06-28 C 100
But all of the machine's data frames Brand name includes Brand called B .但是机器的所有数据帧 Brand name 都包括Brand 称为B 。 What I want to do is plot line charts for these 3 data frames in one graph ( x axes as Day-shift , y axis as production and color with Brand).
我想要做的是在一张图中绘制这 3 个数据框的折线图(x 轴作为 Day-shift ,y 轴作为生产和颜色与 Brand)。 Need to give a fixed color for each machine ( ex: df1(machine 1) using red ) and for all the machines, Brand B should be visualized as a solid line('-') and all the other brands (ex: A, C, D, E and etc) as a dashed line('--').
需要为每台机器(例如:df1(machine 1) using red)和所有机器提供固定颜色,品牌 B应可视化为实线('-')和所有其他品牌(例如:A, C、D、E 等)作为虚线('--')。
I plotted different line charts in one graph.我在一张图中绘制了不同的折线图。 But I have no idea how to plot like above way.
但我不知道如何像上面那样绘制。
In the example below, each machine is plotted in its own colour, with brand "B" being plotted in a solid line, while other brands are plotted in dashed lines, per the requirements.在下面的示例中,每台机器都以自己的颜色绘制,品牌“B”用实线绘制,而其他品牌则按照要求用虚线绘制。
The basic logic runs as follows:基本逻辑如下:
Production
values, or None
, depending on the Brand
value.Production
值或None
每台机器创建两个 numpy 数组,具体取决于Brand
值。
b1
(Mach. 1, brand 'B'), o1
(Mach. 1, brand 'other')) b1
(马赫. 1, 品牌'B'), o1
(马赫. 1, 品牌'其他'))b2
(Mach. 2, brand 'B'), o2
(Mach. 2, brand 'other')) b2
(Mach. 2, 品牌'B'), o2
(Mach. 2, 品牌'other'))import numpy as np
import pandas as pd
from plotly.offline import iplot
# Copied datasets from SO question.
df1 = pd.read_clipboard()
df2 = pd.read_clipboard()
# Machine 1: Create numpy arrays of values for the given brand.
b1 = np.where(df1['Brand'] == 'B', df1['Production'], None)
o1 = np.where(df1['Brand'] != 'B', df1['Production'], None)
# Machine 2: Same as above.
b2 = np.where(df2['Brand'] == 'B', df2['Production'], None)
o2 = np.where(df2['Brand'] != 'B', df2['Production'], None)
# Setup.
t = []
line = ['solid', 'dash']
brand = ['B', 'Other']
# Machine 1: Create traces for brand B and Other.
for i, Y in enumerate([b1, o1]):
t.append({'x': df1['Day-Shift'],
'y': Y,
'name': f'Machine 1: {brand[i]}',
'line': {'color': 'red',
'dash': line[i]}})
# Machine 2: Create traces for brand B and Other.
for i, Y in enumerate([b2, o2]):
t.append({'x': df2['Day-Shift'],
'y': Y,
'name': f'Machine 2: {brand[i]}',
'line': {'color': 'blue',
'dash': line[i]}})
# Plot the graph.
iplot({'data': t})
The example code shown here uses the lower-level Plotly API, rather than a convenience wrapper such as graph_objects
to express
.此处显示的示例代码使用较低级别的 Plotly API,而不是像
graph_objects
这样的便利包装器来express
. The reason is that I (personally) feel it's helpful to users to show what is occurring 'under the hood', rather than masking the underlying code logic with a convenience wrapper.原因是我(个人)觉得展示“幕后”发生的事情对用户有帮助,而不是用方便的包装器掩盖底层代码逻辑。
This way, when the user needs to modify a finer detail of the graph, they will have a better understanding of the list
s and dict
s which Plotly is constructing for the underlying graphing engine (orca).这样,当用户需要修改图形的更精细细节时,他们将对 Plotly 为底层图形引擎(orca)构建的
list
和dict
有更好的理解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.