Describe in depth the "drawing principles" of the four commonly used drawing libraries in Python. Are there any drawing libraries that you can't learn?

Why write this article?

Recently, many fans have come to ask me that there are too many Python drawing libraries. Do I know which one to learn? Even after I choose a certain drawing library 不知道怎么学, I don’t know what to do in the first step, or what to do next, four words 一学就忘.

In fact, this was also 困扰a problem for me at the time. After I finished learning numpy and pandas, I started learning matplotlib. I was very broken anyway. Every time I felt that there was so much drawing code, the drawing logic was completely messed up, and I didn’t know how to do it.

With my repeated learning later, I found a way to learn Python drawing library, and that is to learn it 绘图原理. As the saying goes: After “知己知彼,百战不殆”learning the principle, the rest is the question of proficiency.

Today we use the article with everyone comb matplotlib, seaborn, plotly, pyechartsdrawing principles, so that we no longer learn it so hard!
Insert picture description here

1. Matplotlib drawing principle

For more detailed drawing instructions of matplotlib, you can refer to the following article, I believe you will learn it after reading it.

Matplotlib drawing principle: http://suo.im/678FCo

1) Description of drawing principle

Through my own study and understanding, I highly summarize the matplotlib drawing principle into the following steps:

  • ① Guide library
  • ② Create a figurecanvas object
  • ③ Get the axescoordinate system object of the corresponding position
  • ④ Call the axes object to draw graphics at the corresponding position
  • ⑤ Display graphics

2) Case description

# 1.导入相关库
import matplotlib as mpl
import matplotlib.pyplot as plt
# 2.创建figure画布对象
figure = plt.figure()
# 3.获取对应位置的axes坐标系对象
axes1 = figure.add_subplot(2,1,1)
axes2 = figure.add_subplot(2,1,2)
# 4.调用axes对象,进行对应位置的图形绘制
axes1.plot([1,3,5,7],[4,9,6,8])
axes2.plot([1,2,4,5],[8,4,6,2])
# 5.显示图形
figure.show()

The results are as follows:
Insert picture description here

2. Seaborn drawing principle

Among the four drawing libraries, only there is a certain connection with the others , matplotliband seabornthere is no connection between the other drawing libraries, and even the drawing principles are different.

seaborn is matplotlib 更高级的封装. Therefore, before learning seaborn, you must first know the drawing principles of matplotlib. Since seaborn is a more advanced package of matplotlib, those of matplotlib 调优参数设置can also be used after drawing graphics with seaborn.

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, so 绘图更加容易that it can draw a lot of more delicate graphics without knowing a lot of underlying parameters. Not only that, seaborn is also compatible numpyand pandasdata structure, which plays a great role in organizing data, thus helping us to complete data visualization to a greater extent.

Due to the drawing principles of seaborn and matplotlib 绘图原理一致, I will not introduce them in detail here. You can refer to the drawing principles of matplotlib above to learn how seaborn draws. Here is a website for everyone.

Seaborn drawing principle: http://suo.im/5D3VPX

1) Case description

# 1.导入相关库
import seaborn as sns
import matplotlib.pyplot as plt

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.show()

The results are as follows:
Insert picture description here
注意:You can see that in the above drawing code, you should have such a feeling, there are both matplotlib drawing code and seaborn drawing code. In fact, this is the case. We draw graphics according to matplobt's drawing principles, but some places can be changed to seaborn-specific codes. The remaining adjustment formats can be adjusted using the methods in matplotlib.

3. Plotly drawing principle

First of all, before introducing the drawing principle of this figure, let's briefly introduce the drawing library plotly.

  • Plotly is a javascript-based drawing library. Plotly has various types of plots and beautiful effects;
  • Easy to save and share plotly's drawing results, and can be seamlessly integrated with the Web;
  • The default drawing result of ploty is an HTML web page file, which can be viewed directly through the browser;

Its drawing principle has nothing to do with matplotlib and seaborn, you need to learn it separately. Similarly, I still provide you with a website for you to learn plotly in more detail.

Plotly drawing principle: http://suo.im/5vxNTu

1) Description of drawing principle

Through my own study and understanding, I highly summarize the principle of plotly drawing into the following steps:

  • ① Draw graphic traces, which are called in "ployly" trace. Each trace is a trace.
  • ② Wrap the track into a list to form a "track list". A track is placed in a list, and multiple tracks are also placed in a list.
  • ③ While creating the canvas, pass the above mentioned 轨迹列表into Figure()it.
  • ④ Use to Layout()add other drawing parameters to improve the graphics.
  • ⑤ Display graphics.

2) Case description

import numpy as np
import pandas as pd
import plotly as py
import plotly.graph_objs as go
import plotly.expression as px
from plotly import tools

df = pd.read_excel("plot.xlsx")
# 1.绘制图形轨迹,在ployly里面叫做`trace`,每一个轨迹是一个trace。
trace0 = go.Scatter(x=df["年份"],y=df["城镇居民"],name="城镇居民")
trace1 = go.Scatter(x=df["年份"],y=df["农村居民"],name="农村居民")
# 2.将轨迹包裹成一个列表,形成一个“轨迹列表”。一个轨迹放在一个列表中,多个轨迹也是放在一个列表中。
data = [trace0,trace1]
# 3.创建画布的同时,并将上述的`轨迹列表`,传入到`Figure()`中。
fig = go.Figure(data)
# 4.使用`Layout()`添加其他的绘图参数,完善图形。
fig.update_layout(
    title="城乡居民家庭人均收入",
    xaxis_title="年份",
    yaxis_title="人均收入(元)"
)
# 5.展示图形。
fig.show()

The results are as follows:
Insert picture description here

4. pyecharts drawing principle

EchartsIt is a data visualization tool open sourced by Baidu. It has been recognized by many developers with its good interactivity and exquisite chart design. Python is an expressive language, which is very suitable for data processing. When data analysis meets data visualization, it was pyechartsborn.

pyecharts divided v0.5and v1two large versions, v0.5 and the two versions are not compatible v1, v1 is a new version, so we are learning as much as possible 基于v1版本to operate.

Like plotly, the drawing principles of pyecharts are completely different from matplotlib and seaborn. We need to learn more about their drawing principles. Based on this, we will also provide you with a website to learn pyecharts in more detail.

The drawing principle of pyecharts: http://suo.im/5S1PF1

1) Description of drawing principle

Through my own study and understanding, I highly summarize the principle of plotly drawing into the following steps:

  • ① Select the chart type;
  • ② Declare the graphics class and add data;
  • ③ Select global variables;
  • ④ Display and save the chart;

2) Case description

# 1.选择图表类型:我们使用的是线图,就直接从charts模块中导入Line这个模块;
from pyecharts.charts import Line
import pyecharts.options as opts
import numpy as np

x = np.linspace(0,2 * np.pi,100)
y = np.sin(x)

(
 # 2.我们绘制的是Line线图,就需要实例化这个图形类,直接Line()即可;
 Line()
 # 3.添加数据,分别给x,y轴添加数据;
 .add_xaxis(xaxis_data=x)
 .add_yaxis(series_name="绘制线图",y_axis=y,label_opts=opts.LabelOpts(is_show=False))
 .set_global_opts(title_opts=opts.TitleOpts(title="我是标题",subtitle="我是副标题",title_link="https://www.baidu.com/"),
                  tooltip_opts=opts.TooltipOpts())
).render_notebook() # 4.render_notebook()用于显示及保存图表;

The results are as follows:
Insert picture description here

summary

Through the above study, I believe that everyone will have a new understanding of the drawing principles of these libraries.

In fact, no matter it is a drawing library of any programming software, it has its drawing principles. Instead of drawing all kinds of graphics blindly, we should first figure out their routines and then practice graphics in the drawing library. If this continues, I think everyone will have a great improvement.

Guess you like

Origin blog.csdn.net/weixin_41261833/article/details/107607695