使用 4 种绘图库,绘制一类图

下面使用 4 种常见的绘图库绘制柱状图和折线图,使用尽可能最少的代码绘制,快速入门这些库是本文的写作目的。

1 matplotlib

导入包:

import matplotlib 
matplotlib.__version__  # '2.2.2'

import matplotlib.pyplot as plt

绘图代码:

import matplotlib.pyplot as plt 
plt.plot([0, 1, 2, 3, 4, 5],
        [1.5, 1, -1.3, 0.7, 0.8, 0.9]
        ,c='red')
plt.bar([0, 1, 2, 3, 4, 5],
        [2, 0.5, 0.7, -1.2, 0.3, 0.4]
        )
plt.show()
使用 4 种绘图库,绘制一类图_第1张图片

2 seaborn

导入包:

import seaborn as sns 
sns.__version__ # '0.8.0'

绘制图:

sns.barplot([0, 1, 2, 3, 4, 5],
        [1.5, 1, -1.3, 0.7, 0.8, 0.9]
        )
sns.pointplot([0, 1, 2, 3, 4, 5],
        [2, 0.5, 0.7, -1.2, 0.3, 0.4]
        )
plt.show()
使用 4 种绘图库,绘制一类图_第2张图片

3 plotly 绘图

导入包:

import plotly 
plotly.__version__ # '2.0.11'

绘制图(自动打开html):

import plotly.graph_objs as go
import plotly.offline as offline

pyplt = offline.plot
sca = go.Scatter(x=[0, 1, 2, 3, 4, 5],
             y=[1.5, 1, -1.3, 0.7, 0.8, 0.9]
            )
bar = go.Bar(x=[0, 1, 2, 3, 4, 5],
            y=[2, 0.5, 0.7, -1.2, 0.3, 0.4]
            )
fig = go.Figure(data = [sca,bar])
pyplt(fig)
使用 4 种绘图库,绘制一类图_第3张图片

4 pyecharts

导入包:

import pyecharts
pyecharts.__version__ # '1.7.1'

绘制图(自动打开html):

bar = (
        Bar()
        .add_xaxis([0, 1, 2, 3, 4, 5])
        .add_yaxis('ybar',[1.5, 1, -1.3, 0.7, 0.8, 0.9])
    )
line = (Line()
        .add_xaxis([0, 1, 2, 3, 4, 5])
        .add_yaxis('yline',[2, 0.5, 0.7, -1.2, 0.3, 0.4])
        )
bar.overlap(line)
bar.render_notebook()
使用 4 种绘图库,绘制一类图_第4张图片

大家在复现代码时,需要注意API与包的版本紧密相关,与上面版本不同的包其内的API可能与以上写法有略有差异,大家根据情况自行调整即可。

你还可以看:

11. 装饰器案例

10. 从容使用生成器,从这2个小案例开始

简单总结下 yield

使用 4 种绘图库,绘制一类图_第5张图片

长按关注

你点的每个“在看”,我都认真当成了喜欢

你可能感兴趣的:(使用 4 种绘图库,绘制一类图)