[Python] How to create filled and stacked x y scatter plot with data from multiple rows and...

Discussão em 'Python' iniciado por Stack, Agosto 7, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working in Jupyter notebooks trying to build a stacked and filled x,y scatter bar chart from the dataframe (df_xy_columns) below:

    sum_y_gran PVR Group min_x min_x2 max_x max_x2 min_y max_y max_y2 min_y2
    2 216 1_Low 0 0 68 68 0 54 54 0
    6 170 1_Low 68 68 138 138 0 30 30 0
    3 150 1_Low 138 138 193 193 0 10 10 0
    1 216 2_Med 0 0 68 68 54 116 116 54
    5 170 2_Med 68 68 138 138 30 90 90 30
    8 150 2_Med 138 138 193 193 10 69 69 10
    0 216 3_High 0 0 68 68 116 216 216 116
    4 170 3_High 68 68 138 138 90 170 170 90
    7 150 3_High 138 138 193 193 69 150 150 69


    Each of the layers of the scatter plot should represent one of the unique categories in "PVR Group".

    Looking something like this plot: stacked bar chart with x axis

    I've successfully created a dictionary with series for the x and y data for each PVR Group with the data in the correct order using this code:

    grouped = df_xy_columns.groupby('PVR Group')
    x_series_by_category = {}
    y_series_by_category = {}
    for name, group in grouped:
    # Select the columns you want to include in the Series
    # You can select a single column or combine multiple columns
    x_selected_data = group[['min_x','min_x','max_x','max_x']].values.flatten()
    x_series_by_category[name] = pd.Series(x_selected_data, name=f'x_data_for_{name}')
    y_selected_data = group[['min_y','max_y','max_y','min_y']].values.flatten()
    y_series_by_category[name] = pd.Series(y_selected_data, name=f'y_data_for_{name}')


    And created a stacked scatter plot using the dictionary from this code:

    plt.xlabel("Cumulative " + x_axis)
    plt.ylabel(y_axis)
    plt.title(y_axis+" Bookshelf Chart by "+gran)
    plt.grid(False) # Optional: Add a grid
    for name in x_series_by_category:
    plt.plot(x_series_by_category[name], y_series_by_category[name],linestyle='-')


    All that's left is to figure out how to shade between the lines and I'm stumped as I can't seem to find a way to define the previous series as the shade between variable without manually naming it...

    Continue reading...

Compartilhe esta Página