Python头歌实训之matplotlib数据可视化

1.各省gdp的和生成条状图

import pandas
import matplotlib
matplotlib.use('Agg')
matplotlib.rcParams['font.family']='SimHei'
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
import matplotlib.pyplot as plt
df = pandas.read_excel("test/各省GDP.xlsx",dtype={"年份":str})
#代码开始
x = list(df['年份'].unique())
y = list(df.groupby(['年份'])['GDP'].sum())
plt.figure(figsize=(10, 10))
plt.title('GDP条状图')
plt.bar(x, height=y)
#代码结束
plt.savefig("image1/gdptxt.jpg")

2.各省银行数量绘制饼图

import pandas
import matplotlib
matplotlib.use("agg")
matplotlib.rcParams['font.family']='SimHei'
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
import matplotlib.pyplot as plt
df = pandas.read_excel("test/银行信息.xlsx")
#代码开始
plt.figure(figsize=(10,10))
x = df.groupby(['省份'])['银行编号'].count()
x = pandas.DataFrame(x).reset_index()
l1 = []
for i in x['省份']:
    l1.append(i[:2])
l2 = list(x['银行编号'])
plt.pie(l2, labels=l1)
plt.title('银行省份分布图')
#代码结束
plt.savefig("image2/yhbt.jpg")

3.各类银行数量绘制折线图

import pandas
import matplotlib
matplotlib.use("Agg")
matplotlib.rcParams['font.family']='SimHei'
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
import numpy as np
import matplotlib.pyplot as plt
df = pandas.read_excel("test/银行信息.xlsx")
#代码开始
df1 = df.groupby(['银行种类'])['银行编号'].count()
df2 = pandas.DataFrame(df1).reset_index().sort_values(by=['银行编号'], ascending=False)
a, b = 0, 0
l1, l2 = [], []
for i in df2['银行种类']:
    a += 1
    l1.append(i)
    if a == 10:
        break
for i in df2['银行编号']:
    b += 1
    l2.append(i)
    if b == 10:
        break
plt.figure(figsize=(10, 10))
plt.plot(l1, l2)
plt.title('银行种类折线图')
#代码结束
plt.savefig("image3/yhzxt.jpg")

4.各日超市销售金额绘制折线图

import matplotlib
matplotlib.use('Agg')
matplotlib.rcParams['font.family']='SimHei'
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
import matplotlib.pyplot as plt
import pandas as pd
import datetime
df = pd.read_excel("tbsc/step1/统计数据.xlsx",sheet_name="日期统计")
#代码开始
l1, l2 = list(df['日期']), list(df['合计金额'])
plt.figure(figsize=(10, 14))
plt.title('日期销售')
plt.xlabel('日期')
plt.ylabel('金额')
plt.xlim(datetime.date(2000,5,20),datetime.date(2000,7,18))
plt.gca().xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%Y-%m-%d'))
plt.xticks(pd.date_range('2000-5-20','2000-7-18'), rotation=90)
plt.ylim(0, 1800)
plt.plot(l1, l2)
#代码结束
plt.savefig("image4/rqzxt.jpg")

5.四种类别的销售数量合计最高的三种商品绘制多个条形图

import matplotlib
matplotlib.use('Agg')
matplotlib.rcParams['font.family']='SimHei'
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
#代码开始
x = ['烟', '饮料', '零食', '酒']
l = np.linspace(0,1,3)
t = 0
plt.figure(figsize=(10,14))
for i in x:
    t += 1
    df = pd.read_excel("tbsc/step2/类别销售.xlsx",sheet_name=i)
    s = df.groupby(['商品名称'])['数量'].sum()
    s = s.reset_index()
    s = s.sort_values(by=['数量'], ascending=False)
    a, b, c, d = [], [], 0, 0
    for j in s['商品名称']:
        j = j.strip()
        a.append(j)
        c += 1
        if c == 3:
            break
    for k in s['数量']:
        b.append(k)
        d += 1
        if d == 3:
            break
    plt.subplot(2,2,t)
    plt.title(i)
    plt.bar(x=l[0], height=b[0], width=0.2, label=a[0])
    plt.bar(x=l[1], height=b[1], width=0.2, label=a[1])
    plt.bar(x=l[2], height=b[2], width=0.2, label=a[2])
    plt.legend(a)
    plt.xticks(l,[1,2,3])
    for m,n in zip(l, b):
        plt.text(m,n+1,n)
#代码结束
plt.savefig("image5/lbzxt.jpg")
plt.show()

6.销售各类别数据绘制饼图

import matplotlib
matplotlib.use('Agg')
matplotlib.rcParams['font.family']='SimHei'
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_excel("tbsc/step3/统计数据.xlsx",sheet_name="类别统计")
n = len(df['类别'].unique())
x = sum(df['合计金额'])/5/n
a, b = [], []
#代码开始
s = 0
for i, j in zip(df['类别'], df['合计金额']):
    if j >= x:
        a.append(i)
        b.append(j)
    else:
        s += j
a.append('其他')
b.append(s)
plt.figure(figsize=(10,10))
plt.title('各类别销售分布')
plt.pie(b, labels=a, autopct='%1.2f%%')
#代码结束
plt.savefig("image6/lbbt.jpg")

你可能感兴趣的:(头歌实训,python)