Perfect Plots Bubble Plot

 

Perfect Plots: Bubble Plot

In [1]:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
In [2]:
df2= pd.read_csv('/home/wojciech/Pulpit/1/autos.csv')
df2.head(3)
Out[2]:
  Unnamed: 0 symboling normalized_losses make fuel_type aspiration num_doors body_style drive_wheels engine_location engine_size fuel_system bore stroke compression_ratio horsepower peak_rpm city_mpg highway_mpg price
0 0 3 NaN alfa-romero gas std two convertible rwd front 130 mpfi 3.47 2.68 9.0 111.0 5000.0 21 27 13495.0
1 1 3 NaN alfa-romero gas std two convertible rwd front 130 mpfi 3.47 2.68 9.0 111.0 5000.0 21 27 16500.0
2 2 1 NaN alfa-romero gas std two hatchback rwd front 152 mpfi 2.68 3.47 9.0 154.0 5000.0 19 26 16500.0

3 rows × 27 columns

 

I am making a synthetic variable that is supposed to increase the bubble – fuel consumption.

In [3]:
df2['city_mpg2'] = df2['city_mpg']*30
In [4]:
class Bubble_Plot_1:
    
    def __init__(self,df,X,Y,size, kolor, title):
        self.df = df
        self.X = X
        self.Y = Y
        self.size = size
        self.kolor = kolor
        self.title = title
    
    def buble(self):
        fig = plt.figure(figsize=(14, 7), dpi= 280, facecolor='white', edgecolor='black')    
        plt.scatter(X, Y, data=df, s=size, c=kolor, cmap='PuBu', edgecolors='grey', linewidths=0.8)
        ## cmap='YlGn','PuBu','YlOrBr','RdYlGn'
        plt.title(title, fontsize=16)
        plt.xlabel(X, fontsize=18)
        plt.ylabel(Y, fontsize=18)
        plt.colorbar()

        plt.show()    

    
    
import matplotlib.pyplot as plt
In [5]:
df=df2

X = 'horsepower'
Y = 'engine_size'
size = 'city_mpg2'
kolor = 'price'
title = 'Car comparison'  #<-- Tytuł wpisujemy z ręki

ZNP = Bubble_Plot_1(df,X,Y,size, kolor, title)
ZNP.buble()
In [6]:
class Bubble_Plot_2:
    
    def __init__(self, df, X, Y, size, kolor, title, title_leg, title_bub):
        self.df = df
        self.X = X
        self.Y = Y
        self.size = size
        self.kolor = kolor
        self.title = title
        self.title = title_leg
        self.title = title_bub
    
    def buble2(self):
        fig, ax = plt.subplots(figsize=(14, 7), dpi= 280, facecolor='white', edgecolor='black')    
        ax.scatter(X, Y, data=df, s=size, c=kolor, cmap='RdYlGn', edgecolors='grey', linewidths=0.8)
        ax.set_title(title, fontsize=14)
        ax.set_xlabel(X, fontsize=12)
        ax.set_ylabel(Y, fontsize=12)
        ## cmap='YlGn','PuBu','YlOrBr','RdYlGn'

        ## Sztuczka żeby mieć colorbar
        AA = ax.scatter(X, Y, data=df, s=size, c=kolor, cmap='RdYlGn', edgecolors='grey', linewidths=0.1)
        plt.colorbar(AA)

        handles, labels = AA.legend_elements(prop="sizes", alpha=0.2)
        legend2 = ax.legend(handles, labels, loc="upper left", title=title_leg)
        ## sztuczka żeby mieć podpisy na kólkach
        for i, txt in enumerate(df[title_bub]):
            ax.annotate(txt, (df[X][i],df[Y] [i]))

        plt.show()  
    
import matplotlib.pyplot as plt
In [7]:
df=df2

X = 'horsepower'
Y = 'engine_size'
size = 'city_mpg2'
kolor = 'price'
title = 'Car comparison'          ##<- tytuł wpisujemy z ręki
title_leg = 'fuel consumption'    ##<- tytuł wpisujemy z ręki
title_bub = 'make'


PRL = Bubble_Plot_2(df,X,Y,size, kolor, title, title_leg,title_bub)
PRL.buble2()