目录
1、散点图绘制
2、折线图绘制
1、散点图绘制
代码1:sin(x)点绘制
from pyecharts.charts import Scatterimport pyecharts.options as optsimport numpy as npx = np.linspace(0, 10, 50) # 从0开始,到10结束,绘制50个等差点y = np.sin(x) # sin(x)绘制# 链式调用point = (Scatter(init_opts=opts.InitOpts(width="720px", height="320px")).add_xaxis(xaxis_data=x).add_yaxis(series_name='', y_axis=y, label_opts=opts.LabelOpts(is_show=False)) # 是否显示散点对应的数据(默认显示))point.render()# point.render_notebook()
代码2:绘制sin和cos散点图
from pyecharts.charts import Scatterimport pyecharts.options as optsimport numpy as npx = np.linspace(0, 2*np.pi, 100)y = np.sin(x)y2 = np.cos(x)(Scatter().add_xaxis(xaxis_data=x).add_yaxis(series_name='sin', y_axis=y).add_yaxis(series_name='cos', y_axis=y2, label_opts=opts.LabelOpts(is_show=False))).render()
代码3:
# 导入库from pyecharts import options as optsfrom pyecharts.charts import Scatter# 设置销售数据week = ["周一","周二","周三","周四","周五","周六","周日"]c =Scatter()# 散点图绘制c.add_xaxis(week)c.add_yaxis("商家A",[80,65,46,37,57,68,90])c.set_global_opts(title_opts=opts.TitleOpts(title="一周的销售额(万元)")) # 设置图表标题c.render()# c.render_notebook()
2、折线图绘制
折线图是用直线段将各个数据点连接起来而组成的图形,以折线方式显示数据的变化趋势。折线图可以显示随时间(根据常用比例设置)而变化的连续数据,因此非常适合显示相等时间间隔的数据趋势。在折线图中,类别数据沿水平轴均匀分布,值数据沿垂直轴均匀分布。
代码1:sin(x)函数绘制
from pyecharts.charts import Lineimport pyecharts.options as optsimport numpy as npx = np.linspace(0, 10, 50) # 从0开始,到10结束,绘制50个等差点y = np.sin(x)line = (Line(init_opts=opts.InitOpts(width="720px", height="320px")).add_xaxis(xaxis_data=x).add_yaxis(series_name='', y_axis=y, label_opts=opts.LabelOpts(is_show=False)) # 是否显示散点对应的数据(默认显示))line.render()# line.render_notebook()
代码2:
# 导入库from pyecharts import options as optsfrom pyecharts.charts import Scatter,Line# 设置销售数据week = ["周一","周二","周三","周四","周五","周六","周日"]c = Line()# 折线图绘制c.add_xaxis(week)c.add_yaxis("商家A",[80,65,46,37,57,68,90])c.set_global_opts(title_opts=opts.TitleOpts(title="一周的销售额(万元)")) # 设置图表标题c.render()# c.render_notebook()
运行结果:
如果觉得《4 基于pyecharts的python数据可视化——散点图和折线图的绘制》对你有帮助,请点赞、收藏,并留下你的观点哦!