为学生分数柱状图着色
校董会对您之前的可视化很满意,但他们想知道,是否有办法在图上直观地区分成绩好与差的学生。
这是一个运用颜色——尤其是颜色刻度——的好机会。您认为从红色(较差分数)到绿色(较好分数)的刻度很合适。
上一题中用于创建学生分数柱状图的部分代码已提供。
student_scores DataFrame 也可用。您可以在控制台中打印并查看。
本练习是课程的一部分
Python 中的 Plotly 数据可视化入门
练习说明
- 创建一个从红色(
255, 0, 0)到绿色(3, 252, 40)的颜色刻度列表。 - 将
color参数设置为合适的数值列。 - 在图中将
my_scale用作颜色刻度。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Create your own continuous color scale
my_scale = ["rgb(____)", "rgb(____)"]
# Create the bar plot
fig = px.bar(data_frame=student_scores,
x="student_name", y="score", title="Student Scores by Student",
# Set the color variable and scale
color="____",
color_continuous_scale=____
)
# Show the plot
fig.show()