Error in user YAML: (<unknown>): found a tab character that violate indentation while scanning a plain scalar at line 3 column 3
---
- oeasy Python 0823
- 这是 oeasy 系统化 Python 教程,从基础一步步讲,扎实、完整、不跳步。愿意花时间学,就能真正学会。
- 本教程同步发布在:
- 个人网站: `https://oeasy.org`
- 蓝桥云课: `https://www.lanqiao.cn/courses/3584`
- GitHub: `https://github.com/overmind1980/oeasy-python-tutorial`
- Gitee: `https://gitee.com/overmind1980/oeasypython`
---这是一段示例实验介绍。
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# 创建示例数据
n_bubbles = 30
x = np.random.rand(n_bubbles) # X轴数据
y = np.random.rand(n_bubbles) # Y轴数据
z = np.random.rand(n_bubbles) # Z轴数据
colors = np.random.rand(n_bubbles) # 用于颜色映射的值
sizes = 1000 * np.random.rand(n_bubbles) # 气泡大小
# 创建3D图形
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')
# 绘制气泡图
scatter = ax.scatter(x, y, z,
c=colors, # 颜色映射值
s=sizes, # 气泡大小
alpha=0.6, # 透明度
cmap='viridis') # 颜色映射方案
# 添加颜色条
plt.colorbar(scatter)
# 设置坐标轴标签
ax.set_xlabel('X轴')
ax.set_ylabel('Y轴')
ax.set_zlabel('Z轴')
# 设置标题
plt.title('3D气泡图示例')
# 显示图形
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# 示例数据:电影分析
movies = ['电影A', '电影B', '电影C', '电影D', '电影E']
box_office = np.array([2.5, 1.8, 3.2, 1.2, 4.0]) # 票房(亿)
ratings = np.array([8.5, 7.2, 8.8, 6.5, 9.0]) # 评分
comments = np.array([10000, 5000, 15000, 3000, 20000]) # 评论数
budget = np.array([5000, 3000, 8000, 2000, 10000]) # 制作预算(万)
# 创建3D图形
fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(111, projection='3d')
# 绘制气泡图
scatter = ax.scatter(box_office, ratings, comments,
s=budget/30, # 气泡大小根据预算确定
c=ratings, # 颜色根据评分确定
alpha=0.6,
cmap='viridis')
# 添加颜色条
plt.colorbar(scatter, label='评分')
# 设置坐标轴标签
ax.set_xlabel('票房(亿)')
ax.set_ylabel('评分')
ax.set_zlabel('评论数')
# 设置标题
plt.title('电影数据分析')
# 为每个气泡添加电影名称标签
for i, movie in enumerate(movies):
ax.text(box_office[i], ratings[i], comments[i], movie)
plt.show()
- 本文来自 oeasy Python 系统教程。
- 想完整、扎实学 Python,
- 搜索 oeasy 即可。
