如何使用 Python 中的 matplotlib 在单个页面上绘制多个图表?
pythonmatplotlibserver side programmingprogramming更新于 2024/9/19 4:28:17
使用 Pandas,我们可以创建一个数据框并创建一个图形和轴。之后,我们可以使用散点图方法来绘制点。
步骤
创建学生列表、他们获得的分数以及每个分数的颜色编码。
使用 Panda 的 DataFrame 和步骤 1 的数据制作数据框。
使用 subplots 方法创建 fig 和 ax 变量,其中默认 nrows 和 ncols 为 1。
使用 plt.xlabel() 方法设置 X 轴标签。
使用 plt.ylabel() 方法设置 Y 轴标签。
*y* 与 *x* 的散点图,标记大小和/或颜色各不相同。
要显示图形,请使用 plt.show()方法。
示例
from matplotlib import pyplot as plt
import pandas as pd
no_of_students = [1, 2, 3, 5, 7, 8, 9, 10, 30, 50]
marks_obtained_by_student = [100, 95, 91, 90, 89, 76, 55, 10, 3, 19]
color_coding = ['red', 'blue', 'yellow', 'green', 'red', 'blue', 'yellow', 'green', 'yellow', 'green']
df = pd.DataFrame(dict(students_count=no_of_students,
marks=marks_obtained_by_student, color=color_coding))
fig, ax = plt.subplots()
plt.xlabel('Students count')
plt.ylabel('Obtained marks')
ax.scatter(df['students_count'], df['marks'], c=df['color'])
plt.show()
输出

相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

