Make graphics refined: seaborn drawing basics

1. Advantages of seaborn

  • It simplifies the representation of complex data sets;

  • You can easily build complex visualizations, concisely control matplotlib graphic styles and several built-in themes;

  • Seaborn cannot replace matplotlib, but a good supplement to matplotlib;

2. Seaborn's official website

To learn a certain knowledge point, the best thing is to follow the instructions on the official website, because the knowledge points in the official website are complete and comprehensive. Seaborn's official website link: http://seaborn.pydata.org

image

3. Introduction to the author of seaborn

image

4. Why the abbreviation of seaborn is sns instead of sbn?

The use of sns comes from an internal joke related to the American drama The West Wing. There is a character in this play, named Samual Norman Seaborn, whose initials are abbreviated as sns, so the final abbreviation is sns.

5. The relationship between seaborn and matplotlib?

seaborn is a more advanced package of matplotlib. Therefore, before learning seaborn, you must first know the drawing principles of matplotlib.

We know that to use matplotlib to draw, a lot of drawing parameters need to be adjusted, and there are many things to remember. Seaborn has made a more advanced package based on matplotlib, which makes drawing easier. It does not need to understand a large number of underlying parameters to draw many more delicate graphics. Not only that, seaborn is also compatible with numpy and pandas data structures, which plays a great role in organizing data, thereby helping us to complete data visualization to a greater extent.

The most important thing: seaborn is a more advanced package of matplotlib. In other words, the tuning parameter settings of matplotlib can also be used after drawing the graph with seaborn.
  

6. 3 ways to use seaborn to draw

  • plt.style.use("seaborn"): It's just that the drawing style of seaborn is called, which does not really reflect the benefits of seaborn drawing.

  • sns.set(): After using this method, all the parameters in matplotlib that have been written before are restored. Therefore, setting the Chinese font display and setting the normal display of the negative sign must be placed after the sns.set() code.

  • Directly call seaborn function to draw: This method can truly reflect the advantages of seaborn drawing, and it is also a drawing method we often use. (Most used)

1)plt.style.use(“seaborn”)

df = pd.read_excel("data.xlsx",sheet_name="数据源")
df1 = df.groupby("品牌").agg({"销售数量":np.sum})

# 使用matplotlib风格绘图
plt.bar(x=df1.index,height=df1["销售数量"],width=0.5,color="blue")
plt.savefig(r"G:\6Tipdm\2 python绘图\seaborn\picture\seaborn绘图方式_1",dpi=300)

# 使用seaborn风格绘图
plt.style.use("seaborn")
plt.bar(x=df1.index,height=df1["销售数量"],width=0.5,color="blue")
plt.savefig(r"G:\6Tipdm\2 python绘图\seaborn\picture\seaborn绘图方式_2",dpi=300)

The results are as follows:

image
image

2)sns.set()

There are several parameters in this method, but in practice, we can use the default values, because the default parameter drawing is already very beautiful, and we don't need to set it specially.

① Common parameters: sns.set(style=, context=, font_scale=)
style sets the style of drawing.
The context generally uses the default style and does not need to be set by ourselves. The default is context="notebook".
font_scale controls the scale of the coordinate axis, generally set to font_scale=1.2.
② The demonstration is as follows

df = pd.read_excel("data.xlsx",sheet_name="数据源")
df1 = df.groupby("品牌").agg({"销售数量":np.sum})

# 使用matplotlib风格绘图
plt.bar(x=df1.index,height=df1["销售数量"],width=0.5,color="blue")
plt.savefig(r"G:\6Tipdm\2 python绘图\seaborn\picture\seaborn绘图方式_3",dpi=300)

# 使用seaborn风格绘图
sns.set()
plt.rcParams["font.sans-serif"] = "SimHei"
plt.rcParams["axes.unicode_minus"] = False

plt.bar(x=df1.index,height=df1["销售数量"],width=0.5,color="blue")
plt.savefig(r"G:\6Tipdm\2 python绘图\seaborn\picture\seaborn绘图方式_4",dpi=300)

The results are as follows:

image
image

3) Directly call seaborn function to draw (most commonly used): the advantages of seaborn drawing are reflected

df = pd.read_excel("data.xlsx",sheet_name="数据源")

sns.set_style("dark")
plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.rcParams["axes.unicode_minus"] = False
# 注意:estimator表示对分组后的销售数量求和。默认是求均值。
sns.barplot(x="品牌",y="销售数量",data=df,color="steelblue",orient="v",estimator=sum)
plt.savefig(r"G:\6Tipdm\2 python绘图\seaborn\picture\seaborn绘图方式_5",dpi=300)

The results are as follows:

image
Note: The benefits of directly calling the seaborn function for drawing are well reflected in this code. It can be seen that if you directly use the code in matplotlib to draw, you need to group and aggregate the data set before you can draw the final graph. [Advantages]: Directly use the sns.barplot() function to draw, barplot can directly display the groupby grouping results according to the specified summary method for visual display, and we do not need to process the data.

Welcome to scan the code to follow the author's CSDN:

image

PS: I would like to thank Mr. Huang for his contribution. At the same time, I also welcome friends who have followed the [Cola's Data Analysis Road] public account to actively contribute to the original author. The appreciation of this article and the traffic of the next day will be given to the original author.


Guess you like

Origin blog.51cto.com/15064638/2598079