def plot_one_y(df, title: str): df.index = pd.to_datetime(df['date']) df.sort_index(inplace=True) df = df.drop(['date'], axis=1) return (Line( init_opts=opts.InitOpts(width="1200px", height="400px")).add_xaxis( xaxis_data=df.index.strftime( '%Y-%m-%d').values.tolist()).add_yaxis( series_name=title.upper(), y_axis=np.round(df['r2'].values, 2).tolist(), is_smooth=True, label_opts=opts.LabelOpts(is_show=False), linestyle_opts=opts.LineStyleOpts(width=2), markline_opts=opts.MarkLineOpts( data=[opts.MarkLineItem(type_="average")]), ).set_global_opts( datazoom_opts=opts.DataZoomOpts(), legend_opts=opts.LegendOpts(pos_bottom="0%", pos_right='45%'), title_opts=opts.TitleOpts( title=title.upper(), pos_left='0%', ), tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"), toolbox_opts=opts.ToolboxOpts(is_show=True), xaxis_opts=opts.AxisOpts(boundary_gap=False), yaxis_opts=opts.AxisOpts( axislabel_opts=opts.LabelOpts(formatter="{value}"), splitline_opts=opts.SplitLineOpts(is_show=True), ), ).set_series_opts(markpoint_opts=opts.MarkPointOpts(data=[ opts.MarkPointItem(type_='max', name='Max'), opts.MarkPointItem(type_='min', name='Min') ]), ))
def other_line() -> Line: url = 'https://services1.arcgis.com/0MSEUqKaxRlEPj5g/arcgis/rest/services/cases_time/FeatureServer/0/query?f=json&where=Report_Date%3C%3D%272020-02-01%2015%3A59%3A59%27&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&orderByFields=Report_Date%20asc&resultOffset=0&resultRecordCount=2000&cacheHint=true' r_data = json.loads(requests.get(url).text) data = r_data['features'] #初始化json数据,为dict ['chinaTotal'] dates = [] numbers = [] for i in data: date = time.strftime( "%m.%d", time.localtime(i['attributes']['Report_Date'] / 1000)) dates.append(date) numbers.append(i['attributes']['Other_Locations']) c = ( Line(init_opts=opts.InitOpts(theme=ThemeType.VINTAGE)) #设置主题 .add_xaxis(dates) #直接列表 .add_yaxis('确诊', numbers).set_global_opts( title_opts=opts.TitleOpts(title="2019-nCov 海外国家疫情分析", subtitle=""), toolbox_opts=opts.ToolboxOpts(is_show=True), # ).set_series_opts( # 设置系列配置 markpoint_opts=opts.MarkPointOpts(data=[ opts.MarkPointItem(type_="max", name="最大值"), opts.MarkPointItem(type_="min", name="最小值"), ]), areastyle_opts=opts.AreaStyleOpts(opacity=0.5), label_opts=opts.LabelOpts(is_show=False), )) return c
def line_base(l1, l2) -> Line: lh_list = [y["total_box"] for y in l2] lh_list.extend([0 for _ in range(3)]) # 前面三天为0 c = (Line( init_opts=opts.InitOpts(bg_color="", page_title="总票房")).add_xaxis( [y["date"] for y in reversed(l1)]).add_yaxis( "哪吒之魔童降世", [y["total_box"] for y in reversed(l1)], is_smooth=True, markpoint_opts=opts.MarkPointOpts( data=[opts.MarkPointItem(type_="max")])).add_yaxis( "烈火英雄", reversed(lh_list), is_smooth=True, markpoint_opts=opts.MarkPointOpts( data=[opts.MarkPointItem( type_="max")])).set_global_opts( title_opts=opts.TitleOpts( title="总票房", subtitle_textstyle_opts={ "color": "red" }, subtitle="单位: 万元"), toolbox_opts=opts.ToolboxOpts())) return c.render("line.html")
def draw_charts(product_name, case_name, total_secs, qps, latency): secs = range(0, int(total_secs), int(total_secs / 100)) secs.append(int(total_secs)) c = (Line( init_opts=opts.InitOpts(width="1600px", height="800px") ).add_xaxis(xaxis_data=secs).add_yaxis( series_name="QPS", y_axis=qps, markpoint_opts=opts.MarkPointOpts(data=[ opts.MarkPointItem(type_="max", name="Max Value"), opts.MarkPointItem(type_="min", name="Min Value"), ]), markline_opts=opts.MarkLineOpts( data=[opts.MarkLineItem(type_="average", name="Average QPS")]), ).add_yaxis( series_name="Latency", y_axis=latency, markline_opts=opts.MarkLineOpts( data=[opts.MarkLineItem(type_="average", name="Average Latency")]), ).set_global_opts( title_opts=opts.TitleOpts(title="QPS", subtitle="DML only"), tooltip_opts=opts.TooltipOpts(trigger="axis"), toolbox_opts=opts.ToolboxOpts(is_show=True), xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False), ).render("sysbench-" + product_name + '-' + case_name + ".html"))
def line_markpoint(data) -> Line: level = data[2] city = data[0] # s=data[1] # print(s) title = "北成上广深折线图-个数" c = (Line().add_xaxis(level).add_yaxis( city[0], data[1][0], markpoint_opts=opts.MarkPointOpts( data=[opts.MarkPointItem(type_="max")])).add_yaxis( city[1], data[1][1], markpoint_opts=opts.MarkPointOpts( data=[opts.MarkPointItem(type_="max")])).add_yaxis( city[2], data[1][2], markpoint_opts=opts.MarkPointOpts( data=[opts.MarkPointItem(type_="min")])).add_yaxis( city[3], data[1][3], markpoint_opts=opts.MarkPointOpts( data=[opts.MarkPointItem(type_="max")])). add_yaxis( city[4], data[1][4], markpoint_opts=opts.MarkPointOpts( data=[opts.MarkPointItem(type_="max")])).set_global_opts( title_opts=opts.TitleOpts(title="北成上广深-折线图"))) return c
def paint_bar(self, x: list, collects: list, title: str, mark_point: bool = False): bar = Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT)) bar.add_xaxis(x) for collect in collects: for i, (name, unit, data) in enumerate(collect): bar.add_yaxis(f'{name}-单位:{unit}', data, yaxis_index=i) if i != 0: bar.extend_axis(yaxis=opts.AxisOpts( name='', type_='value', position='right', )) bar.set_global_opts(title_opts=opts.TitleOpts(title=title, pos_left='5%'), legend_opts=opts.LegendOpts(pos_bottom='0')) bar.set_series_opts( label_opts=opts.LabelOpts(position='top'), tooltip_opts=opts.TooltipOpts(formatter=f'{{b}}年{{a}}:{{c}}')) if mark_point: bar.set_series_opts( markpoint_opts=opts.MarkPointOpts(data=[ opts.MarkPointItem(type_='max', name='最大值'), opts.MarkPointItem(type_='min', name='最小值') ], symbol_size=80)) return bar
def bar_datazoom_inside() -> Timeline: tl = Timeline() for i in range(2014, 2020): c = ( Bar( init_opts=opts.InitOpts( animation_opts=opts.AnimationOpts( animation_delay=1000, animation_easing="elasticOut" ) ) ) .add_xaxis(list(zip(list(data总.set_index('类别').index)))) .add_yaxis("显示",list(data总["{}".format(i)])) .set_global_opts( title_opts=opts.TitleOpts(title="纵横小说月票榜"), datazoom_opts=opts.DataZoomOpts(type_="inside"), visualmap_opts=opts.VisualMapOpts(type_="color", max_=250000, min_=200,pos_right='20',pos_top='middle'), toolbox_opts=opts.ToolboxOpts(), ) .set_series_opts( label_opts=opts.LabelOpts(is_show=False), markpoint_opts=opts.MarkPointOpts( data=[ opts.MarkPointItem(type_="max", name="最大值"), opts.MarkPointItem(type_="min", name="最小值"), ] ), ) ) tl.add(c, "{}年".format(i)) return tl
def setrose(): x1 = ['2016年', '2017年', '2018年', '2019年', '2020年'] y1 = ['文学', 350.0, 345.0, 345.0, 355.0, 355.0] y2 = ['理学', 285.0, 290.0, 280.0, 290.0, 288.0] c = (Line(init_opts=opts.InitOpts( width="1600px", height="800px")).add_xaxis(xaxis_data=x1).add_yaxis( series_name="文学", y_axis=y1, markpoint_opts=opts.MarkPointOpts(data=[ opts.MarkPointItem(type_="max", name="最大值"), opts.MarkPointItem(type_="min", name="最小值"), ]), markline_opts=opts.MarkLineOpts( data=[opts.MarkLineItem(type_="average", name="平均值")]), ).add_yaxis( series_name="理学", y_axis=y2, markpoint_opts=opts.MarkPointOpts(data=[ opts.MarkPointItem(type_="max", name="最大值"), opts.MarkPointItem(type_="min", name="最小值"), ]), markline_opts=opts.MarkLineOpts(data=[ opts.MarkLineItem(type_="average", name="平均值"), opts.MarkLineItem(symbol="none", x="90%", y="max"), opts.MarkLineItem(symbol="circle", type_="max", name="最高点"), ]), ).set_global_opts( title_opts=opts.TitleOpts(title="近5年理学/文学国家线趋势"), tooltip_opts=opts.TooltipOpts(trigger="axis"), toolbox_opts=opts.ToolboxOpts(is_show=True), xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False), )) return c
def common_draw(df_list: List[pd.DataFrame], chart_type=Line, columns=[], name_field='security_id', render='html', file_name=None): if len(df_list) > 1: df_list = fill_with_same_index(df_list=df_list) chart = None if chart_type == Line: chart = get_default_line() assert len(columns) == 1 xdata = [to_time_str(timestamp) for timestamp in df_list[0].index] chart.add_xaxis(xdata) for df in df_list: series_name = df[df[name_field].notna()][name_field][0] if len(columns) == 1: ydata = df.loc[:, columns[0]].values.tolist() chart.add_yaxis(series_name, ydata, is_smooth=True, markpoint_opts=opts.MarkPointOpts( data=[opts.MarkPointItem(type_="min"), opts.MarkPointItem(type_="max")])) if render == 'html': chart.render(get_ui_path(file_name)) elif render == 'notebook': chart.render_notebook() return chart
def base_select() -> 'html': the_region = request.form["the_region_selected"] dfs_base = df_base.query("region=='{}'".format(the_region)) c = ( Bar(init_opts=opts.InitOpts(theme=ThemeType.ESSOS)) .add_xaxis(dfs_base['地区'].tolist()) .add_yaxis(the_region, dfs_base['count'].tolist()) .set_global_opts(title_opts=opts.TitleOpts(title=the_region, subtitle=""), xaxis_opts=opts.AxisOpts(name="地区", axislabel_opts={"rotate": 45}), datazoom_opts=[opts.DataZoomOpts()]) .set_series_opts( label_opts=opts.LabelOpts(is_show=False), markpoint_opts=opts.MarkPointOpts( data=[ opts.MarkPointItem(type_="max", name="最大值"), opts.MarkPointItem(type_="min", name="最小值"), ]) ) ) c.render("./static/tmp/echarts_2018bar.html") with open("./static/tmp/echarts_2018bar.html", encoding="utf8", mode="r") as f: plot_all = "".join(f.readlines()) data_str = dfs_base.to_html() return render_template('pyecharts.html', myechart=plot_all, the_res=data_str, the_select_region=regions_available_base, bottom_title="分析:柱状图", mark="base", )
def barDistance(data) -> Bar: """ -- 按运行里程区间统计车次数量 :param data:list:[[X轴-里程区间], [Y轴-车次数量]] :return: Bar """ distance_bar = ( Bar(init_opts=opts.InitOpts(width='1000px', height='500px')).add_xaxis(data[0]). add_yaxis("车次数量", data[1], color='#425066').set_global_opts( legend_opts=opts.LegendOpts(is_show=False), # 不显示图例 xaxis_opts=opts.AxisOpts( axislabel_opts=opts.LabelOpts(rotate=30), name='公里'), # 横坐标标签倾斜 yaxis_opts=opts.AxisOpts(name='次'), title_opts=opts.TitleOpts( title='按运行里程区间统计车次数量', title_textstyle_opts=opts.TextStyleOpts( font_size=20))).set_series_opts( label_opts=opts.LabelOpts(is_show=False), markline_opts=opts.MarkLineOpts( symbol='circle', precision=0, data=[ opts.MarkLineItem(name='平均', type_='average') ]), # 平均值线 markpoint_opts=opts.MarkPointOpts(data=[ opts.MarkPointItem(type_="max", name="Max"), opts.MarkPointItem(type_="min", name="Min") ]), )) return distance_bar
def barSpeed(data) -> Bar: """ -- 按列车类型统计运行速度 :param data:list:[[X轴-类型名称], [Y轴-速度]] :return: Bar """ speed_bar = ( Bar(init_opts=opts.InitOpts(width='1000px', height='500px')).add_xaxis(data[0]). add_yaxis("车次数量", data[1], color='#8c4356').set_global_opts( legend_opts=opts.LegendOpts(is_show=False), # 不显示图例 xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts( rotate=0)), # 横坐标标签倾斜 yaxis_opts=opts.AxisOpts(name='公里/小时'), title_opts=opts.TitleOpts( title='按列车类型统计运行速度', title_textstyle_opts=opts.TextStyleOpts( font_size=20))).set_series_opts( label_opts=opts.LabelOpts(is_show=False), markline_opts=opts.MarkLineOpts( symbol='circle', precision=0, data=[ opts.MarkLineItem(name='平均', type_='average') ]), # 平均值线 markpoint_opts=opts.MarkPointOpts(data=[ opts.MarkPointItem(type_="max", name="Max"), opts.MarkPointItem(type_="min", name="Min") ]), )) return speed_bar
def line_pf() -> Line: c = (Line(opts.InitOpts( bg_color='#FFFFFF', width='800px', height='500px')).add_xaxis(arr).add_yaxis( "功率因数", val[13], color='LightSkyBlue', is_symbol_show=False, markline_opts=opts.MarkLineOpts( data=[opts.MarkLineItem(type_="average")]), areastyle_opts=opts.AreaStyleOpts(opacity=0.4), markpoint_opts=opts.MarkPointOpts(data=[ opts.MarkPointItem(type_="max"), opts.MarkPointItem(type_="min") ])).add_yaxis( "下限(0.9)", paras[5], linestyle_opts=opts.LineStyleOpts(width=1.5, type_='dashed'), is_symbol_show=False).set_series_opts( label_opts=opts.LabelOpts(is_show=False)).set_global_opts( title_opts=opts.TitleOpts( title='功率因数趋势图', subtitle='日期:' + df.Date.iloc[0] + ' to ' + df.Date.iloc[-1], ))) return c
def line_markpoint(): obj_l = Line() obj_l.add_xaxis(Faker.choose()) obj_l.add_yaxis("商家A", Faker.values(), markpoint_opts=opts.MarkPointOpts( data=[opts.MarkPointItem(type_="min"),opts.MarkPointItem(type_="max")], ) ) obj_l.add_yaxis("商家B", Faker.values(), markpoint_opts=opts.MarkPointOpts( data=[opts.MarkPointItem(type_="min"),opts.MarkPointItem(type_="max")] ) ) obj_l.set_series_opts( # 该参数设置在这里对商家A,商家B都起作用 # 设置该参数,则x对应的y值会显示出来,该参数默认为True label_opts=opts.LabelOpts(is_show=False) ) obj_l.set_global_opts( title_opts=opts.TitleOpts(title="Line-MarkPoint(最大最小值)", subtitle="最大最小值标注"), xaxis_opts=opts.AxisOpts( name="x轴", # 坐标轴两边留白策略,类目轴和非类目轴的设置和表现不一样,可设置x轴刻度顶格 boundary_gap=True, # 类目轴中在 boundaryGap 为 true 的时候有效,可以保证刻度线和标签对齐。 axistick_opts=opts.AxisTickOpts(is_align_with_label=True) ), ) return obj_l
def line_base(self, date: list, price: list) -> Line: line = ( Line() .add_xaxis(xaxis_data=date) .add_yaxis( series_name="价格", y_axis=price, markpoint_opts=opts.MarkPointOpts( data=[ opts.MarkPointItem(type_="max", name="最大值"), opts.MarkPointItem(type_="min", name="最小值"), ] ), markline_opts=opts.MarkLineOpts( data=[opts.MarkLineItem(type_="average", name="平均值")] ), ) .set_global_opts( title_opts=opts.TitleOpts(title="历史价格记录"), tooltip_opts=opts.TooltipOpts(trigger="axis"), toolbox_opts=opts.ToolboxOpts(is_show=False), xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False), ) ) return line
def barTime(self, data) -> Bar: # 进出港航班数量按小时统计 time_bar = ( Bar(init_opts=opts.InitOpts(width='1000px', height='500px')). add_xaxis(data[0][0]).add_yaxis( "{}航班数量".format(data[2]), data[0][1], color='#1685a9').set_series_opts( label_opts=opts.LabelOpts(is_show=False), markpoint_opts=opts.MarkPointOpts(data=[ opts.MarkPointItem(type_="max", name="Max"), opts.MarkPointItem(type_="min", name="Min"), ]), markline_opts=opts.MarkLineOpts( symbol='circle', precision=0, data=[opts.MarkLineItem(name='平均', type_='average')])). set_global_opts( legend_opts=opts.LegendOpts(is_show=False), # 不显示图例 xaxis_opts=opts.AxisOpts(name_rotate=30), # 【【【X坐标轴标签倾斜,不好使】】】 title_opts=opts.TitleOpts( title='{}航班数量按小时统计'.format(data[2]), title_textstyle_opts=opts.TextStyleOpts( font_size=20), # 设置标题大小 subtitle='{},全国共{}航班{}架次。[数据来源:ctrip.com]'.format( self.today_ymd, data[2], data[1])))) return time_bar
def enroll(): global wid, hei data = analysis_data.enroll_rate() data['true_rate'] = data['true_rate'].apply(lambda x: float(x) * 10) print(data['year'].values) c = (Line(init_opts=opts.InitOpts(width=wid, height=hei)).add_xaxis( [x for x in data['year'].values]).add_yaxis( '报名人数', data['num_people'].values, is_smooth=True, markpoint_opts=opts.MarkPointOpts( data=[opts.MarkPointItem(type_="max")])).add_yaxis( "实际录取人数", data['true_num'].values, is_smooth=True, markpoint_opts=opts.MarkPointOpts( data=[opts.MarkPointItem(type_="max")]), ).add_yaxis( "实际录取率", [round(x, 2) for x in data['true_rate'].values], is_smooth=True, markpoint_opts=opts.MarkPointOpts( data=[opts.MarkPointItem(type_="max")]), ).set_global_opts(title_opts=opts.TitleOpts(title="全国高考走势图"))) return c
def barCompanyOut(data) -> Bar: # 全国民航机场进出港航班数量(TOP 15) company_out_bar = ( Bar(init_opts=opts.InitOpts(width='1000px', height='500px')).add_xaxis(data[0]). add_yaxis("航空公司", data[1], color='#574266').set_global_opts( legend_opts=opts.LegendOpts(is_show=False), # 不显示图例 datazoom_opts=[ opts.DataZoomOpts(), opts.DataZoomOpts(type_="inside") ], # DataZoom类型 title_opts=opts.TitleOpts( title='本周航空公司出港航班数量统计', title_textstyle_opts=opts.TextStyleOpts( font_size=20), # 设置标题大小 subtitle='{}-{},本周发送航班最多的航空公司是:{},{},{}。[数据来源:ctrip.com]'. format( data[2], data[3], data[4], data[5], data[6]))).set_series_opts( label_opts=opts.LabelOpts(is_show=False), # 不显示标签值 markpoint_opts=opts.MarkPointOpts(data=[ opts.MarkPointItem(type_="max", name="Max"), opts.MarkPointItem(type_="min", name="Min"), ]), # 最大值,最小值 markline_opts=opts.MarkLineOpts( symbol='circle', precision=0, data=[ opts.MarkLineItem(name='平均', type_='average') ]) # 平均值线 )) return company_out_bar
def set_process() -> Bar: """ 进程调度比较图 :return: """ index = ['时间片轮转调度', '短进程优先调度', '非剥夺优先级调度'] data = [217, 459, 324] bar = ( Bar() .add_xaxis(index) .add_yaxis(series_name='三大调度算法', yaxis_data=data) .set_global_opts(title_opts=opts.TitleOpts(title='三大算法调度性能分析'), xaxis_opts=opts.AxisOpts(type_='category', name='算法名称'), yaxis_opts=opts.AxisOpts(type_='value', name='获胜次数', axislabel_opts=opts.LabelOpts(formatter='{value}/次')), toolbox_opts=opts.ToolboxOpts(), tooltip_opts=opts.TooltipOpts(axis_pointer_type='cross') ) .set_series_opts(markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(name='最大值', type_='min'), opts.MarkPointItem(name='最大值', type_='max')]), 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(0, 77, 167, 1)' }], false)"""), "barBorderRadius": [30, 30, 30, 30], "shadowColor": 'rgb(0, 160, 221)', }} ) ) return bar
def three_lines_base(): c = ( Line().add_xaxis(genre).add_yaxis( 'viewing', view, #设置标签指出最高值 markpoint_opts=opts.MarkPointOpts( data=[opts.MarkPointItem(type_="max")]), ).add_yaxis( 'coins', coin, # 设置标签指出最高值 markpoint_opts=opts.MarkPointOpts( data=[opts.MarkPointItem(type_="max")]), ).add_yaxis( 'favorite', favorite, # 设置标签指出最高值 markpoint_opts=opts.MarkPointOpts( data=[opts.MarkPointItem(type_="max")]), ).set_global_opts( title_opts=opts.TitleOpts( title='viewing amount grouped by categories'), #将分区名旋转45度来保证所有分区都能显示 xaxis_opts=opts.AxisOpts( axislabel_opts={'rotate': 45})).dump_options_with_quotes()) return c
def mark_fun(date, action, return_rate, mark_list): if action == 'buy': mark_list.append( opts.MarkPointItem( name="", coord=[date, return_rate], value=return_rate, itemstyle_opts=opts.ItemStyleOpts(color='#FF0000'))) elif action == 'sell': mark_list.append( opts.MarkPointItem( name="", coord=[date, return_rate], value=return_rate, itemstyle_opts=opts.ItemStyleOpts(color='#008000'))) elif action == 'close': mark_list.append( opts.MarkPointItem( name="", coord=[date, return_rate], value=return_rate, itemstyle_opts=opts.ItemStyleOpts(color='#0000FF'))) else: mark_list.append( opts.MarkPointItem( name="", coord=[date, return_rate], value=return_rate, itemstyle_opts=opts.ItemStyleOpts(color='#FFA500')))
def gen_mark_points_data_list(): data_points_list = [ opts.MarkPointItem(type_="max", name="最大值"), opts.MarkPointItem(type_="min", name="最小值"), ] return data_points_list
def drawbar(arrt, value, name): # 图表初始化配置 init_opts = opts.InitOpts(page_title=name, height="700px") bar = Bar(init_opts=init_opts) # 标题配置 title = opts.TitleOpts(title=name, pos_left='center') # 图例配置 legend_opts = opts.LegendOpts(pos_top="5%", pos_left="15%") # 工具箱配置 # feature = opts.ToolBoxFeatureOpts(save_as_image=True, restore=True, data_view=True, data_zoom=True) # 工具箱配置 toolbox_opts = opts.ToolboxOpts(pos_top="5%", pos_right="30%") bar.set_global_opts( title_opts=title, legend_opts=legend_opts, toolbox_opts=toolbox_opts, # 区域缩放配置项 datazoom_opts=opts.DataZoomOpts(), ) # add_yaxis bar.add_xaxis(arrt) # 渐变色 bar.add_yaxis( "", value, gap="0%", category_gap="30%", # 自定义颜色 itemstyle_opts=opts.ItemStyleOpts( color=JsCode("""new echarts.graphic.LinearGradient(0, 0, 1, 0, [{ offset: 0, color: 'rgb(39, 117, 182)' }, { offset: 0.5, color: 'rgb(147, 181,207)' }, { offset: 1, color: 'rgb(35, 118, 183)' }], false)""")), ) bar.set_series_opts( label_opts=opts.LabelOpts(is_show=False), markline_opts=opts.MarkLineOpts(data=[ opts.MarkLineItem(type_="min", name="最小值"), opts.MarkLineItem(type_="max", name="最大值"), opts.MarkLineItem(type_="average", name="平均值"), ]), markpoint_opts=opts.MarkPointOpts(data=[ opts.MarkPointItem(type_="max", name="最大值"), opts.MarkPointItem(type_="min", name="最小值"), opts.MarkPointItem(type_="average", name="平均值"), ]), ) bar.render('{0}.html'.format(name))
def sentiment_analysis(self): with open(r'./用户影评相关数据/' + self.filmname + '用户影评相关信息.json', 'r', encoding='UTF-8') as f: t1 = json.load(f, strict=False) self.textBrowser.append("开始生成" + self.filmname + "的情感分析图......") QApplication.processEvents() # 取出里面的数据 comment_list = [] for each in t1: comment_list.append(each['用户评论']) # 存储情感数据 sentimentslist = [] for i in comment_list: s = round(SnowNLP(i).sentiments, 2) sentimentslist.append(s) # 对数据进行处理,计算出各个得分的个数 result = {} for i in set(sentimentslist): result[i] = sentimentslist.count(i) info = sorted(result.items(), key=lambda x: x[0], reverse=False) # dict的排序方法 attr, val = [], [] for each in info[:-1]: attr.append(str(each[0])) val.append(each[1]) e = (Line(init_opts=opts.InitOpts( width="665px", height="500px")).add_xaxis(attr).add_yaxis( "评论情感分析折线图", val, markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem()]), is_smooth=True, ).set_global_opts( tooltip_opts=opts.TooltipOpts(is_show=True), toolbox_opts=opts.ToolboxOpts( is_show=True, pos_right="30%", ), title_opts=opts.TitleOpts(title="评论情感分析折线图")).render( "./爬虫数据关联可视化/" + self.filmname + "影评可视化数据/line_markpoint_custom.html")) QApplication.processEvents() self.emotion_pic = (Line(init_opts=opts.InitOpts( width="665px", height="500px")).add_xaxis(attr).add_yaxis( "评论情感分析折线图", val, markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem()]), is_smooth=True, ).set_global_opts(tooltip_opts=opts.TooltipOpts(is_show=True), toolbox_opts=opts.ToolboxOpts( is_show=True, pos_right="30%", ), title_opts=opts.TitleOpts(title="评论情感分析折线图"))) self.saveflag = '3' self.textBrowser.append(self.filmname + "的情感分析图生成完毕!") QApplication.processEvents() self.show_sentiment_analysis() QApplication.processEvents()
def line_chart(): # 折线图 line = Line() x_y_axis = get_chart_data() line.add_xaxis(x_y_axis[0]) for j in x_y_axis[1]: for key, value in j.items(): line.add_yaxis(key, value, areastyle_opts=opts.AreaStyleOpts(opacity=1), label_opts=opts.LabelOpts(is_show=False)) line.set_series_opts( # linestyle_opts=opts.LineStyleOpts( # width=5, # opacity=1, # type_='solid', # ) markpoint_opts=opts.MarkPointOpts(data=[ opts.MarkPointItem(type_="max", name="最大值"), opts.MarkPointItem(type_="min", name="最小值"), ]), ) line.set_global_opts( title_opts=opts.TitleOpts( title="API耗时统计", subtitle="生产环境", pos_left="30%", title_textstyle_opts=opts.TextStyleOpts(color='red'), subtitle_textstyle_opts=opts.TextStyleOpts(color='blue')), xaxis_opts=opts.AxisOpts( name="运行时间", type_="category", boundary_gap=False, # axislabel_opts=opts.LabelOpts(rotate=15) ), yaxis_opts=opts.AxisOpts(name="实际的响应时间(单位:秒)", # min_=0, # max_=20 ), legend_opts=opts.LegendOpts(type_='scroll', selected_mode='multiple', pos_left='right', pos_top='10%', orient='vertical'), tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="line"), toolbox_opts=opts.ToolboxOpts(is_show=True, pos_left='right'), visualmap_opts=opts.VisualMapOpts(is_show=True, type_="size", min_=0, max_=20, range_text=["最大值", "最小值"]), datazoom_opts=[opts.DataZoomOpts(range_start=50, range_end=100)], ) line = line.dump_options_with_quotes() return line
def draw_line(cls, title: str, xaxis_data: List[str], yaxis_data: pd.DataFrame) -> Line: """ 根据df内容绘制折线图 :param title: 标题 :param xaxis_data: 横轴数据 :param yaxis_data: 纵轴绘制数据 :return: """ # 获取dataframe最大最小值 min_data = yaxis_data.min().min() max_data = yaxis_data.max().max() c = ( Line(init_opts=opts.InitOpts( width=DEFAULT_WIDTH)).add_xaxis(xaxis_data).set_series_opts( label_opts=opts.LabelOpts(is_show=True)). set_global_opts( title_opts=opts.TitleOpts(title=title, pos_left="0%"), toolbox_opts=opts.ToolboxOpts(), # 显示工具箱 tooltip_opts=opts.TooltipOpts(is_show=True), axispointer_opts=opts.AxisPointerOpts( is_show=True, type_="none"), # 指针移动时显示所有数值 legend_opts=opts.LegendOpts( is_show=True, selected_mode="multiple", # pos_bottom="0%", # pos_right="0%", # orient="vertical", ), # 显示图例说明 datazoom_opts=[ opts.DataZoomOpts( range_start=0, range_end=100, orient="vertical", pos_left=DATAZOOM_VERTICAL_POS_LEFT, ), opts.DataZoomOpts(range_start=0, range_end=100, orient="horizontal"), ], # 增加缩放配置横纵轴都支持缩放 # visualmap_opts = opts.VisualMapOpts(max_ = max_data, min_ = min_data, pos_bottom="10%") )) # 遍历dataframe 依次添加数据到y轴 column_list = yaxis_data.columns.tolist() for column in column_list: c.add_yaxis( column, yaxis_data[column].tolist(), markpoint_opts=opts.MarkPointOpts(data=[ opts.MarkPointItem(type_="min", symbol_size=60), opts.MarkPointItem(type_="max", symbol_size=60), ]), ) return c
def bar_markpoint_type() -> Bar: c = (Bar().add_xaxis( Faker.choose()).add_yaxis("商家A", Faker.values()).add_yaxis( "商家B", Faker.values()).set_global_opts(title_opts=opts.TitleOpts( title="Bar-MarkPoint(指定类型)")).set_series_opts( label_opts=opts.LabelOpts(is_show=False), markpoint_opts=opts.MarkPointOpts(data=[ opts.MarkPointItem(type_="max", name="最大值"), opts.MarkPointItem(type_="min", name="最小值"), opts.MarkPointItem(type_="average", name="平均值"), ]), )) return c
def line_markpoint() -> Line: c = (Line().add_xaxis(Faker.choose()).add_yaxis( "商家A", Faker.values(), markpoint_opts=opts.MarkPointOpts( data=[opts.MarkPointItem(type_="min")]), ).add_yaxis( "商家B", Faker.values(), markpoint_opts=opts.MarkPointOpts( data=[opts.MarkPointItem(type_="max")]), ).set_global_opts(title_opts=opts.TitleOpts(title="Line-MarkPoint"))) return c
def bar_border_radius(): c = ( Bar(init_opts=opts.InitOpts( animation_opts=opts.AnimationOpts( # 柱状图显示效果动画控制代码 animation_delay=500, animation_easing="cubicOut"), theme=ThemeType.MACARONS, # 柱状图显示主题 page_title="Bar Learn", # 设置html页面标题 )) # .reversal_axis() # 翻转XY轴 .add_xaxis(["草莓", "芒果", "葡萄", "雪梨", "西瓜", "柠檬", "车厘子"]).add_yaxis( "A", Faker.values(), category_gap="50%", # 柱间距对应的控制代码 markpoint_opts=opts.MarkPointOpts(), is_selected=True # A系列柱子是否显示对应的控制代码 ).set_global_opts( title_opts=opts.TitleOpts( # 标题 title="Bar-参数使用例子", subtitle="副标题"), toolbox_opts=opts.ToolboxOpts(), # toolbox 工具箱配置 yaxis_opts=opts.AxisOpts(position="right", name="Y轴"), # Y轴右侧控制 datazoom_opts=opts.DataZoomOpts(), # 数据区域放大缩小设置 ).set_series_opts( itemstyle_opts={ # A系列柱子颜色渐变对应的控制代码 "normal": { "color": JsCode(""" new echarts.graphic.LinearGradient( 0, 0, 0, 1, [{ offset: 0, color: 'rgba(0, 244, 255, 1)'}, { offset: 1, color: 'rgba(0, 77, 167, 1)'}], false)"""), "barBorderRadius": [6, 6, 6, 6], "shadowColor": 'rgb(0, 160, 221)', } }, # A系列柱子最大和最小值标记点对应的控制代码 markpoint_opts=opts.MarkPointOpts(data=[ opts.MarkPointItem(type_="max", name="最大值"), opts.MarkPointItem(type_="min", name="最小值"), ]), # A系列柱子最大和最小值标记线对应的控制代码 markline_opts=opts.MarkLineOpts(data=[ opts.MarkLineItem(type_="min", name="最小值"), opts.MarkLineItem(type_="max", name="最大值") ]))) return c
def foregin_history_all(): path = readPath + '/各国历史疫情信息' path2 = savaPath + '/各国历史疫情信息' dwf.createFile(path2) Filelist = [] # 将当前文件夹内的所有表名存放此列表 for home, dirs, files in os.walk(path): for filename in files: Filelist.append(filename) for i in Filelist: data = pd.read_excel(readPath + '/各国历史疫情信息/%s' % (i), index=False) n = i[:-5] # 只提取国家名,不要后缀(.xlsx) y1_confirm = data['confirm'] y2_confirm_add = data['confirm_add'] y3_heal = data['heal'] y4_dead = data['dead'] date_list = [] for j in data['date']: date_list.append(j) x = date_list lines = ( Line() .add_xaxis(xaxis_data=x) .add_yaxis(series_name='确诊人数', y_axis=y1_confirm, is_symbol_show=True, label_opts=opts.LabelOpts(is_show=False), markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="max"), ])) .add_yaxis(series_name='增加确诊人数', y_axis=y2_confirm_add, is_symbol_show=True, label_opts=opts.LabelOpts(is_show=False), markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="max"), ])) .add_yaxis(series_name='治愈人数', y_axis=y3_heal, is_symbol_show=True, label_opts=opts.LabelOpts(is_show=False), markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="max"), ])) .add_yaxis(series_name='死亡人数', y_axis=y4_dead, is_symbol_show=True, label_opts=opts.LabelOpts(is_show=False), markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="max"), ])) .set_global_opts(title_opts=opts.TitleOpts(title="%s" % (n) + "疫情走势", subtitle="数据来源:腾讯新闻"), yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(font_size=10, interval=3))) ) # lines.render(savaPath + '\\daliy_changes_view\\各国历史疫情信息\\%s.html' % (n), index=False) if flag: dwf.write_to_file(savaPath + '/各国历史疫情信息/%s.txt' % (n),str(lines.dump_options_with_quotes())) page.add(lines)