1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
| from pyecharts.charts import * from pyecharts import options as opts from pyecharts.globals import CurrentConfig import pyecharts.faker as F from pyecharts.faker import Faker import numpy as np import pandas as pd
CurrentConfig.ONLINE_HOST = "https://cdn.kesci.com/lib/pyecharts_assets/"
class Draw(object): def __init__(self,x,y,ylabel,title,xname,yname): self.x=x self.y=y self.ylabel=ylabel self.title=title self.xname=xname self.yname=yname
def draw(self,tool): tool.add_xaxis(self.x) for i, j in enumerate(self.y): tool.add_yaxis(self.ylabel[i], j) tool.set_series_opts(markpoint_opts=opts.MarkPointOpts( data=[opts.MarkPointItem(type_="max", name="最大值"), opts.MarkPointItem(type_="min", name="最小值"), ], symbol='pin', symbol_size=45), markline_opts=opts.MarkLineOpts( data=[opts.MarkLineItem(type_="average", name="平均值")] ))
tool.set_global_opts(title_opts=opts.TitleOpts(title='工具栏显示') , toolbox_opts=opts.ToolboxOpts() ) return tool
class DrawLine(Draw): def __init__(self,x,y,ylabel,title,xname,yname): super(DrawLine, self).__init__(x,y,ylabel,title,xname,yname) tool=Line() self.d=self.draw(tool) def render(self,path): self.d.render(path)
def DrawHist(y, yname, title, bins=None): if bins == None: bins = np.linspace(min(y), max(y), 15) y = pd.cut(y, bins) value = pd.value_counts(y).sort_index() x_value = [f'{round(i.left, 2)}-{round(i.right, 2)}' for i in value.index]
fit=np.arange(1,len(x_value)+1) p1=np.polyfit(fit,value,11) n = list(p1)[::-1]
y1 = np.array([n[0]] * len(x_value)) for i in range(1, len(n)): y1 += x_value ** i * n[i]
value = value.tolist() c = ( Bar() .add_xaxis(x_value) .add_yaxis(yname, value, category_gap=0, color=Faker.rand_color()) .set_global_opts(title_opts=opts.TitleOpts(title=title)) .set_series_opts(label_opts=opts.LabelOpts(is_show=False)) ) x=DrawMulStack([c,DrawLine(fit,[y1],["n"],"n","n","n")]) return x
class DrawBar(Draw): def __init__(self,x,y,ylabel,title,xname,yname): super(DrawBar, self).__init__(x,y,ylabel,title,xname,yname) tool=Bar() self.d=self.draw(tool)
def render(self,path): self.d.render(path)
class DrawScatter(Draw): def __init__(self, x, y, ylabel, title, xname, yname): super(DrawScatter, self).__init__(x, y, ylabel, title, xname, yname) tool = Scatter() self.d = self.draw(tool) def render(self, path): self.d.render(path)
def DrawBox(x,y_data): box_plot = Boxplot() box_plot = ( box_plot.add_xaxis(xaxis_data=[i for i in x]) .add_yaxis(series_name="箱线图", y_axis=box_plot.prepare_data(y_data)) .set_global_opts( title_opts=opts.TitleOpts( pos_left="left", title="居左的标题" ), xaxis_opts=opts.AxisOpts( type_="category", boundary_gap=True, splitline_opts=opts.SplitLineOpts(is_show=False), ), yaxis_opts=opts.AxisOpts( type_="value", name="km/s minus 299,000", splitarea_opts=opts.SplitAreaOpts( is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1) ), ), ) .set_series_opts(tooltip_opts=opts.TooltipOpts(formatter="{b}: {c}")) ) return box_plot
def DrawMulStack(charts): grid = ( Grid(init_opts=opts.InitOpts(width="1000px", height="600px")) .add( charts[0], grid_opts=opts.GridOpts(pos_left="10%", pos_right="10%", pos_bottom="15%"), ) .add( charts[1], grid_opts=opts.GridOpts(pos_left="10%", pos_right="10%", pos_bottom="15%"), ) ) return grid
def DrawPie(x,y): pie = Pie() pie.add("", [list(z) for z in zip(y,x)], label_opts=opts.LabelOpts(position="left", formatter="{d}%"), radius=["30%", "50%"], rosetype="area" * 10) pie.set_global_opts( title_opts=opts.TitleOpts(title="饼图", pos_left="40%"), legend_opts=opts.LegendOpts( type_="scroll", pos_top="20%", pos_left="80%", orient="vertical" ) ) return pie
if __name__ == '__main__': x_data = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu'] y_data_1 = [123, 153, 89, 107, 98, 23] y_data_2 = [32, 213, 60, 167, 142, 45] l=DrawBar(x_data,[y_data_1,y_data_2],["系列1","系列2"],"题目","品牌","数量").d l.render("./Res/test01.html")
y_data = [ [980, 930, 650, 760, 810, 1000, 1000, 960, 960, ], [920, 930, 650, 760, 310, 1000, 100, 960, 960, ], [680, 930, 650, 260, 810, 1400, 1000, 960, 960, ], [780, 930, 650, 760, 810, 1000, 600, 930, 960, ], [980, 630, 650, 760, 810, 1000, 1000, 960, 960, ], ]
p=DrawPie(F.Faker.values(),F.Faker.choose()) p.render("./Res/test04.html")
|