def show(x: list, y: list, title: str = "键盘键入统计"): bar = Bar(init_opts=opts.InitOpts(theme=ThemeType.WONDERLAND)) bar.add_xaxis(x) bar.add_yaxis("", y, category_gap="60%") bar.set_series_opts( itemstyle_opts={ "normal": { "color": JsCode("""new echarts.graphic.LinearGradient(0, 0, 0, 1, [{ offset: 0, color: 'rgba(0, 244, 255, 1)' }, { offset: 1, color: 'rgba(250, 177, 160, 1)' }], false)"""), "barBorderRadius": [30, 30, 30, 30], "shadowColor": "rgb(0, 160, 221)", } }) bar.set_global_opts( title_opts=opts.TitleOpts(title=title), datazoom_opts=[opts.DataZoomOpts(), opts.DataZoomOpts(type_="inside")], ) bar.load_javascript() bar.render_notebook() return Markup(bar.render_embed())
def show_num(df): df_time = df.groupby(['times']).size() values = df_time.values.tolist() index = df_time.index.tolist() bar = Bar() bar.add_xaxis(index) bar.add_yaxis("数量 & 时间", values) bar.set_global_opts( xaxis_opts=opts.AxisOpts(name="评论日期", axislabel_opts={"rotate": 30})) bar.render_notebook()
def show_pic_two(): data = df.loc[:, 'city'].value_counts().sort_values(ascending=False) citys = list(data.index)[:15] city_count = list(data)[:15] bar = Bar() bar.add_xaxis(citys) bar.add_yaxis("Top 15", city_count) bar.set_global_opts(title_opts=opts.TitleOpts(title="")) bar.render_notebook()
def show_star(df): df_time = df.groupby(['times']).size() dic = {} for k in df_time.index: stars = df.loc[df['times'] == str(k), 'stars'] stars = list(map(int, stars)) dic[k] = round(sum(stars) / len(stars), 2) bar_star = Bar() bar_star.add_xaxis([x for x in dic.keys()]) bar_star.add_yaxis("星级 & 时间", [x for x in dic.values()]) bar_star.set_global_opts( xaxis_opts=opts.AxisOpts(name="评论日期", axislabel_opts={"rotate": 30})) bar_star.render_notebook()
def vtradevolume(cftable, freq="D", rendered=True): """ aid function on visualization of trade summary :param cftable: cftable (pandas.DataFrame) with at least date and cash columns :param freq: one character string, frequency label, now supporting D for date, W for week and M for month, namely the trade volume is shown based on the time unit :returns: the Bar object """ ### WARN: datazoom and time conflict, sliding till 1970..., need further look into pyeacharts ### very unsatisfied about current visualize effect, and it seems the order of add and set option matters a lot if freq == "D": datedata = [d.to_pydatetime() for d in cftable["date"]] selldata = [[row["date"].to_pydatetime(), row["cash"]] for _, row in cftable.iterrows() if row["cash"] > 0] buydata = [[row["date"].to_pydatetime(), row["cash"]] for _, row in cftable.iterrows() if row["cash"] < 0] elif freq == "W": cfmerge = cftable.groupby( [cftable["date"].dt.year, cftable["date"].dt.week])["cash"].sum() datedata = [ dt.datetime.strptime(str(a) + "4", "(%Y, %W)%w") for a, _ in cfmerge.iteritems() ] selldata = [[dt.datetime.strptime(str(a) + "4", "(%Y, %W)%w"), b] for a, b in cfmerge.iteritems() if b > 0] buydata = [[dt.datetime.strptime(str(a) + "4", "(%Y, %W)%w"), b] for a, b in cfmerge.iteritems() if b < 0] elif freq == "M": cfmerge = cftable.groupby( [cftable["date"].dt.year, cftable["date"].dt.month])["cash"].sum() datedata = [ dt.datetime.strptime(str(a) + "15", "(%Y, %m)%d") for a, _ in cfmerge.iteritems() ] selldata = [[dt.datetime.strptime(str(a) + "15", "(%Y, %m)%d"), b] for a, b in cfmerge.iteritems() if b > 0] buydata = [[dt.datetime.strptime(str(a) + "15", "(%Y, %m)%d"), b] for a, b in cfmerge.iteritems() if b < 0] else: raise ParserFailure("no such freq tag supporting") buydata = [[d, round(x, 1)] for d, x in buydata] selldata = [[d, round(x, 1)] for d, x in selldata] bar = Bar() bar.add_xaxis(datedata) # buydata should before selldata, since emptylist in the first line would make the output fig empty: may be bug in pyecharts bar.add_yaxis(series_name="买入", yaxis_data=buydata, category_gap="90%") bar.add_yaxis(series_name="卖出", yaxis_data=selldata, category_gap="90%") bar.set_global_opts( xaxis_opts=opts.AxisOpts(type_="time"), datazoom_opts=[opts.DataZoomOpts(range_start=99, range_end=100)], ) if rendered: return bar.render_notebook() else: return bar
def plot_histogram(name, items, values, mode='html', img_path='res.png'): """ 绘制直方图 Note: 从 pyecharts v1 版本开始,add_yaxis 需要传入两个参数,第一个参数为 str 类型 关于 snapshot: snapshot-selenium 是 pyecharts + selenium 渲染图片的扩展,使用 selenium 需要配置 browser driver。 可以参考 selenium-python 相关介绍,推荐使用 Chrome 浏览器,可以开启 headless 模式。目前支持 Chrome, Safari。 Download: https://selenium-python.readthedocs.io/installation.html#drivers Make sure it’s in your PATH, e. g., place it in /usr/bin or /usr/local/bin. """ bar = Bar() bar.add_xaxis(items) bar.add_yaxis(name, values) if mode == 'html': return bar.render() elif mode == 'notebook': return bar.render_notebook() elif mode == 'img': make_snapshot(snapshot, bar.render(), img_path) else: raise ValueError("mode must in ['html', 'notebook', 'img']")
job = pd.DataFrame(list(job_collection.find())) job.to_csv("job.csv", encoding='utf-8') job_detail = pd.DataFrame(list(details_collection.find())) job_detail.to_csv('job_detail.csv', encoding='utf-8') # 薪资水平 salary_distribute = job['salary'].value_counts() bar = Bar() bar.add_xaxis(salary_distribute.index.values.tolist()[:10]) bar.add_yaxis("", salary_distribute.values.tolist()[:10]) bar.set_global_opts( xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-30)), title_opts=opts.TitleOpts(title="薪资分布", subtitle="薪资分布前10"), ) bar.render_notebook() # 高薪企业 height_salary = job[job['salary'] == '30-60K'] height_salary_year_edu = height_salary[['name', 'salary', 'year', 'edu']] # 职位要求 height_salary_index = height_salary.index.values.tolist() details = [] for index in height_salary_index: detail = job_detail[job_detail.index == index]['details'] details.append(detail.values.tolist()[0]) print(details) # 词云
def vtradevolume(cftable, freq="D", rendered=True): """ aid function on visualization of trade summary :param cftable: cftable (pandas.DataFrame) with at least date and cash columns :param freq: one character string, frequency label, now supporting D for date, W for week and M for month, namely the trade volume is shown based on the time unit :returns: the Bar object """ ### WARN: datazoom and time conflict, sliding till 1970..., need further look into pyeacharts startdate = cftable.iloc[0]["date"] if freq == "D": # datedata = [d.to_pydatetime() for d in cftable["date"]] datedata = pd.date_range(startdate, yesterdayobj(), freq="D") selldata = [[row["date"].to_pydatetime(), row["cash"]] for _, row in cftable.iterrows() if row["cash"] > 0] buydata = [[row["date"].to_pydatetime(), row["cash"]] for _, row in cftable.iterrows() if row["cash"] < 0] elif freq == "W": cfmerge = cftable.groupby( [cftable["date"].dt.year, cftable["date"].dt.week])["cash"].sum() # datedata = [ # dt.datetime.strptime(str(a) + "4", "(%Y, %W)%w") # for a, _ in cfmerge.iteritems() # ] datedata = pd.date_range(startdate, yesterdayobj() + pd.Timedelta(days=7), freq="W-THU") selldata = [[dt.datetime.strptime(str(a) + "4", "(%G, %V)%w"), b] for a, b in cfmerge.iteritems() if b > 0] buydata = [[dt.datetime.strptime(str(a) + "4", "(%G, %V)%w"), b] for a, b in cfmerge.iteritems() if b < 0] # %V pandas gives iso weeknumber which is different from python original %W or %U, # see https://stackoverflow.com/questions/5882405/get-date-from-iso-week-number-in-python for more details # python3.6+ required for %G and %V # but now seems no equal distance between sell and buy data, no idea why elif freq == "M": cfmerge = cftable.groupby( [cftable["date"].dt.year, cftable["date"].dt.month])["cash"].sum() # datedata = [ # dt.datetime.strptime(str(a) + "15", "(%Y, %m)%d") # for a, _ in cfmerge.iteritems() # ] datedata = pd.date_range(startdate, yesterdayobj() + pd.Timedelta(days=31), freq="MS") selldata = [[dt.datetime.strptime(str(a) + "1", "(%Y, %m)%d"), b] for a, b in cfmerge.iteritems() if b > 0] buydata = [[dt.datetime.strptime(str(a) + "1", "(%Y, %m)%d"), b] for a, b in cfmerge.iteritems() if b < 0] else: raise ParserFailure("no such freq tag supporting") buydata = [[d, round(x, 1)] for d, x in buydata] selldata = [[d, round(x, 1)] for d, x in selldata] bar = Bar() datedata = list(datedata) bar.add_xaxis(xaxis_data=datedata) # buydata should before selldata, since emptylist in the first line would make the output fig empty: may be bug in pyecharts bar.add_yaxis(series_name="买入", yaxis_data=buydata) bar.add_yaxis(series_name="卖出", yaxis_data=selldata) bar.set_global_opts( tooltip_opts=opts.TooltipOpts( is_show=True, trigger="axis", trigger_on="mousemove", axis_pointer_type="cross", ), datazoom_opts=[opts.DataZoomOpts(range_start=90, range_end=100)], ) if rendered: return bar.render_notebook() else: return bar
# https://pyecharts.org/#/zh-cn/quickstart # https://github.com/pyecharts/pyecharts from pyecharts.charts import Bar bar = Bar() bar.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]) bar.add_yaxis("商家A", [5, 20, 36, 10, 75, 90]) # render 会生成本地 HTML 文件,默认会在当前目录生成 render.html 文件 # 也可以传入路径参数,如 bar.render("mycharts.html") bar.render() """ bar.render_notebook()#在Notebook中进行渲染图像 #bar.render("1.html")#在本地生成静态网页 #make_snapshot(snapshot, bar.render(), "bar.png")#在本地生成图表截图 """ # bar = ( # Bar() # .add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]) # .add_yaxis("商家A", [5, 20, 36, 10, 75, 90]) # ) # bar.render()
from pyecharts.charts import Bar # In[47]: # 条形图 bar1 = Bar(init_opts=opts.InitOpts(width='1280px', height='1000px')) bar1.add_xaxis(province_num.index.tolist()) bar1.add_yaxis("省份", province_num.values.tolist()) bar1.set_global_opts(title_opts=opts.TitleOpts(title="调剂信息发布数省份分布"), toolbox_opts=opts.ToolboxOpts(), visualmap_opts=opts.VisualMapOpts(max_=110)) bar1.set_series_opts(label_opts=opts.LabelOpts(position='right')) # 标签 bar1.reversal_axis() bar1.render_notebook() # In[48]: from pyecharts.charts import Map # In[49]: c = Map(init_opts=opts.InitOpts(width='800px', height='750px')) c.add('',[list(z) for z in zip(province_num.index.tolist(), province_num.values.tolist())], 'china') c.set_global_opts(title_opts=opts.TitleOpts('调剂信息省份分布地图'), toolbox_opts=opts.ToolboxOpts(is_show=True),