简体   繁体   中英

Plotly: Plot multiple pandas DataFrames in one graph with conditions

I have 4 data frames for 4 different machines(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.

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.

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 . 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). 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('--').

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM