def district_score_box_plot(df, district, school_name): try: # school_name=None middle_score = middle_school_data4box(df) labels = list(middle_score.columns) c = Boxplot(init_opts=opts.InitOpts(width='1750px')) c.add_xaxis([""]) for col in labels: value = [list(middle_score[col].dropna())] c.add_yaxis(col, c.prepare_data(value)) c.set_global_opts( title_opts=opts.TitleOpts(title=f"{district}-{school_name}成绩分布"), legend_opts=opts.LegendOpts(is_show=False), yaxis_opts=opts.AxisOpts(min_=400, max_=750)) return c except: v2 = [Faker.values(450, 650)] v1 = [Faker.values(500, 700)] c = Boxplot(init_opts=opts.InitOpts(width='1750px')) c.add_xaxis(['']) c.add_yaxis("中学A", c.prepare_data(v1)).add_yaxis("中学B", c.prepare_data(v2)) c.set_global_opts(title_opts=opts.TitleOpts( title=f"{district}初中成绩分布(示例)", subtitle="当看到此页面,说明所选区目前无数据")) return c
def test_boxplot_item_base(fake_writer): x_axis = ["expr1", "expr2"] v1 = [ [850, 740, 900, 1070, 930, 850, 950, 980, 980, 880, 1000, 980], [960, 940, 960, 940, 880, 800, 850, 880, 900, 840, 830, 790], ] v2 = [ [890, 810, 810, 820, 800, 770, 760, 740, 750, 760, 910, 920], [890, 840, 780, 810, 760, 810, 790, 810, 820, 850, 870, 870], ] c = Boxplot() series_a = [ opts.BoxplotItem(name=x_axis[0], value=d) for d in c.prepare_data(v1) ] series_b = [ opts.BoxplotItem(name=x_axis[1], value=d) for d in c.prepare_data(v2) ] c.add_xaxis(xaxis_data=x_axis).add_yaxis("A", series_a).add_yaxis( "B", series_b) c.render() _, content = fake_writer.call_args[0] assert_equal(c.theme, "white") assert_equal(c.renderer, "canvas")
def boxplot(self, column:str): if self.check_col_type(column, "number"): series = self.data[column] boxplot = Boxplot() boxplot.add_xaxis([column]).add_yaxis("", boxplot.prepare_data([list(series)])) boxplot.set_global_opts(title_opts=opts.TitleOpts(title="{}-箱线图".format(column))) return boxplot.dump_options() else: return None
def paintBoxPlot(datadict, xtitle, ytitle, title): graph = Boxplot(init_opts=opts.InitOpts(page_title=title)) graph.set_global_opts( title_opts=opts.TitleOpts(title=title), yaxis_opts=opts.AxisOpts(name='Accuracy') ) graph.add_xaxis([xtitle]) for key, value in sorted(datadict.items()): # 为了画图的标准性,小于5人的年级图片将不会被加入盒图 if len(value) > 4: graph.add_yaxis(key, graph.prepare_data([value])) graph.render('buctoj2_boxplot.html')
def boxpolt_base() -> Boxplot: v1 = [ [850, 740, 900, 1070, 930, 850, 950, 980, 980, 880, 1000, 980], [960, 940, 960, 940, 880, 800, 850, 880, 900, 840, 830, 790], ] v2 = [ [890, 810, 810, 820, 800, 770, 760, 740, 750, 760, 910, 920], [890, 840, 780, 810, 760, 810, 790, 810, 820, 850, 870, 870], ] c = Boxplot() c.add_xaxis(["expr1", "expr2"]).add_yaxis("A", c.prepare_data(v1)).add_yaxis( "B", c.prepare_data(v2) ).set_global_opts(title_opts=opts.TitleOpts(title="BoxPlot-基本示例")) return c
def test_boxpolt_base(): v1 = [ [850, 740, 900, 1070, 930, 850, 950, 980, 980, 880, 1000, 980], [960, 940, 960, 940, 880, 800, 850, 880, 900, 840, 830, 790], ] v2 = [ [890, 810, 810, 820, 800, 770, 760, 740, 750, 760, 910, 920], [890, 840, 780, 810, 760, 810, 790, 810, 820, 850, 870, 870], ] c = Boxplot() c.add_xaxis(["expr1", "expr2"]).add_yaxis("A", c.prepare_data(v1)).add_yaxis( "B", c.prepare_data(v2) ) assert c.theme == "white" assert c.renderer == "canvas" c.render("render.html")
def test_boxpolt_base(fake_writer): v1 = [ [850, 740, 900, 1070, 930, 850, 950, 980, 980, 880, 1000, 980], [960, 940, 960, 940, 880, 800, 850, 880, 900, 840, 830, 790], ] v2 = [ [890, 810, 810, 820, 800, 770, 760, 740, 750, 760, 910, 920], [890, 840, 780, 810, 760, 810, 790, 810, 820, 850, 870, 870], ] c = Boxplot() c.add_xaxis(["expr1", "expr2"]).add_yaxis("A", c.prepare_data(v1)).add_yaxis( "B", c.prepare_data(v2)) c.render() _, content = fake_writer.call_args[0] eq_(c.theme, "white") eq_(c.renderer, "canvas")
def box_plot(data: dict, title: str = "箱线图", width: str = "900px", height: str = "680px") -> Boxplot: """ :param data: 数据 样例: data = { "expr 0": [960, 850, 830, 880], "expr 1": [960, 850, 830, 880], } :param title: :param width: :param height: :return: """ x_data = [] y_data = [] for k, v in data.items(): x_data.append(k) y_data.append(v) init_opts = opts.InitOpts(page_title=title, width=width, height=height) chart = Boxplot(init_opts=init_opts) chart.add_xaxis(xaxis_data=x_data) chart.add_yaxis(series_name="", y_axis=y_data) chart.set_global_opts(title_opts=opts.TitleOpts(pos_left="center", title=title), tooltip_opts=opts.TooltipOpts(trigger="item", axis_pointer_type="shadow"), xaxis_opts=opts.AxisOpts( type_="category", boundary_gap=True, splitarea_opts=opts.SplitAreaOpts(is_show=False), axislabel_opts=opts.LabelOpts(formatter="{value}"), splitline_opts=opts.SplitLineOpts(is_show=False), ), yaxis_opts=opts.AxisOpts( type_="value", name="", splitarea_opts=opts.SplitAreaOpts( is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1) ) )) return chart
def boxplot_product_price_base(): """ 此函数用于获取产品价格的箱线图的参数。 Returns ------- c : TYPE-echarts parameters return echarts parameters. """ # data query dataX, dataY = product_price_query() # Declare objects, render pictures c = Boxplot() c.add_xaxis(dataX) c.add_yaxis("Pricing",c.prepare_data(dataY)) c.set_global_opts(title_opts=opts.TitleOpts(title="Distribution of Product Pricing", subtitle="Unit:$")) return c
def boxpolt_base() -> Boxplot: v_sophomore = [[1.1, 2.2, 2.6, 3.2, 3.7, 4.2, 4.7, 4.7, 5.5, 6.3, 8.0], [2.5, 2.5, 2.8, 3.2, 3.7, 4.2, 4.7, 4.7, 5.5, 6.3, 7.0]] v_junior = [[3.6, 3.7, 4.7, 4.9, 5.1, 5.2, 5.3, 5.4, 5.7, 5.8, 5.8], [3.6, 3.7, 4.7, 4.9, 5.1, 5.2, 5.3, 5.4, 5.7, 5.8, 5.8]] # 最小值,下四分位数,中位数、上四分位数、最大值 # [min, Q1, median (or Q2), Q3, max] c = ( Boxplot().add_xaxis(['寒假作业', '暑假作业']).add_yaxis( '大二队员', Boxplot.prepare_data(v_sophomore)).add_yaxis( '大三队员', Boxplot.prepare_data(v_junior)).set_series_opts( label_opts=opts.LabelOpts(is_show=True)).set_global_opts( title_opts=opts.TitleOpts(title='ACM集训队祖传练习完成时长离散度'), xaxis_opts=opts.AxisOpts(name='单位:小时'), legend_opts=opts.LegendOpts( is_show=True)).reversal_axis() #翻转XY轴 ) return c
def salary(self): sql = 'SELECT workYear,replace(salary,\'k\',\'\') s FROM jobs group by workYear,salary order by workYear' results = self.query(sql) sum = {} for r in results: rs = r[1].split('-') a = sum.get(r[0], []) a.extend(rs) sum[r[0]] = a for k in sum: numbers = list(map(int, sum[k])) v = list(set(numbers)) sum[k] = v print(list(sum.values())) c = Boxplot() c.add_xaxis(list(sum.keys())) c.add_yaxis("薪资与工作经验", c.prepare_data(list(sum.values()))) c.set_global_opts(title_opts=opts.TitleOpts(title="薪资与工作经验")) c.render("拉勾薪资.html")
def draw_project_cost_boxplot(): row_dict = request_data.get_project_cost() return_code = row_dict['code'] if return_code == 0: x_data = ['Success', 'Failed'] success_data = row_dict['stateProjectCost']['state']['success'] y_data1 = [[success_data['min'], success_data['25%'], success_data['mean'], success_data['75%'], success_data['max']]] failed_data = row_dict['stateProjectCost']['state']['failed'] y_data2 = [[failed_data['min'], failed_data['25%'], failed_data['mean'], failed_data['75%'], failed_data['max']]] print('boxplot') print(x_data) print(y_data1) print(y_data2) project_cost_boxplot = Boxplot() project_cost_boxplot.add_xaxis(x_data) project_cost_boxplot.add_yaxis("成功项目", y_data1) project_cost_boxplot.add_yaxis("失败项目", y_data2) project_cost_boxplot.set_global_opts(title_opts=opts.TitleOpts(title="项目花费")) return project_cost_boxplot else: return 0
[ 880, 880, 880, 860, 720, 720, 620, 860, 970, 950, 880, 910, 850, 870, 840, 840, 850, 840, 840, 840 ], [ 890, 810, 810, 820, 800, 770, 760, 740, 750, 760, 910, 920, 890, 860, 880, 720, 840, 850, 850, 780 ], [ 890, 840, 780, 810, 760, 810, 790, 810, 820, 850, 870, 870, 810, 740, 810, 940, 950, 800, 810, 870 ]] scatter_data = [650, 620, 720, 720, 950, 970] box_plot = Boxplot() box_plot = (box_plot.add_xaxis( xaxis_data=["expr 0", "expr 1", "expr 2", "expr 3", "expr 4"]).add_yaxis( series_name="", y_axis=box_plot.prepare_data(y_data)).set_global_opts( title_opts=opts.TitleOpts(pos_left="center", title="Michelson-Morley Experiment"), tooltip_opts=opts.TooltipOpts(trigger="item", axis_pointer_type="shadow"), xaxis_opts=opts.AxisOpts( type_="category", boundary_gap=True, splitarea_opts=opts.SplitAreaOpts(is_show=False), axislabel_opts=opts.LabelOpts(formatter="expr {value}"), splitline_opts=opts.SplitLineOpts(is_show=False), ), yaxis_opts=opts.AxisOpts(
y1 = [83, 88, 93, 79, 86, 89, 95, 90, 94, 92] y2 = [77, 79, 74, 82, 73, 78, 75, 85, 83, 81] y3 = [94, 93, 97, 89, 92, 97, 91, 85, 82, 96] y4 = [82, 81, 79, 85, 88, 73, 78, 82, 75, 79] y5 = [71, 78, 74, 73, 79, 77, 75, 74, 75, 81] y6 = [62, 65, 72, 66, 78, 73, 66, 68, 61, 63] # 绘制箱线图 # 注意用yi生成各科的[min, Q2, mid, Q1, max]数据,用于后续绘制箱线图 # 绘图的其它需求 # 1. 主题为马卡龙画风(macarons) # 2. 让箱线图的须更长,数据分布更易于观察:把y轴的最小值设为数据的最小值,而不是从0开始 # (这一步怎么设置的,建议查一下文档,写得明明白白) # 3. 全图标题“各科成绩” boxplot = ( Boxplot(init_opts=opts.InitOpts(theme="macarons")).set_global_opts( title_opts=opts.TitleOpts(title="各科成绩"), yaxis_opts=opts.AxisOpts(min_="dataMin") # dataMin怎么来的,官方文档上去查,写得明明白白 ).add_xaxis(xaxis_data=xData)) # 添加y轴数据 boxplot.add_yaxis(series_name="", y_axis=boxplot.prepare_data([y1, y2, y3, y4, y5, y6])) # 保存路径 resultPath = "./result" if not os.path.exists(path=resultPath): os.mkdir(path=resultPath) # 保存文件名 resultFileName = "scores_boxplot.html" # 执行保存 boxplot.render(path=os.path.join(resultPath, resultFileName))
(Boxplot(init_opts=opts.InitOpts(width="1600px", height="800px")).add_xaxis( xaxis_data=axis_data).add_yaxis( series_name="category0", y_axis=data[0]["boxData"], tooltip_opts=opts.TooltipOpts( formatter=produce_js_func("""function(param) { return [ 'Experiment ' + param.name + ': ', 'upper: ' + param.data[0], 'Q1: ' + param.data[1], 'median: ' + param.data[2], 'Q3: ' + param.data[3], 'lower: ' + param.data[4] ].join('<br/>') }"""))).add_yaxis( series_name="category1", y_axis=data[1]["boxData"], tooltip_opts=opts.TooltipOpts( formatter=produce_js_func("""function(param) { return [ 'Experiment ' + param.name + ': ', 'upper: ' + param.data[0], 'Q1: ' + param.data[1], 'median: ' + param.data[2], 'Q3: ' + param.data[3], 'lower: ' + param.data[4] ].join('<br/>') }"""))). add_yaxis(series_name="category2", y_axis=data[2]["boxData"], tooltip_opts=opts.TooltipOpts( formatter=produce_js_func("""function(param) { return [ 'Experiment ' + param.name + ': ', 'upper: ' + param.data[0], 'Q1: ' + param.data[1], 'median: ' + param.data[2], 'Q3: ' + param.data[3], 'lower: ' + param.data[4] ].join('<br/>') }"""))).set_global_opts( title_opts=opts.TitleOpts(title="Multiple Categories", pos_left="center"), legend_opts=opts.LegendOpts(pos_top="3%"), tooltip_opts=opts.TooltipOpts(trigger="item", axis_pointer_type="shadow"), xaxis_opts=opts.AxisOpts( name_gap=30, boundary_gap=True, splitarea_opts=opts.SplitAreaOpts( areastyle_opts=opts.AreaStyleOpts(opacity=1)), axislabel_opts=opts.LabelOpts(formatter="expr {value}"), splitline_opts=opts.SplitLineOpts(is_show=False)), yaxis_opts=opts.AxisOpts( type_="value", min_=-400, max_=600, splitarea_opts=opts.SplitAreaOpts(is_show=False)), datazoom_opts=[ opts.DataZoomOpts(type_="inside", range_start=0, range_end=20), opts.DataZoomOpts(type_="slider", xaxis_index=0, is_show=True) ]).render("multiple_categories.html"))
plotData = {"一等舱": [], "二等舱": [], "三等舱": []} # 扫描原始数据 for pClass, age in orgData: if pClass == 1: plotData["一等舱"].append(age) elif pClass == 2: plotData["二等舱"].append(age) elif pClass == 3: plotData["三等舱"].append(age) # 绘制船舱等级与年龄关系的箱线图,需求: # 1. 主题为light # 2. 标题为“舱位与年龄的分布” # 3. y轴起点是上述y轴所有数据的最小值 boxplot = (Boxplot(init_opts=opts.InitOpts(theme="light")).set_global_opts( title_opts=opts.TitleOpts(title="舱位与年龄的分布"), yaxis_opts=opts.AxisOpts(min_="dataMin")).add_xaxis( xaxis_data=list(plotData.keys()))) # 获取箱线图y轴[min, Q2, mid, Q1, max]序列 yData = boxplot.prepare_data( items=[plot_value for plot_value in plotData.values()]) print(yData) boxplot.add_yaxis(series_name="", y_axis=yData) # 保存路径 resultPath = "./result" if not os.path.exists(path=resultPath): os.mkdir(path=resultPath) # 保存文件名 resultFileName = "titanic_boxplot.html" # 执行保存 boxplot.render(path=os.path.join(resultPath, resultFileName))
def pollsummary(self,subset='masterCandle',dat_cursor=None,renderfile=False): # 初始化交易时间x轴 heartbeat = 300 # 如果未提供复盘时间游标,则默认使用最近交易日至收盘收盘时间的数据 if dat_cursor is None: dat_cursor = datetime.now().astimezone(timezone(cfg.STR_TIMEZONE)) int_latestViewDate = self.da.int_findClosestTradeDate(datetime.strftime(dat_cursor,'%Y%m%d') ) int_cursorDateTime = int(str(int_latestViewDate)+'150100') int_cursorDateStart = int(str(int_latestViewDate)+'000000') # 如提供复盘游标时间,则该时间为游标卡尺的最后时间,如日期为非交易日则自动调整为最近交易日的收盘时间 else: int_cursorDate = datetime.strftime(dat_cursor,'%Y%m%d') int_latestViewDate = self.da.int_findClosestTradeDate(int_cursorDate) if int_cursorDate==int_latestViewDate: # 如相等说明提供日期为有效交易日,则使用提供的小时分钟秒为游标终点时间,否则将自动替换为最近交易日的收盘时间点 int_cursorDateTime = int(datetime.strftime(dat_cursor,'%Y%m%d%H%M%S')) int_cursorDateStart = int(datetime.strftime(dat_cursor,'%Y%m%d')+'000000') else: int_cursorDateTime = int(str(int_latestViewDate) +'150100') int_cursorDateStart = int(str(int_latestViewDate) +'000000') beatRange = Radar.pop_pulseRange(int_latestViewDate,heartbeat) df_stockPoll = pd.read_sql(DataAgent.dbsession.query(Stock_poll).filter(Stock_poll.time_index>=int_cursorDateStart, Stock_poll.time_index<=int_cursorDateTime, Stock_poll.volume!=0).statement, DataAgent.dbsession.bind) df_indexPoll = pd.read_sql(DataAgent.dbsession.query(Index_poll).filter(Index_poll.time_index>=int_cursorDateStart, Index_poll.time_index<=int_cursorDateTime, Index_poll.volume!=0).statement, DataAgent.dbsession.bind) ''' data_store = libs.tryOpenH5(cfg.H5_FILE_POLL,mode='r') if 'stockPoll' in data_store: df_stockPoll = data_store['stockPoll'] df_stockPoll = df_stockPoll[df_stockPoll['volume']!=0] # 过滤掉数据中非交易中的证券 df_stockPoll = df_stockPoll[(df_stockPoll['time_index']<=int_cursorDateTime)&(df_stockPoll['time_index']>=int_cursorDateStart)] # 过滤掉和画图期间无关的数据 else: df_stockPoll = pd.DataFrame() df_indexPoll = data_store['indexPoll'] if 'indexPoll' in data_store else pd.DataFrame() data_store.close() ''' data_store = libs.tryOpenH5(cfg.H5_FILE_PRE,mode='r') # 如果按照输入日期没有找到当日任何盘中数据,则从入库历史数据中找到当日收盘数据用于填充 if len(df_indexPoll)==0: df_indexPoll = data_store['masterCandle'][data_store['masterCandle']['trade_date']==int_latestViewDate] df_indexPoll['time_index'] = int_cursorDateTime df_indexPoll['pct_change'] = round(df_indexPoll['adj_close']/df_indexPoll['pre_close']*100,2) df_indexPoll['per'] = np.nan if len(df_stockPoll)==0: df_stockPoll = data_store['masterCandle'][data_store['masterCandle']['trade_date']==int_latestViewDate] df_stockPoll['time_index'] = int_cursorDateTime df_stockPoll['pct_change'] = round((df_stockPoll['adj_close']-df_stockPoll['pre_close'])/df_stockPoll['pre_close']*100,2) df_stockPoll.rename(columns={'pe': 'per'}, inplace=True) # 如果入库历史数据中仍未找到任何数据则退出分析程序 if any([len(df_stockPoll)==0,len(df_indexPoll)==0]): data_store.close() print('stockPoll or indexPoll does not exist for the cursor date in the offline data store, process exiting...') return False ''' ------读取security code级别的聚合数据------- ''' if subset=='masterCandle': pass #data_store = libs.tryOpenH5(cfg.H5_FILE_PRE,mode='r') else: data_store.close() # 关闭之前打开的master data file data_store = libs.tryOpenH5('{}{}.dat'.format(cfg.H5_FILE_PATH,subset),mode='r') df_histRec = data_store['AggByCode'] data_store.close() self.poll_aggHistRec = df_histRec # sec code 级别的聚合数据,集合包含证券及指数,输出至对象变量 ''' ------读取security code级别的聚合结束------- ''' sr_intBeatIndex = pd.Series(beatRange.strftime('%Y%m%d%H%M%S').astype('int64')).rename(index='time_index') df_index = pd.merge(sr_intBeatIndex,df_indexPoll,how='left',left_on='time_index',right_on='time_index',suffixes=['','1']) df_stock = pd.merge(sr_intBeatIndex,df_stockPoll,how='left',left_on='time_index',right_on='time_index',suffixes=['','1']) df_stock = pd.merge(df_stock,df_histRec,how='left',left_on='ts_code',right_index=True,suffixes=['','1']) self.poll_index = df_index #将结果输出至analyitics对象用于方法外的访问 self.poll_stock = df_stock #将结果输出至analyitics对象用于方法外的访问 # libs.df_csv(cfg.PATH_BUGFILE,(df_stock,)) ''' --------所有by time index级别的聚合在这里完成 -----''' def popPctList(x): # 移除涨跌幅超过+- 10%的, 新股不受10%涨跌幅限制因此会干扰box chart显示 return [0]*3 if len(x)<3 else [i for i in x if abs(i)<11] def bigBox(x, up=True): # 中阳(阴)以上k线数量 if up: return list(x.loc[(x['box_size']>0) & (x['klineSML'].str.match(DataAgent.re_bigUpBoxPat))]['ts_code']) else: return list(x.loc[(x['box_size']<0) & (x['klineSML'].str.match(DataAgent.re_bigDnBoxPat))]['ts_code']) def breakThrough(x,up=True): if up: # 找到上穿有效末跌高点的 return list(x.loc[x['close']>=x['validPiv_dnHigh']]['ts_code']) else: # 找到跌破有效末升低点的 return list(x.loc[x['close']<=x['validPiv_upLow']]['ts_code']) df_stockBeatGrp = df_stock.groupby('time_index') df_stockBeatRollup = df_stockBeatGrp.agg({'ts_code': ['count'], 'pct_change': [popPctList], 'per': ['median']}) # function放入中括号内会生成两个层级列名,方便后面join成新列名.如无中括号将只产生一层列名 df_stockBeatRollup.columns = ['_'.join(x) for x in df_stockBeatRollup.columns.ravel()] df_stockBeatRollup['upThruList'] = df_stockBeatGrp.apply(lambda x: breakThrough(x,up=True)) if len(df_histRec) > 0 else np.nan df_stockBeatRollup['dnThruList']= df_stockBeatGrp.apply(lambda x: breakThrough(x,up=False)) if len(df_histRec) > 0 else np.nan df_stockBeatRollup['upThruCount'] = df_stockBeatRollup.apply(lambda row: len(row['upThruList']),axis=1) if len(df_histRec) > 0 else np.nan df_stockBeatRollup['dnThruCount']= df_stockBeatRollup.apply(lambda row: len(row['dnThruList']),axis=1) if len(df_histRec) > 0 else np.nan df_stockBeatRollup['upBigBoxList'] = df_stockBeatGrp.apply(lambda x: bigBox(x,up=True)) df_stockBeatRollup['dnBigBoxList'] = df_stockBeatGrp.apply(lambda x: bigBox(x,up=False)) df_stockBeatRollup['upBigBoxCount'] = df_stockBeatRollup.apply(lambda row: len(row['upBigBoxList']),axis=1) if len(df_histRec) > 0 else np.nan df_stockBeatRollup['dnBigBoxCount'] = df_stockBeatRollup.apply(lambda row: len(row['dnBigBoxList']),axis=1) if len(df_histRec) > 0 else np.nan ''' --------by time index级别的聚合结束 -------------------------------''' df_stockBeatRollup.astype(dtype= {'ts_code_count':'int32','upThruCount':'int32','dnThruCount':'int32','upBigBoxCount':'int32','dnBigBoxCount':'int32'}) self.poll_aggStkPoll = df_stockBeatRollup.loc[df_stockBeatRollup['ts_code_count']>0] # 只将有效结果输出到analytics对象属性中用于function以外的调用 ''' ------------------作图区域---------------------------''' per_median = round(self.poll_aggStkPoll.loc[self.poll_aggStkPoll.index==self.poll_aggStkPoll.index.max()].per_median.values[0],2) lst_xaxis = ['{}:{}:{}'.format(i.hour,i.minute,i.second) for i in beatRange] marketSummaryBoxPlot = Boxplot() lst_yaxis = np.around(marketSummaryBoxPlot.prepare_data(df_stockBeatRollup['pct_change_popPctList'].values), decimals=2).tolist() marketSummaryBoxPlot.add_xaxis(lst_xaxis) marketSummaryBoxPlot.add_yaxis("%", lst_yaxis) marketSummaryBoxPlot.set_global_opts(title_opts=opts.TitleOpts(title='整体涨跌幅分布 - {} (最后市盈率中位数:{})'.format(int_latestViewDate,str(per_median))), yaxis_opts=opts.AxisOpts(name="涨幅",min_=-10,max_=10), xaxis_opts=opts.AxisOpts(name="", axislabel_opts=opts.LabelOpts(is_show=False), axisline_opts=opts.AxisLineOpts(is_show=True), axistick_opts=opts.AxisTickOpts(is_show=True), splitline_opts=opts.SplitLineOpts(is_show=False),), legend_opts=opts.LegendOpts(is_show=False), ) lst_upThruBar = df_stockBeatRollup['upThruCount'].values.tolist() lst_dnThruBar = df_stockBeatRollup['dnThruCount'].values.tolist() lst_bigUpBoxBar = df_stockBeatRollup['upBigBoxCount'].values.tolist() lst_bigDnBoxBar = df_stockBeatRollup['dnBigBoxCount'].values.tolist() pivotCountChart = ( Bar() .add_xaxis(lst_xaxis) .add_yaxis( series_name="向上突破数量", yaxis_data=lst_upThruBar, label_opts=opts.LabelOpts(is_show=False), itemstyle_opts=opts.ItemStyleOpts(color="#FD625E"), ) .add_yaxis( series_name="向下突破数量", xaxis_index=1, yaxis_index=1, yaxis_data=lst_dnThruBar, label_opts=opts.LabelOpts(is_show=False), itemstyle_opts=opts.ItemStyleOpts(color="#01B8AA"), ) .add_yaxis( series_name="长阳线数量", xaxis_index=1, yaxis_index=1, yaxis_data=lst_bigUpBoxBar, label_opts=opts.LabelOpts(is_show=False), itemstyle_opts=opts.ItemStyleOpts(color="red"), ) .add_yaxis( series_name="长阴线数量", xaxis_index=1, yaxis_index=1, yaxis_data=lst_bigDnBoxBar, label_opts=opts.LabelOpts(is_show=False), itemstyle_opts=opts.ItemStyleOpts(color="green"), ) .set_global_opts( xaxis_opts=opts.AxisOpts(name="小时/分钟",), yaxis_opts=opts.AxisOpts( axislabel_opts=opts.LabelOpts(is_show=True), axisline_opts=opts.AxisLineOpts(is_show=True), axistick_opts=opts.AxisTickOpts(is_show=True), splitline_opts=opts.SplitLineOpts(is_show=True), ), legend_opts=opts.LegendOpts(is_show=True,pos_bottom='0%', pos_left="center"), ) ) gridChart = Grid() gridChart.add( marketSummaryBoxPlot, grid_opts=opts.GridOpts(pos_left="10%", pos_right="8%", pos_bottom='45%'), ) gridChart.add( pivotCountChart, grid_opts=opts.GridOpts(pos_left="10%", pos_right="8%", pos_top="55%"), ) fname = '{}marketSummary{}.html'.format(cfg.PATH_ANAFILE,int_latestViewDate) gridChart.render(fname) if renderfile else None return gridChart
def find_distribution_render(x,Y,x_name,query,table_path,answer): Max=0 y=[] data=[] label=[] boxplot = Boxplot() boxplot.add_xaxis(xaxis_data=label) for i in range(len(Y)): data.append(Y[i][1]) label.append(Y[i][0]) # boxplot.add_yaxis(series_name=Y[i][0],y_axis=boxplot.prepare_data(data)) for j in Y[i][1]: y.append(j) boxplot.add_yaxis(series_name="", y_axis=boxplot.prepare_data(data)) boxplot.set_global_opts( yaxis_opts=opts.AxisOpts(name="Distribution",axislabel_opts=opts.LabelOpts(font_size="100%"),name_textstyle_opts=opts.TextStyleOpts(font_size="100%")), legend_opts=opts.LegendOpts(is_show=False), xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=40,interval=0,font_size='100%'),name="Category",name_textstyle_opts=opts.TextStyleOpts(font_size="100%")), graphic_opts=[opts.GraphicText( graphic_item=opts.GraphicItem( left="center", top="bottom", z=100, ), graphic_textstyle_opts=opts.GraphicTextStyleOpts( # 可以通过jsCode添加js代码,也可以直接用字符串 text=['\n' + "Q:" + ' ' + query + '\n'+"\n" + 'A:' + ' ' + answer], font="14px Microsoft YaHei", graphic_basicstyle_opts=opts.GraphicBasicStyleOpts( fill="#333" ) ) )] ) bar=Bar({"theme": ThemeType.MACARONS}) bar.add_xaxis(xaxis_data=x) for i in range(len(Y)): max_index, max_number = max(enumerate(Y[i][1]), key=operator.itemgetter(1)) min_index, min_number = min(enumerate(Y[i][1]), key=operator.itemgetter(1)) if max_number>Max: Max=max_number bar.add_yaxis(Y[i][0],label_opts=opts.LabelOpts(is_show=False),y_axis=Y[i][1],markpoint_opts=opts.MarkPointOpts( data=[opts.MarkPointItem(coord=[max_index,max_number*1.01],value="MAX",name="最大值",itemstyle_opts=opts.ItemStyleOpts(opacity=0.6)),opts.MarkPointItem(coord=[min_index,min_number*1.05],value="MIN",name="最小值",itemstyle_opts=opts.ItemStyleOpts(opacity=0.6))] ), markline_opts=opts.MarkLineOpts( data=[opts.MarkLineItem(type_="average",name="平均值")] )) bar.set_global_opts( datazoom_opts=[opts.DataZoomOpts(range_start=10, range_end=90), opts.DataZoomOpts(type_="inside")], yaxis_opts=opts.AxisOpts( axislabel_opts=opts.LabelOpts(font_size="100%"), name_textstyle_opts=opts.TextStyleOpts(font_size="100%")), xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=40, interval=0, font_size='100%'), name=x_name, name_textstyle_opts=opts.TextStyleOpts(font_size="100%")), graphic_opts=[opts.GraphicText( graphic_item=opts.GraphicItem( left="center", top="bottom", ), graphic_textstyle_opts=opts.GraphicTextStyleOpts( # 可以通过jsCode添加js代码,也可以直接用字符串 text=['\n' + "Q:" + ' ' + query + '\n' + "\n"+'A:' + ' ' + answer], font="14px Microsoft YaHei", graphic_basicstyle_opts=opts.GraphicBasicStyleOpts( fill="#333" ) ) )] ) grid = Grid( init_opts=opts.InitOpts( width="100%", height="100%", renderer= globals.RenderType.SVG,)) grid.add(boxplot, grid_opts={'left':'20%','bottom':'34%'}) grid1 = Grid( init_opts=opts.InitOpts( width="100%", height="100%", renderer= globals.RenderType.SVG,)) grid1.add(bar,grid_opts={'left':'20%','bottom':'34%'}) option1=grid.dump_options_with_quotes() option1=json.loads(option1) option2=grid1.dump_options_with_quotes() option2=json.loads(option2) option={"option":[option1,option2],"query":query} return option
dom1, dom2, dom3 = [], [], [] list1 = [] for date, AQI in zip(data['Date'], data['AQI']): year = date.split('-')[0] if year in ['2015', '2016']: dom1.append(AQI) elif year in ['2017', '2018']: dom2.append(AQI) elif year in ['2019', '2020']: dom3.append(AQI) x = ['2015-2016年', '2017-2018年', '2019-2020年'] y = [dom1, dom2, dom3] boxplot = Boxplot() boxplot.add_xaxis(x) boxplot.add_yaxis('Delhi', boxplot.prepare_data(y)) boxplot.set_global_opts(title_opts=opts.TitleOpts( title='2015-2020印度德里AQI箱型图', pos_left='center', ), legend_opts=opts.LegendOpts( pos_left='left', orient='vertical', )) boxplot.render('boxplot_AQI.html')
# In[94]: look_support_money = article[['看一看', '点赞', '赞赏']] # 箱型图绘制 from pyecharts import options as opts from pyecharts.charts import Boxplot v1 = [ list(look_support_money['看一看']), list(look_support_money['点赞']), list(look_support_money['赞赏']) ] c = Boxplot(init_opts=opts.InitOpts(theme=ThemeType.VINTAGE, chart_id=4)) c.add_xaxis(["看一看", "点赞", '赞赏']) c.add_yaxis("", c.prepare_data(v1)) c.set_global_opts(title_opts=opts.TitleOpts(title="看一看,点赞,赞赏分布")) c.render("../output/看一看,点赞,赞赏.html") c.render_notebook() # ## 文章类型占比 # In[95]: kind = article['文章类型'].value_counts() # 绘制玫瑰图 from pyecharts import options as opts from pyecharts.charts import Pie
# In[138]: # vis from pyecharts import options as opts from pyecharts.charts import Boxplot v1 = [ [850, 740, 900, 1070, 930, 850, 950, 980, 980, 880, 1000, 980], [960, 940, 960, 940, 880, 800, 850, 880, 900, 840, 830, 790], ] v2 = [ [890, 810, 810, 820, 800, 770, 760, 740, 750, 760, 910, 920], [890, 840, 780, 810, 760, 810, 790, 810, 820, 850, 870, 870], ] c = Boxplot() c.add_xaxis(["expr1", "expr2"]) c.add_yaxis("A", c.prepare_data(v1)) c.add_yaxis("B", c.prepare_data(v2)) c.set_global_opts(title_opts=opts.TitleOpts(title="BoxPlot-基本示例")) c.render_notebook() # ## WordCloud # In[156]: # vis import pyecharts.options as opts from pyecharts.charts import WordCloud import jieba.analyse