电影票房排行:
1.导入numpy模块、pyplot模块,分别取别名为np、plt,并设置中文
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
2.设置电影名称和导入票房数据
labels = ["哪吒之魔童降世","流浪地球","复仇者联盟4:终局之战",
"疯狂的外星人","飞驰人生","烈火英雄","蜘蛛侠:英雄远征",
"速度与激情:特别行动","扫毒2:天地对决","大黄蜂",
"惊奇队长","比悲伤更悲伤的事","哥斯拉2:怪兽之王",
"阿丽塔:战斗天使","银河补习班"]
bar_width = [48.57,46.18,42.05,21.83,17.03,16.70,14.01,13.84,
12.85,11.38,10.25,9.46,9.27,8.88,8.64]
y_data = range(len(labels))
3. 创建代表画布的Figure类的对象fig,在画布fig上添加坐标系风格的绘画区域ax,绘制图表并设置颜色为粉色
fig = plt.figure()
ax = fig.add_subplot(111)
ax.barh(y_data,bar_width,height=0.2,color='pink')
4.设置x轴和y轴的标签
ax.set_xlabel("总票房(亿元)")
ax.set_ylabel("电影名称")
5.设置y轴的刻度线位置、刻度标签,并展示图表
ax.set_yticks(y_data)
ax.set_yticklabels(labels)
plt.show()
展示图表结果如下:
支付宝月账单:
1.导入模块并设置中文
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
2.社长外侧文字说明和导入数据
kinds = ['购物','人情往来','餐饮美食','通信物流',
'生活日用','交通日行','休闲娱乐','其他']
money_scale = [800,100,1000,200,300,200,200,200]
dev_position = [0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1]
3. 创建代表画布的Figure类的对象fig,在画布fig上添加坐标系风格的绘画区域ax,并绘制图表
fig = plt.figure()
ax = fig.add_subplot(111)
ax.pie(money_scale,autopct='%3.1f%%',shadow=True,
explode=dev_position,startangle=90)
4.添加标题和图例,并展示图表
plt.title("支付宝月账单报告")
ax.legend(kinds,loc='upper right',bbox_to_anchor=[1.3,1.1])
plt.show()
展示图表结果如下:
汽车速度与制动距离网格图:
1.导入模块并设置中文
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False
2.准备数据
x_speed = np.arange(10,210,10)
y_distance = np.array([0.5,2.0,4.4,7.9,12.3,17.7,24.1,31.5,
39.9,49.2,59.5,70.8,83.1,96.4,110.7,
126.0,142.2,159.4,177.6,196.8])
3. 创建代表画布的Figure类的对象fig,在画布fig上添加坐标系风格的绘画区域ax,并绘制图表
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(x_speed,y_distance,s=50,alpha=0.9,linewidths=0.3)
4.设置x轴的标签、刻度标签
plt.xlabel('速度(km/h)')
plt.ylabel('制动距离(m)')
plt.xticks(x_speed)
5.显示网格并展示图表
plt.grid(b=True,linewidth=0.3)
plt.show()
展示图表结果如下: