Python实现三台机器生产数据平滑折线图绘制求助
Got it, let's get those messy lines smoothed out! The problem you ran into with gaussian_filter1d is likely due to the None values in your filtered data (which become NaNs in numpy arrays) — the filter can't handle missing values properly by default. Here's a straightforward fix with step-by-step adjustments to your code:
Step 1: Handle Missing Values & Prepare Data for Smoothing
First, we'll replace None with numpy's native np.nan, then filter out those NaN values to create clean datasets for smoothing. We'll also keep the corresponding Day-Shift labels so our x-axis stays aligned.
Step 2: Apply Gaussian Smoothing
Use gaussian_filter1d from scipy.ndimage on the clean production values. Adjust the sigma parameter to control smoothness: higher values mean smoother lines, lower values keep more of the original data's shape.
Full Modified Code
import numpy as np import pandas as pd from plotly.offline import iplot import plotly.graph_objects as go from scipy.ndimage import gaussian_filter1d # Add this import # Assume df1, df2, df3 are your preloaded DataFrames for each machine # ---------------------- # Process Machine 1 Data # ---------------------- # Filter Quality A production, replace non-A with NaN b1 = np.where(df1['Quality'] == 'A', df1['Production'], np.nan) # Get clean (non-NaN) x and y values mask1 = ~np.isnan(b1) x1_clean = df1['Day-Shift'][mask1] y1_clean = b1[mask1] # Apply Gaussian smoothing (sigma=1 is a good starting point) y1_smoothed = gaussian_filter1d(y1_clean, sigma=1) # ---------------------- # Process Machine 2 Data # ---------------------- b2 = np.where(df2['Quality'] == 'A', df2['Production'], np.nan) mask2 = ~np.isnan(b2) x2_clean = df2['Day-Shift'][mask2] y2_clean = b2[mask2] y2_smoothed = gaussian_filter1d(y2_clean, sigma=1) # ---------------------- # Process Machine 3 Data # ---------------------- b3 = np.where(df3['Quality'] == 'A', df3['Production'], np.nan) mask3 = ~np.isnan(b3) x3_clean = df3['Day-Shift'][mask3] y3_clean = b3[mask3] y3_smoothed = gaussian_filter1d(y3_clean, sigma=1) # ---------------------- # Create Plotly Traces # ---------------------- traces = [] # Fix the name bug (your original code had all traces named 'Machine1') traces.append({ 'x': x1_clean, 'y': y1_smoothed, 'name': 'Machine1', 'line': {'color': 'red', 'dash': 'solid'} }) traces.append({ 'x': x2_clean, 'y': y2_smoothed, 'name': 'Machine2', 'line': {'color': 'blue', 'dash': 'solid'} }) traces.append({ 'x': x3_clean, 'y': y3_smoothed, 'name': 'Machine3', 'line': {'color': 'yellow', 'dash': 'solid'} }) # ---------------------- # Plot the Smoothed 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=traces, layout=layout) iplot(fig)
Key Notes:
- Adjust Smoothness: Tweak the
sigmavalue ingaussian_filter1d(e.g.,sigma=0.5for less smoothing,sigma=2for more). - Fix Trace Names: Your original code had all three traces labeled "Machine1" — I corrected that to make the legend accurate.
- Handling Gaps: If you want to keep gaps in the line where there's no Quality A data (instead of dropping NaNs), you could use linear interpolation before smoothing, but dropping NaNs is simpler for clean, smooth lines where data exists.
内容的提问来源于stack exchange,提问作者user13419531

