LoslegenKostenlos loslegen

Revenue histogram subplots

The revenue histogram with colors by industry (with stacked bars) you created for the New York Stock Exchange firm was enlightening, as it showed which industries tended to be in which area of the histogram.

However, the firm wishes to understand the distribution of each industry without having to hover to see. The previous histogram has too much in a single plot for this analysis, but they don't want multiple plots. How can you help solve this conundrum?

Your task is to create a histogram of company revenues by industry as a stacked subplot and a shared x-axis to allow meaningful comparison of industries.

You have a revenues DataFrame loaded for you.

Diese Übung ist Teil des Kurses

Introduction to Data Visualization with Plotly in Python

Kurs anzeigen

Anleitung zur Übung

  • Create a subplot grid with 3 rows and 1 column, and share the x-axis.
  • Loop through the selected industries and add a histogram trace for each.
  • Place each trace in the correct row of the subplot grid.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# 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()
Code bearbeiten und ausführen