Python 数据可视化

 绘制直线图 

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
y = [4, 5, 3, 7, 5, 7, 8, 9, 6]

x1 = [3, 4, 9, 3, 7, 6, 7, 10, 9]
y1 = [6, 5, 8, 7, 5, 7, 10, 9, 11]

plt.xlabel('Mount')
plt.ylabel('Sales')
plt.title('Sales of Company \n Company A and Company B')

plt.plot(x, y, label='Company A')
plt.plot(x1, y1, label='Company B')
plt.legend()  # 区分两条线
plt.show()
from matplotlib import pyplot as plt

plt.rcParams['font.sans-serif'] = ['FangSong']
x_dates = ['1-26', '1-27', '1-28', '1-29', '1-30']
y_comfirmed = [2066, 2844, 4630, 6086, 7830]
y_death = [56, 81, 106, 132, 170]
# 图示的名字
plt.figure('新冠肺炎疫情统计图表', facecolor='#f0f0f0', figsize=(10, 8))
# 图表的名字
plt.title('疫情曲线', fontsize=10)
# 绘制
plt.plot(x_dates, y_comfirmed, label='确诊')
plt.plot(x_dates, y_death, label='死亡')
plt.xlabel('时间')
plt.ylabel('数据')
plt.legend()
plt.show()

直方图

# 直方图
import matplotlib.pyplot as plt

Citt_A_Age = [1, 2, 3, 8, 9, 14, 15, 18, 19, 21, 22, 24, 28, 27, 29, 30, 36, 34, 37, 39, 37, 40, 46, 48, 47, 50, 58, 60, 69, 75, 85, 60, 90, 100]

bins = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110]

plt.hist(Citt_A_Age, bins,histtype='bar', rwidth=0.8)

plt.xlabel('Age')
plt.ylabel('Num')
plt.title('The City Hist')
plt.show()

散点图

# 散点图
import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [1, 2, 1.5, 2.9, 4.7, 6.6, 7.5, 6, 8.6, 7.1]

plt.scatter(x, y, label='scatter_plot', color='b', marker='*')

plt.xlabel('x')
plt.ylabel('y')
plt.title('A scater plot')
plt.legend()
plt.show()

饼图

# 饼图
import matplotlib.pyplot as plt

sales = [1.8, 2.3, 3.6, 7.8]
company_names = ['Company A', 'Company B', 'Company C', 'Company D']
colors_1 = ['azure', 'lavender', 'pink', 'aqua']

plt.pie(sales,
        labels=company_names,
        colors=colors_1,
        startangle=90,
        shadow=True,
        autopct='%1.2f%%',
        explode=(0.2, 0, 0, 0))
plt.show()

横向条形图

from matplotlib import pyplot as plt

plt.rcParams['font.sans-serif'] = ['FangSong']
x_provinces = ['湖北', '湖南', '浙江', '河南']
y_dara_confirmed = [13522, 593, 675, 829]
colors = ['red', 'blue', 'pink', 'green']
# 取出有几个数据几个数据画几个图, 对应的具体数据,对应x轴的显示
plt.barh(range(len(y_dara_confirmed)), y_dara_confirmed, tick_label=x_provinces, color = colors)
plt.show()

条形图

from matplotlib import pyplot as plt
# 条形图
plt.rcParams['font.sans-serif'] = ['FangSong']
x1 = [1, 3, 5, 7, 9]
y1 = [4, 7, 5, 2, 1]

x2 = [2, 4, 6, 8, 10]
y2 = [8, 7, 9, 11, 6]

# 绘制数据
plt.bar(x1, y1, label='A')
plt.bar(x2, y2, label='B')
# 设置标签内容
plt.xlabel('x轴')
plt.ylabel('y轴')
# 显示
plt.legend()
plt.show()

堆叠柱状图

import matplotlib.pyplot as plt
# 堆叠柱状图
# 设置中文
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
# 数据
x_date = ['2007', '2008', '2009', '2012', '2013', '2014']
y_data_beijing = [58000, 59000, 62000, 71000, 84000, 90000]
y_data_shanghai = [52000, 54000, 51500, 58000, 48000, 95000]
# 绘图
plt.bar(x=x_date, height=y_data_beijing, label='北京', color='red', alpha=0.8)
plt.bar(x=x_date, height=y_data_shanghai, label='上海', color='green', alpha=0.8)
# 在图上标记一下数据 enumerate获取列表中两个数据,x索引,y值
# ha 表示水平对齐方式,va表示垂直对齐方式
for x, y in enumerate(y_data_beijing):
    plt.text(x, y+100, '%s' % y, ha='center', va='bottom')
for x, y in enumerate(y_data_shanghai):
    plt.text(x, y+100, '%s' % y, ha='center', va='top')
# 设置标题
plt.title('北京VS上海')
# 设置坐标轴的说明
plt.xlabel('日期')
plt.ylabel('数量')
plt.legend()
plt.show()

水平柱状图

import matplotlib.pyplot as plt
import numpy as np
# 水平柱状图
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
# 构建数据
x_data = ['02-06', '02-07', '02-08', '02-09', '02-10']
y_data_beijing = [274, 297, 315, 326, 337]
y_data_shanghai = [254, 269, 281, 292, 295]
y_data_guangzhou = [225, 284, 298, 304, 313]
y_data_shenzhen = [314, 334, 351, 364, 368]

# 设置柱状图的宽度
bar_width = 0.2
# 数据绘制
# y轴使用range(len (x_data)),显示刻度 0,1,2    alpha 渐变
plt.barh(y=range(len(x_data)), width=y_data_beijing, label='北京', color='r', alpha=0.8, height=bar_width)
plt.barh(y=np.arange(len(x_data))+bar_width, width=y_data_shanghai, label='上海', color='g', alpha=0.8, height=bar_width)
plt.barh(y=np.arange(len(x_data))+bar_width*2, width=y_data_guangzhou, label='广州', color='b', alpha=0.8, height=bar_width)
plt.barh(y=np.arange(len(x_data))+bar_width*3, width=y_data_shenzhen, label='深圳', color='y', alpha=0.8, height=bar_width)
#在柱状图上显示数据
for y, x in enumerate(y_data_beijing):
    plt.text(x, y, '%s' % x, ha='center', va='top')
for y, x in enumerate(y_data_shanghai):
    plt.text(x, y+bar_width, '%s' % x, ha='center', va='top')
for y, x in enumerate(y_data_guangzhou):
    plt.text(x, y+bar_width*2, '%s' % x, ha='center', va='top')
for y, x in enumerate(y_data_shenzhen):
    plt.text(x, y+bar_width*3, '%s' % x, ha='center', va='top')
# y轴设置刻度
plt.yticks(np.arange(len(x_data))+bar_width/2, x_data)
# 设置标题
plt.title('北上广深数据对比')
# 设置坐标轴名称
plt.xlabel('确认病例')
plt.ylabel('日期')
plt.legend()
plt.show()

饼状图

import matplotlib.pyplot as plt
# 设置中文
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
# 湖北数据
cities = ['武汉市', '黄石市', '十堰市', '宜昌市', '襄阳市', '而州市', '荆门市', '孝感市', '荆州市', '黄冈市', '咸宁市', '恩施', '仙桃市', '潜江市', '天门市', '随州', '神农佳林区']
# 2020年2月12日确诊数据
confirmed_persons = [32994, 911, 562, 810, 1101, 1065, 927, 2847, 1160, 2662, 534, 229, 480, 94, 362, 32, 10]
# 各市数据2015年
persons = [10610000, 2460000, 3380000, 4120000, 5610000, 1060000, 2900000, 4880000, 5710000, 6300000, 2510000, 3320000, 1150000, 960000, 1290000, 2190000, 80000]
# 绘制饼状图
# plt.pie(persons, labels=cities)
# 在一个图示中绘制多个图一行两列子图分布
fig, axs = plt.subplots(1, 2, figsize=(40, 6))
# 第一列数据和标题
axs[0].pie(persons, labels=cities)
axs[0].set_title('2015年各城市数据')
# 第二行的数据和标题  startangle:倾斜角度
axs[1].pie(confirmed_persons, labels=cities, startangle=45, textprops={'fontsize':5})
axs[1].set_title('2020-02-12确认病例')
plt.legend()
plt.show()

你可能感兴趣的:(python,python)