Python Visualization - Seaborn Implementation

Source: Internet
Author: User
Keywords python seaborn seaborn python
Although the Matplotlib library has been able to complete most of the drawing tasks, sometimes the chart is not very good-looking, and the Seaborn library provides more advanced interfaces and many customized themes to make the drawing more beautiful. In other words, it is a supplement to the Matplotlib library.
The drawing effect of Matplotlib
Let's take an example:
import numpy as np
import  matplotlib.pyplot  as plt
def plot_ sin(flip=1):
x =  np.linspace (0, 14, 100)
for i in range(1, 7):
plt.plot (x,  np.sin (x + i * 0.5) * (7 - i) * flip)
plot_ sin()
plt.show ()
import numpy as np
import  matplotlib.pyplot  as plt
def plot_ sin(flip=1):
x =  np.linspace (0, 14, 100)
for i in range(1, 7):
plt.plot (x,  np.sin (x + i * 0.5) * (7 - i) * flip)
plot_ sin()
plt.show ()
First, import the necessary library, and then define a function to draw sine function. By setting different offsets, six curves are obtained. This is the normal Matplotlib drawing method
The Seaborn library is introduced by the following code:
import numpy as np
import  matplotlib.pyplot  as plt
import seaborn as sns
def plot_ sin(flip=1):
x =  np.linspace (0, 14, 100)
for i in range(1, 7):
plt.plot (x,  np.sin (x + i * 0.5) * (7 - i) * flip)
sns.set ()
plot_ sin()
plt.show ()
import numpy as np
import  matplotlib.pyplot  as plt
import seaborn as sns
def plot_ sin(flip=1):
x =  np.linspace (0, 14, 100)
for i in range(1, 7):
plt.plot (x,  np.sin (x + i * 0.5) * (7 - i) * flip)
sns.set ()
plot_ sin()
plt.show ()
First, import Seaborn as SNS to import the Seaborn library. Before the normal drawing steps, the sns.set () set the drawing style. default sns.set () the method is as follows:
set(context="notebook", style="darkgrid", palette="deep", font="sans-serif", font_ scale=1, color_ codes=False, rc=None)
One
set(context="notebook", style="darkgrid", palette="deep", font="sans-serif", font_ scale=1, color_ codes=False, rc=None)
Set the size of the picture through the context parameter, set the style of the background through the style, and set the parameters of the color.
Seaborn offers five predefined themes:
Darkgrid: gray background + white grid
Whitegrid: white background + black grid
Dark: grey background
White: white background
Ticks: axis with scale
When Seaborn is used for drawing, light gray background and white gridlines are used by default. To change the background theme, use set_ The style() function or the style parameter passed in set() is equivalent. For example:
import numpy as np
import  matplotlib.pyplot  as plt
import seaborn as sns
def plot_ sin(flip=1):
x =  np.linspace (0, 14, 100)
for i in range(1, 7):
plt.plot (x,  np.sin (x + i * 0.5) * (7 - i) * flip)
sns.set_ Style ('whitegrid ') is equivalent to sns.set (style='whitegrid')
plot_ sin()
plt.show ()
import numpy as np
import  matplotlib.pyplot  as plt
import seaborn as sns
def plot_ sin(flip=1):
x =  np.linspace (0, 14, 100)
for i in range(1, 7):
plt.plot (x,  np.sin (x + i * 0.5) * (7 - i) * flip)
sns.set_ Style ('whitegrid ') is equivalent to sns.set (style='whitegrid')
plot_ sin()
plt.show ()
Here, the background style is changed to white background and black grid, and the following figure is obtained:
Visualization of Python drawing: Seaborn implementation
To change the size, you can use set_ The context() function, or pass in the context parameter in set(). There are four predefined contexts in Seaborn:
paper
notebook
talk
poster
By default, notebook is used. These definitions increase the size and thickness of the image. See the effect through the following code:
import numpy as np
import  matplotlib.pyplot  as plt
import seaborn as sns
def plot_ sin(flip=1):
x =  np.linspace (0, 14, 100)
for i in range(1, 7):
plt.plot (x,  np.sin (x + i * 0.5) * (7 - i) * flip)
plt.figure (figsize=(12, 8))
sns.set ()
plt.subplot (221)
plot_ sin()
sns.set (context='paper', style='whitegrid')
plt.subplot (222)
plot_ sin()
sns.set (context='talk', style='dark')
plt.subplot (223)
plot_ sin()
sns.set (context='poster', style='white')
plt.subplot (224)
plot_ sin()
plt.show ()
import numpy as np
import  matplotlib.pyplot  as plt
import seaborn as sns
def plot_ sin(flip=1):
x =  np.linspace (0, 14, 100)
for i in range(1, 7):
plt.plot (x,  np.sin (x + i * 0.5) * (7 - i) * flip)
plt.figure (figsize=(12, 8))
sns.set ()
plt.subplot (221)
plot_ sin()
sns.set (context='paper', style='whitegrid')
plt.subplot (222)
plot_ sin()
sns.set (context='talk', style='dark')
plt.subplot (223)
plot_ sin()
sns.set (context='poster', style='white')
plt.subplot (224)
plot_ sin()
plt.show ()
The settings of SNS need to be defined before drawing the subgraph. If the plt.subplot () is wrong. And it can be seen that the line width and coordinate font size of the image curve have obvious changes.
Seaborn has other interesting functions besides changing the background format, canvas size. For example:
import numpy as np
import  matplotlib.pyplot  as plt
import seaborn as sns
sns.set () ා use default settings
data =  sns.load_ Data set ('flies')
data =  data.pivot ('month', 'year', 'passengers')
sns.heatmap (data, annot=True, fmt='d', lw=0.5, cmap="YlGnBu")
plt.show ()
import numpy as np
import  matplotlib.pyplot  as plt
import seaborn as sns
sns.set () ා use default settings
data =  sns.load_ Data set ('flies')
data =  data.pivot ('month', 'year', 'passengers')
sns.heatmap (data, annot=True, fmt='d', lw=0.5, cmap="YlGnBu")
plt.show ()
Here, we call the heatmap() function of SNS to make the heat map, where the annot parameter controls whether the value is displayed, the FMT parameter controls the format of the value, the LW parameter is the line width, and the CMAP parameter controls the color mode used. The results are as follows:
summary
This paper introduces Seaborn, an advanced drawing library, and explains how to set the background style and graphic style simply. It should be noted that Seaborn settings must be defined before drawing functions. The library can make more beautiful and interesting graphics. If you are interested, you can view the official website documents and sample graphs

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.