Python Forum

Full Version: Control chart in Python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I need to develop control chart (P chart) in Python with the purpose of highlighting the violating points to end users.

Please suggest.

Regards,
K.Sasidharan
That should be pretty easy to do with matplotlib.
Hi,

Its great to hear, can you please let me know how to use this module to create P chart for sample data as below.
Sample Data:
ID --> Supplier-->Month-->Sample_Size-->Defective
21 --> Supplier1 -->201906 -->10440 --> 4
33 --> Supplier1 -->201907 -->14490 --> 6
44 --> Supplier2 -->201906 -->4199 --> 2
21 --> Supplier3 -->201906 -->7356 --> 1
21 --> Supplier1 -->201908 -->1487 --> 10
71 --> Supplier2 -->201910 -->980 --> 3
81 --> Supplier1 -->201910 -->12056 --> 5
35 --> Supplier1 -->202004 -->1177 --> 12
18 --> Supplier2 -->202001 -->16234 --> 1
123 -->Supplier3 -->201908 -->3800 --> 4
213 -->Supplier1 -->201912 -->15 --> 0
22 --> Supplier2 -->202001 -->6 --> 0
We don't generally write code for people here, we help them fix code that they are working on.
Hi,

Thank you.. sure I understand and here is my code where I used "pyspc" module but it doesnt allow me to specify parameters.
import pandas as pd
from pyspc import *

data = pd.DataFrame(your_sample_data_here)

# selecting first row as header
data.columns = data.iloc[0]
data = data.reindex(data.index.drop(0))

# selecting last two columns
data = data[data.columns[-2:]]

# Creating chart
p = spc(data) + p() + rules()
Then as suggested I used "Matplotlib" shown below
import os
import sys
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
font = {'size': 15}
matplotlib.rc('font', **font)
curdir = !pwd
rootdir = os.path.abspath(curdir[0])
sys.path.append(rootdir)
plt.plot('Data')
plt.axis([Data.Monthkey])
plt.show()
but not sure about P chart option.
Any idea.. I am really struggling with this, need to complete ASAP
Well, it kind of hard to help, you have posted any code that is vaguely runnable, and I'm not familiar with control charts. You would need to plot the data points to get the main line using matplotlib.pyplot.plot. That's pretty simple, you just pass the lists of x and y coordinates as parameters. Then you need to calculate the mean and standard deviation of your data. Numpy can calculate that if you load your data into a numpy array. Then you need to plot lines for the mean and 3*sigma (or whatever you choose), which can be done with matplotlib.pyplot.hlines (see this Stack Overflow question).