Search code examples
pythonpandasdataframelinechartsmoothing

rather than directly plotting ,need to plot smooth line chart python


i have a 3 df's fro 3 machines(Machine1/Machine2/Machine3) .Each df with 3 columns. Day-shift and production. sample df:

   Day-Shift    Production Quality
    Day 11-01    20         A
    Night 11-01  45         A
    Day 11-02    65         A
    Night 11-02  12         B  
    Day 11-03    97         B

my code:

import numpy as np
import pandas as pd
from plotly.offline import iplot

import plotly.graph_objects as go


# Machine1: Create numpy arrays of values for the given quality.
b1 = np.where(df1['Quality'] == 'A', df1['Production'], None)

# Machine2: Same as above.
b2 = np.where(df2['Quality'] == 'A', df2['Production'], None)

# Machine3: Same as above.
b3 = np.where(df3['Quality'] == 'A', df3['Production'], None)


# Setup.
t = []
line = ['solid']
Quality = ['A']


t.append({'x': df1['Day-Shift'], 
              'y': b1, 
              'name': f'Machine1',
              'line': {'color': 'red', 
                       'dash': line[0]}})


t.append({'x': df2['Day-Shift'], 
              'y': b2,
              'name': f'Machine1',
              'line': {'color': 'blue', 
                       'dash': line[0]}})


t.append({'x': df3['Day-Shift'], 
              'y': b3,
              'name': f'Machine1',
              'line': {'color': 'yellow', 
                       'dash': line[0]}})
    

# Plot the graph.


layout = go.Layout(

    title='Production meterage of Machine1/Machine2/Machine3 for Quality A',
        template='plotly_dark',
        xaxis=dict(
            autorange=True
        ),
        yaxis=dict(
            autorange=True
        )
    )
fig = go.Figure(data=t, layout=layout)
iplot(fig)

Chart I got:

enter image description here

I created one line chart for all three machines. But the line chart looks messy. Need to do smoothing. I tried with gaussian_filter1d. But It does not work for me.


Solution

  • I think the best way of representing your data is with a histogram. I don't know much of ploty ofline module but you can do it (easily) with matplotlib.

    Here is some documentation from matplotlib https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.hist.html

    and an example: https://matplotlib.org/3.1.1/gallery/statistics/hist.html

    and an example with multiply datasets for 1 chart https://matplotlib.org/3.1.1/gallery/statistics/histogram_multihist.html