What is
Seaborn
Seaborn is a graphic visualization Python package based on Matplotlib. It provides a highly interactive interface for users to make a variety of attractive statistical charts.
Seaborn is a more advanced API encapsulation based on Matplotlib, which makes drawing easier. In most cases, using Seaborn can make very attractive drawings, while using Matplotlib can make drawings with more characteristics.
Seaborn should be seen as a complement to Matplotlib, not a replacement. At the same time, it can be highly compatible with numpy and pandas data structures and statistics models such as SciPy and statsmodels.
In accordance with the international practice, we need to pack first
pip3 install seaborn
What error is reported is certain. Seaborn package depends on SciPy package, so you need to install SciPy first. The solution is as follows:
Upgrade PIP solves this problem
python3 -m pip install --upgrade pip
#Install the package and install the dependent package (SC) required by the package
pip3 install seaborn -U
#Or
pip3 install scipy
pip3 install seaborn
seaborn API
Seaborn requires that the input type of raw data is a dataframe or numpy array of panda. The drawing function has the following forms:
SNS. Graph name (x ='x-axis column name ', y = Y-axis column name', data = original data DF object)
SNS. Drawing name (x = X-axis column name ', y = Y-axis column name', hue = group drawing parameter ', data = original data DF object)
SNS. Drawing name (x= np.array , y= np.array [, ...])
Histogram drawing
barplot
The point estimates and confidence intervals are displayed as rectangular bars.
A bar chart represents an estimate of the central trend of a numerical variable with the height of each rectangle, and uses error bars to provide some indication of the uncertainty surrounding the estimate
API introduction
seaborn.barplot (x=None, y=None, hue=None, data=None, order=None, hue_ order=None, estimator=<function mean>, ci=95, n_ boot=1000, units=None, orient=None, color=None, palette=None, saturation=0.75, errcolor='.26', errwidth=None, capsize=None, dodge=True, ax=None, **kwargs)
Example for barplot
import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
x = np.arange (8)
y = np.array ([1,5,3,6,2,4,5,6])
df = pd.DataFrame ({"x-axis": x,"y-axis": y})
sns.barplot ("x-axis","y-axis",palette="RdBu_ r",data=df)
plt.xticks (rotation=90)
plt.show ()
The abscissa is an integer of 0-7, and the ordinate represents the weight of the eight integers respectively. Adjusting the palette parameter can beautify the display style
seaborn.barplot actual combat
1. Data set
Crawled through the cat's eyes, the user commented on the movie "four kings of the sky", as shown in the figure below, including
(1. Comment time; (2. User ID of the reviewer; (3) region of the reviewer; (4) rating; (5) comment content
According to the score, let's take a look at the distribution of movie scores
2. Code
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
F = open ('deerjee. TXT ', encoding ='utf-8')
data = pd.read_ csv(f,sep=',',header=None,encoding='UTF-8',names=['date','nickname','city','rate','comment'])
#Scoring analysis
rate = data['rate'].value_ counts()
sns.set_ style("darkgrid")
bar_ plot = sns.barplot (x=( rate.index ),y=( rate.values/sum (rate)),palette="muted")
plt.xticks (rotation=90)
plt.show ()
Through pandas, read out the data in data, score under rate, data ['rate ']. Value_ Count() to count the number of people in each score, as shown in the figure below
The last abscissa is rate.index (0.0, 0.5, 1.0, 1.5.... 5.0), the vertical coordinate is the number of people / total number of people giving each score, which is easy to understand, and the final calculation is the proportion of this score.
3. Analysis results
See more than 40% of the people gave a score of 5.0, more than 85% of the people gave a score of more than 3.5, at least that the film in word-of-mouth performance is relatively good
Summary
Combined with the movie data crawled down by crawlers, according to the score, the release histogram of the score is drawn by Seaborn. It only introduces a method of Seaborn, don't worry, stpe by stpe. In fact, you can't remember the five drawing methods once you get into the document. After you learn the methods, you can consult the document according to your needs, and now you can use them