pythonpandasplotly

How to add space between Bubbles and increase thier size


I have a bubble chart developed using plotly library and here’s the data :

import plotly.express as px
import pandas as pd
data = {
    "lib_acte":["test 98lop1", "test9665 opp1", "test QSDFR1", "test ABBE1", "testtest21","test23"],
    "x":[12.6, 10.8, -1, -15.2, -10.4, 1.6],
    "y":[15, 5, 44, -11, -35, -19],
    "circle_size":[375, 112.5, 60,210, 202.5, 195],
    "color":["green", "green", "green", "red", "red", "red"]
}

#load data into a DataFrame object:
df = pd.DataFrame(data)

fig = px.scatter(
        df,
        x="x", 
        y="y", 
        color="color",
        size='circle_size',
        text="lib_acte",
        hover_name="lib_acte",
        color_discrete_map={"red": "red", "green": "green"},
        title="chart"
      )
fig.update_traces(textposition='middle right', textfont_size=14, textfont_color='black', textfont_family="Inter", hoverinfo="skip")
newnames = {'red':'red title', 'green': 'green title'}

fig.update_layout(
        {
            
            'yaxis': {
                "range": [-200, 200],
                'zerolinewidth': 2, 
                "zerolinecolor": "red",
                "tick0": -200,
                "dtick":45,
            },
            'xaxis': {
                "range": [-200, 200],
                'zerolinewidth': 2, 
                "zerolinecolor": "gray",
                "tick0": -200,
                "dtick": 45,
                #  "scaleanchor": 'y'
            },
           
            "height": 800,
            
            
        }
    )
fig.add_scatter(
        x=[0, 0, -200, -200],
        y=[0, 200, 200, 0],
        fill="toself",
        fillcolor="gray",
        zorder=-1,
        mode="markers",
        marker_color="rgba(0,0,0,0)",
        showlegend=False,
        hoverinfo="skip"
    )
fig.add_scatter(
        x=[0, 0, 200, 200],
        y=[0, -200, -200, 0],
        fill="toself",
        fillcolor="yellow",
        zorder=-1,
        mode="markers",
        marker_color="rgba(0,0,0,0)",
        showlegend=False,
        hoverinfo="skip"
    )
fig.update_layout(
   
            paper_bgcolor="#F1F2F6",
        )
fig.show()

output :

enter image description here

now what I’m looking for please a way to add space between bubble if they are tight like (test 981op1 and test9665 opp1), and also a way to to increase the each bubble size 4% of its size for example. thanks for your help.


Solution

  • To change the size of your bubbles just do something like this:

    
    multiplier = 1.04  # made bigger by 4%
    df["circle_size"] = df["circle_size"]*multiplier
    

    But you still need to change the maximum size of the bubbles:

    Here i changed it to the biggest bubble size:

    biggest_bubble_size = max(df["circle_size"])
    
    fig = px.scatter(
            df,
            x="x", 
            y="y", 
            color="color",
            size='circle_size',
            size_max= biggest_bubble_size,   #change the maximum size of bubbles
            text="lib_acte",
            hover_name="lib_acte",
            color_discrete_map={"red": "red", "green": "green"},
            title="chart"
          )
    

    Now the bubbles seam too large, so i recommend setting the multiplier to something like this:

    multiplier = 0.1
    

    And for the points being too close together just change the graph scale acording to the furthest point:

    maxrange = max(df["x"].max(), df["y"].max())
    maxrange = maxrange*1.2  #to give some space to the furthest point
    
    number_of_lines = 10 #changes the number of lines in the graph
    
    fig.update_layout(
            {
                
                'yaxis': {
                    "range": [-maxrange, maxrange],
                    'zerolinewidth': 2, 
                    "zerolinecolor": "red",
                    "tick0": -maxrange,
                    "dtick":maxrange/number_of_lines,
                },
                'xaxis': {
                    "range": [-maxrange, maxrange],
                    'zerolinewidth': 2, 
                    "zerolinecolor": "gray",
                    "tick0": -maxrange,
                    "dtick": maxrange/number_of_lines,
                    #  "scaleanchor": 'y'
                },
               
                "height": 800,
                
                
            }
        )
    

    enter image description here