营收直方图子图
您之前为纽约证券交易所的公司创建了按行业着色的营收直方图(堆叠柱形),这张图很有启发性,因为它展示了各行业在直方图中的大致分布位置。
不过,公司还想在不依赖悬停查看的情况下,直接看到每个行业自身的分布。上一张直方图把太多信息放在一张图里,不利于这种分析,但他们也不想分成多张独立的图。如何解决这个难题?
您的任务是创建一个按行业划分的公司营收直方图,将其做成堆叠的子图布局,并共享 x 轴,以便对不同行业进行有意义的比较。
已为您加载 revenues DataFrame。
本练习是课程的一部分
Python 中的 Plotly 数据可视化入门
练习说明
- 创建一个 3 行 1 列的子图网格,并共享 x 轴。
- 遍历选定的行业,为每个行业添加一个直方图轨迹。
- 将每个轨迹放到子图网格中对应的行。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Create the subplots
fig = make_subplots(rows=____, cols=____, ____=True,
subplot_titles=['Tech Industry Revenue',
'Retail Industry Revenue',
'Professional Services Industry Revenue'])
# Loop through the industries
row_num = 1
for industry in ['Tech', 'Retail', 'Professional Services']:
df = revenues[revenues.Industry == industry]
# Create a histogram
hist = px.____(df, x='Revenue')
# Add the trace to the subplot
fig.____(hist.data[0],
# Position the trace
row=____, col=1)
row_num +=1
# Show the plot
fig.show()