import matplotlib.pyplot as pltimport numpy as np# 构造数据movies = ['蜘蛛侠', '速10', '人生路不熟']days = ['Day 1', 'Day 2', 'Day 3']ticket_sales = np.array([[983.45, 1000.35, 1297.4], [180.73, 248.76, 573.83], [86.35, 249.5, 352.13]])# 绘制柱状图(展示三部电影近三天票房总和)total_sales = np.sum(ticket_sales, axis=1)plt.barh(movies, total_sales)plt.title('Three Movies Total Ticket Sales in Three Days')plt.xlabel('Ticket Sales')plt.ylabel('Movies')plt.show()# 绘制柱状图(展示三部电影近三天的票房)plt.barh(movies, ticket_sales[:,0], label=days[0])plt.barh(movies, ticket_sales[:,1], left=ticket_sales[:,0], label=days[1])plt.barh(movies, ticket_sales[:,2], left=np.sum(ticket_sales[:,0:2], axis=1), label=days[2])plt.title('Three Movies Ticket Sales in Three Days')plt.xlabel('Ticket Sales')plt.ylabel('Movies')plt.legend()plt.show()4.要求在同一张表格提现,并且全部用中文显示
时间: 2024-01-20 22:04:11 浏览: 228
import matplotlib.pyplot as plt
import numpy as np
# 设置中文字体
plt.rcParams['font.family'] = ['SimHei']
# 构造数据
movies = ['蜘蛛侠', '速10', '人生路不熟']
days = ['第一天', '第二天', '第三天']
ticket_sales = np.array([[983.45, 1000.35, 1297.4], [180.73, 248.76, 573.83], [86.35, 249.5, 352.13]])
# 绘制柱状图(展示三部电影近三天票房总和)
total_sales = np.sum(ticket_sales, axis=1)
plt.subplot(1, 2, 1)
plt.barh(movies, total_sales)
plt.title('三部电影近三天总票房')
plt.xlabel('票房')
plt.ylabel('电影')
# 绘制柱状图(展示三部电影近三天的票房)
plt.subplot(1, 2, 2)
plt.barh(movies, ticket_sales[:,0], label=days[0])
plt.barh(movies, ticket_sales[:,1], left=ticket_sales[:,0], label=days[1])
plt.barh(movies, ticket_sales[:,2], left=np.sum(ticket_sales[:,0:2], axis=1), label=days[2])
plt.title('三部电影近三天的票房')
plt.xlabel('票房')
plt.ylabel('电影')
plt.legend()
# 显示图像
plt.show()
阅读全文