In [1]:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
Autos¶
Source of data: https://datahub.io/machine-learning/autos
In [2]:
df2= pd.read_csv('/home/wojciech/Pulpit/1/autos.csv')
df2.head(3)
Out[2]:
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()