preface

Data provided: data

First, draw a pie chart

A pie chart can show the distribution of data

Request for data: Draw pie chart of gross profit ratio of each region to total gross profit (assuming beverage gross profit margin is 25% and non-beverage gross profit margin is 20%)

  1. Load the necessary packages
import pandas as pd
import matplotlib.pyplot as plt
Copy the code
  1. Read the data
path='.. / data/data. CSV '
f=open(path)
data=pd.read_csv(f)
Copy the code
  1. Name of extraction place
place=list(set(data['place'])) # Extraction location
Copy the code
  1. Calculate total gross profit
# Calculate gross beverage and non-beverage revenue
total_income=data.groupby(data['Commodity category'[])'value'].sum()
# Calculate beverage and non-beverage gross profit separately
NonDrink_total_profit=total_income['Not a drink'] *0.2
Drink_total_profit=total_income['drink'] *0.25
# Total gross profit
total_profit=NonDrink_total_profit+Drink_total_profit
Copy the code

The groupby function is often used as a grouping function. If you are not sure about the groupby function, you can refer to this blog: groupby function for details. The following is the total amount calculated by grouping.

  1. Calculate gross profit for each region
labels=list()  # Tags for each area
profit=list()  # Save district profits
for pla in place:
    labels.append(str(pla)+'region')
    place_data=data[data['place']==pla] # Data by region
    # Calculate total beverage and non-beverage revenue for each region separately
    place_income=place_data.groupby(place_data['Commodity category'[])'value'].sum()
    # Calculate beverage and non-beverage gross profit for each region separately
    NonDrink_place_profit=place_income['Not a drink'] *0.2
    Drink_place_profit=place_income['drink'] *0.25
    # Calculate total regional profit
    place_profit=NonDrink_place_profit+Drink_place_profit
    # Save in the list
    profit.append(round(place_profit/total_profit*100.2)) The round function is used to reserve the decimal point
Copy the code
  1. Draw a pie chart
plt.axes(aspect=1) # standard round
plt.pie(x=profit,labels= labels,autopct='% 0.2 f % %')
plt.show() 
Copy the code

As can be seen from the pie chart, the gross profit of region C and region E is relatively large, while the gross profit of region D is the smallest.

Code summary:

import pandas as pd
import matplotlib.pyplot as plt

path='.. / data/data. CSV '
f=open(path)
data=pd.read_csv(f)

place=list(set(data['place'])) # Extraction location

Calculate gross profit
# Calculate gross beverage and non-beverage revenue
total_income=data.groupby(data['Commodity category'[])'value'].sum()
# Calculate beverage and non-beverage gross profit separately
NonDrink_total_profit=total_income['Not a drink'] *0.2
Drink_total_profit=total_income['drink'] *0.25
# Total gross profit
total_profit=NonDrink_total_profit+Drink_total_profit

Calculate gross profit for each region.
labels=list()  # Tags for each area
profit=list()  # Save district profits
for pla in place:
    labels.append(str(pla)+'region')
    place_data=data[data['place']==pla] # Data by region
    # Calculate total beverage and non-beverage revenue for each region separately
    place_income=place_data.groupby(place_data['Commodity category'[])'value'].sum()
    # Calculate beverage and non-beverage gross profit for each region separately
    NonDrink_place_profit=place_income['Not a drink'] *0.2
    Drink_place_profit=place_income['drink'] *0.25
    # Calculate total regional profit
    place_profit=NonDrink_place_profit+Drink_place_profit
    # Save in the list
    profit.append(round(place_profit/total_profit*100.2))
    
"' drawing ' ' '
plt.axes(aspect=1) # standard round
plt.pie(x=profit,labels= labels,autopct='% 0.2 f % %')
plt.show()   
f.close()
Copy the code

Other issues

data

Python drawing Chinese font does not display problems

Data analysis practice – bar chart

Data analysis practice – line chart

Data analysis practice – pie chart

Data analysis – bubble chart

Data analysis actual combat – thermal map

reference

Matplotlib official information