#271 Custom your bubble plot

The chart #270 describes how to draw a basic bubble plot with matplotlib. This page aims to describe how to custom several features of your bubble plot.

  •  

     

     

     

    The ‘c‘ argument allow you to pick up a color. I recommend to lower the opacity with the ‘alpha‘ argument for a better looking result (alpha=0 means totally transparent, alpha=1 means totally opaque)

     

     

     

     

     

     

    
    # libraries
    import matplotlib.pyplot as plt
    import numpy as np
    
    # create data
    x = np.random.rand(5)
    y = np.random.rand(5)
    z = np.random.rand(5)
    
    # Change color with c and alpha
    plt.scatter(x, y, s=z*4000, c="red", alpha=0.4)
    #plt.show()
    
    
  •  

     

     

     

    As in scatterplot, you can easily custom the shape of the markers on the bubble plot.

     

     

     

     

     

     

    
    # libraries
    import matplotlib.pyplot as plt
    import numpy as np
    
    # create data
    x = np.random.rand(5)
    y = np.random.rand(5)
    z = np.random.rand(5)
    
    # Change shape with marker
    plt.scatter(x, y, s=z*4000, marker="D")
    #plt.show()
    
    
  •  

     

     

     

     

    You can custom the average size of markers playing with the s argument. Multiply the numerical variable you use for size by 2 and the markers will be 2 times bigger.

     

     

     

     

     

     

    
    # libraries
    import matplotlib.pyplot as plt
    import numpy as np
    
    # create data
    x = np.random.rand(5)
    y = np.random.rand(5)
    z = np.random.rand(5)
    
    # Change global size playing with s
    plt.scatter(x, y, s=z*200)
    plt.savefig('PNG/#271_Bubble_plot_customization3.png', dpi=96)
    plt.clf()
    #plt.show()
    
    
  •  

     

     

     

     

     

    You can even custom the edges of the markers with the linewidth argument.

     

     

     

     

     

    
    # libraries
    import matplotlib.pyplot as plt
    import numpy as np
    
    # create data
    x = np.random.rand(5)
    y = np.random.rand(5)
    z = np.random.rand(5)
    
    # Change line around dot
    plt.scatter(x, y, s=z*4000, c="green", alpha=0.4, linewidth=6)
    plt.savefig('PNG/#271_Bubble_plot_customization4.png', dpi=96)
    plt.clf()
    #plt.show()
    
    
  •  

     

     

     

    As usual, just calling the seaborn library before the code of your graphic will highly improve the general aspect of your graphic:

     

     

     

     

     

    
    
    
    # libraries
    import matplotlib.pyplot as plt
    import numpy as np
    
    # create data
    x = np.random.rand(5)
    y = np.random.rand(5)
    z = np.random.rand(5)
    
    # pimp your plot with the seaborn style
    import seaborn as sns
    plt.scatter(x, y, s=z*4000, c="green", alpha=0.4, linewidth=6)
    
    # Add titles (main and on axis)
    plt.xlabel("the X axis")
    plt.ylabel("the Y axis")
    plt.title("A bubble plot", loc="left")
    
    

Leave a Reply

Your email address will not be published.