目录
- """
- 学习中遇到问题没人解答?小编创建了一个Python学习交流QQ群:732481539
- 寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
- """
-
- # 柱状图/条形图
- from pyecharts.charts import Bar
-
- x_data = ["语文", "数学", "英语", "生物", "物理", "化学"] # x轴数据 数据必须是字符串
- y_data = [114, 95, 107, 81, 85, 87] # y轴数据 数据必须是整数或者小数
- bar = Bar() # 初始化图表
- bar.add_xaxis(x_data) # x轴
- bar.add_yaxis('成绩', y_data) # y轴
- bar.render("bar.html") # 渲染html文件
- # 折线图
- from pyecharts.charts import Line
-
- x_data = ['1月', '2月', '3月', '4月', '5月', '6月'] # x轴数据 数据必须是字符串
- y_data = [1123, 1153, 1089, 1207, 1298, 1123] # y轴数据 数据必须是整数或者小数
- line = Line() # 初始化图表
- line.add_xaxis(x_data) # x轴
- line.add_yaxis('月消费', y_data) # y轴
- line.render("line.html") # 渲染html文件
- # 散点图
- from pyecharts.charts import Scatter
-
- x_data = ['1月', '2月', '3月', '4月', '5月', '6月'] # x轴数据
- y_data = [1123, 1153, 1089, 1207, 1298, 1123] # y轴数据
- scatter = Scatter() # 初始化图表
- scatter.add_xaxis(x_data) # x轴
- scatter.add_yaxis('月消费', y_data) # y轴
- scatter.render("scatter.html") # 渲染html文件
- from pyecharts.charts import EffectScatter
-
- x_data = ['1月', '2月', '3月', '4月', '5月', '6月'] # x轴数据
- y_data = [1123, 1153, 1089, 1207, 1298, 1123] # y轴数据
- effectScatter = EffectScatter()
- effectScatter.add_xaxis(x_data) # x轴
- effectScatter.add_yaxis('月消费', y_data) # y轴
- effectScatter.render("effectScatter.html") # 渲染html文件
- # 箱型图
- from pyecharts.charts import Boxplot
- import random
-
- x_data = ['A', 'B', 'C', 'D', 'E', 'F']
- y_data = [[random.randint(100, 200) for i in range(10)] for item in x_data] #[[164, 102, 161, 106, 144, 186, 197, 186, 196, 125], [158, 131, 143, 111, 110, 175, 109, 107, 104, 153], [163, 174, 182, 142, 134, 158, 113, 149, 176, 183], [198, 142, 182, 125, 105, 149, 171, 122, 105, 156], [146, 126, 100, 164, 149, 117, 118, 170, 138, 155], [177, 110, 119, 178, 126, 164, 131, 176, 195, 134]]
-
- print(y_data)
- Boxplot = Boxplot()
- Boxplot.add_xaxis(x_data) # x轴
- Boxplot.add_yaxis('', Boxplot.prepare_data(y_data)) # y轴
- Boxplot.render("Boxplot.html") # 渲染html文件
- # k线图
- from pyecharts.charts import Kline
-
- date_list = ["2022/6/{}".format(i + 1) for i in range(30)]
- y_data = [
- [2320.26, 2320.26, 2287.3, 2362.94],
- [2300, 2291.3, 2288.26, 2308.38],
- [2295.35, 2346.5, 2295.35, 2345.92],
- [2347.22, 2358.98, 2337.35, 2363.8],
- [2360.75, 2382.48, 2347.89, 2383.76],
- [2383.43, 2385.42, 2371.23, 2391.82],
- [2377.41, 2419.02, 2369.57, 2421.15],
- [2425.92, 2428.15, 2417.58, 2440.38],
- [2411, 2433.13, 2403.3, 2437.42],
- [2432.68, 2334.48, 2427.7, 2441.73],
- [2430.69, 2418.53, 2394.22, 2433.89],
- [2416.62, 2432.4, 2414.4, 2443.03],
- [2441.91, 2421.56, 2418.43, 2444.8],
- [2420.26, 2382.91, 2373.53, 2427.07],
- [2383.49, 2397.18, 2370.61, 2397.94],
- [2378.82, 2325.95, 2309.17, 2378.82],
- [2322.94, 2314.16, 2308.76, 2330.88],
- [2320.62, 2325.82, 2315.01, 2338.78],
- [2313.74, 2293.34, 2289.89, 2340.71],
- [2297.77, 2313.22, 2292.03, 2324.63],
- [2322.32, 2365.59, 2308.92, 2366.16],
- [2364.54, 2359.51, 2330.86, 2369.65],
- [2332.08, 2273.4, 2259.25, 2333.54],
- [2274.81, 2326.31, 2270.1, 2328.14],
- [2333.61, 2347.18, 2321.6, 2351.44],
- [2340.44, 2324.29, 2304.27, 2352.02],
- [2326.42, 2318.61, 2314.59, 2333.67],
- [2314.68, 2310.59, 2296.58, 2320.96],
- [2309.16, 2286.6, 2264.83, 2333.29],
- [2282.17, 2263.97, 2253.25, 2286.33],
- ]
-
- kline = (Kline()
- .add_xaxis(date_list)
- .add_yaxis('', y_data)
- )
-
- kline.render("kline.html") # 渲染html文件
- # 热力图
- from pyecharts.charts import HeatMap
- import random
-
- data = [[i, j, random.randint(0, 100)] for i in range(24) for j in range(7)]
- hour_list = [str(i) for i in range(24)]
- week_list = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
-
- heat = (HeatMap()
- .add_xaxis(hour_list)
- .add_yaxis("", week_list, data)
- )
- heat.render("heat.html")
- # 象型图
- from pyecharts.charts import PictorialBar
-
- x_data = ['1月', '2月', '3月', '4月', '5月', '6月'] # x轴数据
- y_data = [1123, 1153, 1089, 1207, 1298, 1123] # y轴数据
- pictorialBar = PictorialBar() # 初始化图表
- pictorialBar.add_xaxis(x_data) # x轴
- pictorialBar.add_yaxis('月消费', y_data) # y轴
- pictorialBar.render("pictorialBar .html") # 渲染html文件
- # 层叠图Overlap_bar_line
- from pyecharts.charts import Bar
- from pyecharts.charts import Line
-
- x_data = ['1月', '2月', '3月', '4月', '5月', '6月']
- y_data_bar = [1123, 1153, 1089, 1207, 1298, 1123]
- y_data_line = [1300, 1200, 1100, 1400, 1500, 1200]
-
- bar = (Bar()
- .add_xaxis(x_data)
- .add_yaxis('', y_data_bar)
- )
-
- line = (Line()
- .add_xaxis(x_data)
- .add_yaxis('', y_data_line)
- )
-
- overlap = bar.overlap(line)
- overlap.render("overlap_bar_line .html")
- # 层叠图 Overlap_line_scatter
- from pyecharts import options as opts
- from pyecharts.charts import Line, Scatter
- from pyecharts.faker import Faker
-
- x = Faker.choose()
- line = (
- Line()
- .add_xaxis(x)
- .add_yaxis("商家A", Faker.values())
- .add_yaxis("商家B", Faker.values())
- .set_global_opts(title_opts=opts.TitleOpts(title="Overlap-line+scatter"))
- )
- scatter = (
- Scatter()
- .add_xaxis(x)
- .add_yaxis("商家A", Faker.values())
- .add_yaxis("商家B", Faker.values())
- )
- line.overlap(scatter)
- line.render("overlap_line_scatter.html")
Faker.values() 详解:生成7个随机整数,这7个随机整数一般是两位数和三位数的组合
class _Faker: clothes = ["衬衫", "毛衣", "领带", "裤子", "风衣", "高跟鞋", "袜子"] drinks = ["可乐", "雪碧", "橙汁", "绿茶", "奶茶", "百威", "青岛"] phones = ["小米", "三星", "华为", "苹果", "魅族", "VIVO", "OPPO"] fruits = ["草莓", "芒果", "葡萄", "雪梨", "西瓜", "柠檬", "车厘子"] animal = ["河马", "蟒蛇", "老虎", "大象", "兔子", "熊猫", "狮子"] cars = ["宝马", "法拉利", "奔驰", "奥迪", "大众", "丰田", "特斯拉"] dogs = ["哈士奇", "萨摩耶", "泰迪", "金毛", "牧羊犬", "吉娃娃", "柯基"] week = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"] week_en = "Saturday Friday Thursday Wednesday Tuesday Monday Sunday".split() clock = ( "12a 1a 2a 3a 4a 5a 6a 7a 8a 9a 10a 11a 12p " "1p 2p 3p 4p 5p 6p 7p 8p 9p 10p 11p".split() ) visual_color = [ "#313695", "#4575b4", "#74add1", "#abd9e9", "#e0f3f8", "#ffffbf", "#fee090", "#fdae61", "#f46d43", "#d73027", "#a50026", ] months = ["{}月".format(i) for i in range(1, 13)] provinces = ["广东", "北京", "上海", "江西", "湖南", "浙江", "江苏"] guangdong_city = ["汕头市", "汕尾市", "揭阳市", "阳江市", "肇庆市", "广州市", "惠州市"] country = [ "China", "Canada", "Brazil", "Russia", "United States", "Africa", "Germany", ] days_attrs = ["{}天".format(i) for i in range(30)] days_values = [random.randint(1, 30) for _ in range(30)]