This article is about how to use seaborn to draw various bar charts

  • Base Bar Chart

  • Horizontal Bar Chart

  • Title Settings

  • DataFrame based drawing

  • hue parameter setting

  • Color processing

  • Multi-dimensional processing

Personally, I like the graphics drawn by Seaborn:

Seaborn

Import Library

Seaborn is a high-level wrapper for matplotlib, so matplotlib still needs to be imported at the same time:

In [1]:

import pandas as pd
import numpy as np

import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

sns.set_theme(style="whitegrid")
sns.set_style('darkgrid')

Importing built-in data

A built-in dataset of consumer tips in seaborn is used:

In [2]:

tips = sns.load_dataset("tips")
tips.head()

Seaborn

Base Bar Chart

In [3]:

x = ["A","B","C"]
y = [1, 2, 3]

sns.barplot(x, y)
plt.show()

Seaborn

Plot horizontal bar graphs:

# Horizontal Bar Chart

x = ["A","B","C"]
y = [1, 2, 3]

sns.barplot(y, x)
plt.show()

Seaborn

Set title

In [14]:

x = ["A","B","C"]
y = [1, 2, 3]

fig = sns.barplot(x, y)
fig.set_title('title of seaborn')

plt.show()

Seaborn

Specify x-y-data

In [5]:

# Specify by DataFrame

ax = sns.barplot(x="day", y="tip", data=tips)
plt.show()

Seaborn

hue parameters

Implemented group display data

In [6]:

ax = sns.barplot(x="day", 
        y="total_bill", 
        hue="sex", 
        data=tips)

Seaborn

Horizontal Bar Chart

In [7]:

ax = sns.barplot(x="total_bill", 
                 y="day", 
                 data=tips)

Seaborn

Customized order

In [8]:

ax = sns.barplot(x="total_bill", 
                 y="day", 
                 # Add the order parameter to specify the order
                 order=["Sat","Fri","Sun","Thur"],  # Customization
                 data=tips)

Seaborn

Color processing

Use one color

In [9]:

ax = sns.barplot(x="size", 
                 y="total_bill", 
                 data=tips,
                 color="salmon", 
                 saturation=.5)

Seaborn

Color gradient

In [10]:

ax = sns.barplot(x="size", 
                 y="tip", 
                 data=tips,
                 palette="Blues")

Seaborn

Multi-dimensional grouping

In [11]:

g = sns.catplot(x="sex", 
                y="total_bill",
                hue="smoker", 
                col="time",
                data=tips, 
                kind="bar",
                height=4, 
                aspect=.7)

Seaborn

True/False grouping

In [12]:

tips["weekend"] = tips["day"].isin(["Sat", "Sun"])
tips

Out[12]:

Seaborn

In [13]:

ax = sns.barplot(x="day", 
                 y="tip", 
                 hue="weekend",
                 data=tips, 
                 dodge=False)

Seaborn

Seaborn

Related articles

Tips for speeding up pandas

When people talk about data analysis, the most mentioned languages are Python and SQL. python is suitable for data analysis because it has many powerful third-party libraries to assist, pandas being one of them. pandas is described in the documentation as

Speed up pandas with Modin

Modin exists for a reason: to change one line of code to speed up the pandas workflow.Pandas needs no introduction in the field of data science, providing high-performance, easy-to-use data structures and data analysis tools. However, when dealing

How to open a txt file in python

Two ways to open a file f = open("data.txt","r") #Setting the file object f.close() #Close file #For convenience, and to avoid forgetting to close the file object, you can use the following instead

python how to add new content to the dictionary

Adding new content to the dictionary is done by adding new key/value pairs, modifying or deleting existing key/value pairs as shown in the following examples: #!/usr/bin/python dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}

python how to get the current file path

Python method for getting the current path. import os,sys Use sys.path[0], sys.argv[0], os.getcwd(), os.path.abspath(__file__), os.path.realpath(__file__)

How to read csv files in python

Comma-Separated Values (CSV, sometimes called character-separated values because the separating characters can also be other than commas), whose files store tabular data (numbers and text) in plain text

How to output vertically in python

Example: The output is the following case H e l l o W o r l d This can be done using a for loop. for name in "Hello World": print(name) This can also be done using the join method print("\n".join("Hello World"))

python how to input a list with input function

In Python 3.0 onwards, keyboard input uses the input function >>> x=input >>> 123 123 There is no display on the command line, and the input 123 is directly assigned to x and printed. Using input alone can't solve most of the data processing, usually