下面使用 4 种常见的绘图库绘制柱状图和折线图,使用尽可能最少的代码绘制,快速入门这些库是本文的写作目的。
导入包:
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()
导入包:
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()
导入包:
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)
导入包:
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()
大家在复现代码时,需要注意API与包的版本紧密相关,与上面版本不同的包其内的API可能与以上写法有略有差异,大家根据情况自行调整即可。
你还可以看: