How to build beautiful graphs in Python with Seaborn

Future students of the course “Python Developer. Professional ” and everyone who is interested is invited to take part in the open webinar on the topic “ Frameworks and Metaclasses ”. And now we are sharing the traditional translation of useful material.








— , , . .





, Tableau, Power BI, ChartBlocks , no-code . , . , , , Python .





, , Python , . .





Python — data science , — . Python , numpy



, pandas



, matplotlib



, tensorflow



.





Matplotlib



, , , Python , R. . .





matplotlib



, seaborn



. Seaborn



, matplotlib



, .





, seaborn



. , seaborn GitHub.





Seaborn?

Seaborn — Python. matplotlib pandas.





Seaborn . Seaborn , , , .





, .





Seaborn

seaborn



, , Python. seaborn



, matplotlib



, pandas



, numpy



scipy



.





seaborn



, , notebook



, .





pipenv install seaborn notebook
      
      



, .





import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib
      
      



, , . seaborn



, dataframe



pandas



, . , , , .





, .





flights_data = sns.load_dataset("flights")
flights_data.head()
      
      







year





month





passengers





0





1949





Jan





112





1





1949





Feb





118





2





1949





Mar





132





3





1949





Apr





129





4





1949





May





121





load_dataset



, dataframe



. Github.





— Scatter Plot

— , . seaborn , .





sns.scatterplot(data=flights_data, x="year", y="passengers")
      
      



, ? scatterplot



, , , x y.





— Line Plot

, . , . , lineplot



, x y. seaborn



.





sns.lineplot(data=flights_data, x="year", y="passengers")
      
      



— Bar Plot

, , , , seaborn



, , , barplot



.





sns.barplot(data=flights_data, x="year", y="passengers")
      
      



, . .





matplotlib

Seaborn matplotlib, . seaborn . seaborn matplotlib. seaborn matplotlib .





, seaborn, subplot



matplotlib.





diamonds_data = sns.load_dataset('diamonds')
plt.subplot(1, 2, 1)
sns.countplot(x='carat', data=diamonds_data)
plt.subplot(1, 2, 2)
sns.countplot(x='depth', data=diamonds_data)
      
      



subplot



. : – , – , – .





seaborn , matplotlib seaborn.





Seaborn Pandas

, seaborn pandas , dataframe



. seaborn , , , pandas?





drinks_df = pd.read_csv("data/drinks.csv")
sns.barplot(x="country", y="beer_servings", data=drinks_df)
      
      



Seaborn . : darkgrid, whitegrid, dark, white ticks.





sns.set_style("darkgrid")
sns.lineplot(data = data, x = "year", y = "passengers")
      
      



.





sns.set_style("whitegrid")
sns.lineplot(data=flights_data, x="year", y="passengers")
      
      







seaborn, . «tips», seaborn.





.





tips_df = sns.load_dataset('tips')
tips_df.head()
      
      











total_bill





tip





sex





smoker





day





time





size





0





16.99





1.01





Female





No





Sun





Dinner





2





1





10.34





1.66





Male





No





Sun





Dinner





3





2





21.01





3.50





Male





No





Sun





Dinner





3





3





23.68





3.31





Male





No





Sun





Dinner





2





4





24.59





3.61





Female





No





Sun





Dinner





4





, . pandas, , null



, , . pandas.





, . 





tips_df["tip_percentage"] = tips_df["tip"] / tips_df["total_bill"]
tips_df.head()
      
      



:









total_bill





tip





sex





smoker





day





time





size





tip_percentage





0





16.99





1.01





Female





No





Sun





Dinner





2





0.059447





1





10.34





1.66





Male





No





Sun





Dinner





3





0.160542





2





21.01





3.50





Male





No





Sun





Dinner





3





0.166587





3





23.68





3.31





Male





No





Sun





Dinner





2





0.139780





4





24.59





3.61





Female





No





Sun





Dinner





4





0.146808





.





. histplot



, .





sns.histplot(tips_df["tip_percentage"], binwidth=0.05)
      
      



, binwidth



, . 15 20% , , 70%. , , .





, .





sns.histplot(data=tips_df, x="tip_percentage", binwidth=0.05, hue="time")
      
      



, , hue



time



. , .





— , .





sns.barplot(data=tips_df, x="day", y="tip", estimator=np.sum)
      
      



, — , .





, . , ?





, pivot



pandas , .





pivot = tips_df.pivot_table(
    index=["day"],
    columns=["size"],
    values="tip_percentage",
    aggfunc=np.average)
sns.heatmap(pivot)
      
      



, , seaborn, .





, , . !






«Python Developer. Professional».



« ».












All Articles