Home > Software design >  How to add vital few to Pareto Chart in python?
How to add vital few to Pareto Chart in python?

Time:05-31

I use this code that drow

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    data = pd.DataFrame({"Type of defect":["A","B","C","D","E","F","G","Other"], "Count":[17,202,387,25,825,12,3,45]})
    data=data.set_index("Type of defect")
    data = pd.concat([data[data.index!='Other'].sort_values(by='Count',ascending = False), data[data.index=='Прочее']])
    data['Accumulated frequency'] = 100 *data['Count'].cumsum() / data['Count'].sum()
    data['Limit']=80
    data['Vital few']=np.where((data['Limit'] <= data['Accumulated frequency']) & (data['Limit'].shift(1) <= data['Accumulated frequency'].shift(1)), 0, 100)
    fig, axes = plt.subplots()
    ax1 = data.plot(use_index=True, y='Count',  kind='bar', ax=axes)
    ax2 = data.plot(use_index=True, y='Accumulated frequency', marker='D', color="C1", kind='line', ax=axes, secondary_y=True)
    ax2.set_ylim([0,110])
    ax3 = data.plot(use_index=True, y='Limit', color="gray", kind='line', linestyle='dashed', ax=axes, secondary_y=True)
    ax4 = data.plot(use_index=True, y='Vital few', color="yellow", kind='area', ax=axes, secondary_y=True, alpha=0.1)

I get the enter image description here

EDIT: If it is strictly linked to the y-axis value, it can be handled by a bar chart as an alternative method. By increasing the default width, the bars will be connected.

ax4 = data.plot(use_index=True, y='Vital few',color='yellow', kind='bar', width=1.0,ax=axes, secondary_y=True, alpha=0.1)

enter image description here

  • Related