利用matplotlib库中面向对象API,绘制折线图和柱形图

一:添加一个中间气温折线图,并用红色虚线展示该折线图

代码如下:
#最高气温 [32, 33, 34, 34, 33, 31, 30, 29, 30, 29, 26, 23, 21, 25, 31]
#最低气温 [19, 19, 20, 22, 22, 21, 22, 16, 18, 18, 17, 14, 15, 16, 16]

#导入模块
import matplotlib.pyplot as plt
import numpy as np

#准备数据
x = np.arange(4, 19)

#最高气温
y_max = np.array([32, 33, 34, 34, 33, 31, 30, 29, 30, 29, 26, 23, 21, 25, 31])

#最低气温
y_min = np.array([19, 19, 20, 22, 22, 21, 22, 16, 18, 18, 17, 14, 15, 16, 16])

#中间气温
y_med =(y_max + y_min)/2.0

#创建画布并添加绘画区域:
fig = plt.figure( )
ax = fig.add_subplot(111)

ax.plot(x,y_max,
        x,y_min,
       x,y_med,'r--')

#展示图表
plt.show()

运行如图所示:

二、根据下列要求完成柱形图

   1.将现有柱形图颜色设置为橙色。

   2.请在同一个绘图区域内,绘制另一家电商平台的GMV柱形图(用红色),用于比较两家企业的业绩

(另一家电商平台2013年-2019年的GMV: 9770, 14780, 23440, 28920, 38670, 49200, 60270)

   3.柱形图尽量保持美观

代码如下:

#导入模块
import matplotlib.pyplot as plt
import numpy as np

#准备数据
x = np.arange(1,8)
y1 = np.array([10770, 16780, 24440, 30920, 37670, 48200, 57270])

# 准备另一家电商平台的GMV数据
y2 = np.array([9770, 14780, 23440, 28920, 38670, 49200, 60270])

#设置柱形图宽度
bar_width=0.3

#创建画布并添加绘画区域:
fig = plt.figure( )
ax = fig.add_subplot(111)

#添加标签,将现有柱形图颜色设置为橙色
ax.bar(x, y1, tick_label=['FY2013', 'FY2014',
                   'FY2015', 'FY2016',
                   'FY2017', 'FY2018', 'FY2019'], width=bar_width,color='orange')

ax.bar(x+bar_width, y2, width=bar_width,color='red')

#展示图表
plt.show()

运行如图所示:

三、国家统计局给出的网购替代率不一定精准,请为所有条形图添加误差棒,用于展示3%的误差

代码如下:

#导入模块
import matplotlib.pyplot as plt
import numpy as np

# 显示中文
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

#准备数据
x = np.array([0.959, 0.951, 0.935, 0.924, 0.893,
          0.892, 0.865, 0.863, 0.860, 0.856,
          0.854, 0.835, 0.826, 0.816, 0.798,
          0.765, 0.763, 0.67 ])

y = np.arange(1, 19)

#创建画布并添加绘画区域:
fig = plt.figure( )
ax = fig.add_subplot(111)

#添加标签
labels = ["家政、家教、保姆等生活服务", "飞机票、火车票", "家具", "手机、手机配件",
          "计算机及其配套产品", "汽车用品", "通信充值、游戏充值", "个人护理用品",
          "书报杂志及音像制品", "餐饮、旅游、住宿", "家用电器",
          "食品、饮料、烟酒、保健品", "家庭日杂用品", "保险、演出票务",
          "服装、鞋帽、家用纺织品", "数码产品", "其他商品和服务", "工艺品、收藏品"]

#设置误差
error=np.full(18,fill_value=0.03)
ax.barh(y,x,tick_label=labels,height=0.6,xerr =error)

#展示图表
plt.show()

运行如图所示:

四、根据下列要求绘制堆积图

       1. 为A、B、C三家公司的面积图分别设置颜色为:红色,绿色,蓝色

       2.利用stackplot()函数中的labels为每一个独立的面积做好标注             (即展示图例) 

代码如下:

#导入模块
import numpy as np
import matplotlib.pyplot as plt

# 显示中文
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

# 1.准备数据
x = np.arange(1, 13)
y_a = np.array([198, 215, 245, 222, 200, 236, 201, 253, 236, 200, 266, 290])
y_b = np.array([203, 236, 200, 236, 269, 216, 298, 333, 301, 349, 360, 368])
y_c = np.array([185, 205, 226, 199, 238, 200, 250, 209, 246, 219, 253, 288])

# 2.绘制堆积面积图
fig = plt.figure( )
ax = fig.add_subplot(111)

#添加公司标签和颜色标签
labels = ["A公司","B公司","C公司"]
colors = ["red","green","blue"]

ax.stackplot(x,y_a,y_b,y_c, labels =labels, colors =colors)

# 3.在左上角展示标注图例
plt.legend(loc='upper left')

# 4.展示整张图表
plt.show()

运行如图所示:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值