我分别用 matplotlib, seaborn, plotly, pyecharts 这4个库来绘制同一种图形
① matplotlib
import matplotlib
matplotlib.__version__ # '2.2.2'
import matplotlib.pyplot as plt
# import matplotlib as mp1
# 显示中文
plt.rcParams["font.sans-serif"] = [u"SimHei"]
# mp1.rcParams["font.family"] = "STFangsong"
# plt.rcParams["axes.unicode_minus"] = False
# 在线显示matplotlib作出来的图形
%matplotlib inline
plt.plot(["周一","周二","周三","周四","周五","周六","周日"],
[1, 1.5, 4, -1.2, 0.8, 2.3, -2]
,c='red')
plt.bar(["周一","周二","周三","周四","周五","周六","周日"],
[2, 3, -1.5, -1.04, 4.2, 3,0.5]
)
plt.show()
# 保存图片
# plt.savefig(r"C:\Users\QDM\Desktop\混合图.jpg")
# plt.savefig(r"C:\Users\QDM\Desktop\混合图.png")
结果输出:
② seaborn
import seaborn as sns
sns.__version__ # '0.8.0'
sns.pointplot(["周一","周二","周三","周四","周五","周六","周日"],
[1, 1.5, 4, -1.2, 0.8, 2.3, -2]
)
sns.barplot(["周一","周二","周三","周四","周五","周六","周日"],
[2, 3, -1.5, -1.04, 4.2, 3,0.5]
)
plt.show()
输出结果:
③ plotly
import plotly
plotly.__version__ # '2.0.11'
import plotly.graph_objs as go
import plotly.offline as offline
pyplt = offline.plot
sca = go.Scatter(x=["周一","周二","周三","周四","周五","周六","周日"],
y=[1.5, 1, -1.3, 0.7, 0.8, 0.9]
)
bar = go.Bar(x=["周一","周二","周三","周四","周五","周六","周日"],
y=[2, 0.5, 0.7, -1.2, 0.3, 0.4]
)
fig = go.Figure(data = [sca,bar])
pyplt(fig)
输出结果:
④ pyecharts
import pyecharts
pyecharts.__version__ # '1.7.1'
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.charts import Line
from pyecharts.charts import *
# 添加
from pyecharts.charts import Line
bar = (
Bar()
.add_xaxis(["周一","周二","周三","周四","周五","周六","周日"])
.add_yaxis('ybar',[2, 3, -1.5, -1.04, 4.2, 3,0.5])
)
line = (
Line()
.add_xaxis(["周一","周二","周三","周四","周五","周六","周日"])
.add_yaxis('yline',[1, 1.5, 4, -1.2, 0.8, 2.3, -2])
)
# bar.overlap(line)
# bar.render_notebook()
bar.overlap(line)
bar.render_notebook()
# grid = (
# Grid()
# .add(bar, grid_opts=opts.GridOpts(pos_bottom="60%"))
# .add(line, grid_opts=opts.GridOpts(pos_top="60%"))
# )
# grid.render_notebook()
输出结果:
大家在复现代码时,需要注意API与包的版本紧密相关,与上面版本不同的包其内的API可能与以上写法有略有差异,大家根据情况自行调整即可。
来源:oschina
链接:https://my.oschina.net/u/3750423/blog/4316661