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.
The basic logic runs as follows:
- Create two numpy arrays for each machine containing the
Production
values, or None
, depending on the Brand
value.
- This technique keeps the value array lengths equal, thus enabling correct x-axis representation.
- Names:
- (
b1
(Mach. 1, brand 'B'), o1
(Mach. 1, brand 'other'))
- (
b2
(Mach. 2, brand 'B'), o2
(Mach. 2, brand 'other'))
- Loop through machine 1's brands and create named traces for each.
- Loop through machine 2's brands and create named traces for each.
- < Repeat same logic for additional machines ... >
- Plot the graph.
Example code:
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})
Graph:

Comments (TL;DR):
The example code shown here uses the lower-level Plotly API, rather than a convenience wrapper such as graph_objects
to 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).