1. 程式人生 > 程式設計 >基於Python pyecharts實現多種圖例程式碼解析

基於Python pyecharts實現多種圖例程式碼解析

詞雲圖

from pyecharts.charts import WordCloud
def word1():
  words= [
    ("Sam S Club",10000),("Macys",6181),("Amy Schumer",4386),("Jurassic World",4055),("Charter Communications",2467),("Chick Fil A",2244),("Planet Fitness",1868),("Pitch Perfect",1484),("Express",1112),("Home",865),("Johnny Depp",847),("Lena Dunham",582),("Lewis Hamilton",555),("KXAN",550),("Mary Ellen Mark",462),("Farrah Abraham",366),("Rita Ora",360),("Serena Williams",282),("NCAA baseball tournament",273),("Point Break",265),]
  worldcloud = (
    WordCloud()
    .add("",words,word_size_range=[20,100])
    .set_global_opts(title_opts=opt.TitleOpts(title="WorldCloud-shape-diamond"))
  )
  # worldcloud = (
  #   WordCloud()
  #   .add("",100],shape=SymbolType.DIAMOND)
  #   .set_global_opts(title_opts=opt.TitleOpts(title="WorldCloud-shape-diamond"))
  # )
  worldcloud.render("wordl.html")
  os.system("wordl.html")

效果如下:

基於Python pyecharts實現多種圖例程式碼解析

散點圖

from pyecharts.charts import Scatter
import numpy as np

def sca():
  x_data = np.linspace(0,10,30)
  y1_data = np.sin(x_data)
  y2_data = np.cos(x_data)
  # 繪製散點圖
  # 設定圖表大小
  figsise = opt.InitOpts(width="800px",height="600px")
  scatter = Scatter(init_opts=figsise)
  # 新增資料
  scatter.add_xaxis(xaxis_data=x_data)
  scatter.add_yaxis(series_name="sin(x)散點圖",#名稱
           y_axis=y1_data,# 資料
           label_opts=opt.LabelOpts(is_show=False),# 資料不顯示
           symbol_size=15,# 設定散點的大小
           symbol="triangle" # 設定散點的形狀
           )
  scatter.add_yaxis(series_name="cos(x)散點圖",y_axis=y2_data,label_opts=opt.LabelOpts(is_show=False))
  scatter.render()
  os.system("render.html")

效果如下:

基於Python pyecharts實現多種圖例程式碼解析

餅狀圖

from pyecharts.charts import Pie
from pyecharts import options as optfrom pyecharts.faker import Faker as fa

def pie1():
  pie = (
    Pie()
    .add("",[list(z) for z in zip(fa.choose(),fa.values())])
    .set_global_opts(title_opts=opt.TitleOpts(title="pie-基本示例"))
    .set_series_opts(label_opts=opt.LabelOpts(formatter="{b}:{c}"))
  )
  pie.render()
  os.system("render.html")

def pie2():
  pie = (
    Pie()
      .add("",fa.values())],radius=["40%","75%"])
      .set_global_opts(title_opts=opt.TitleOpts(title="pie-示例"),legend_opts=opt.LegendOpts(
                 orient="vertical",pos_top="15%",pos_left="2%"
               ))
      .set_series_opts(label_opts=opt.LabelOpts(formatter="{b}:{c}"))
  )
  pie.render()
  os.system("render.html")


def pie3():
  pie = (
    Pie()
      .add("","75%"],center=["25%","50%"],rosetype="radius",label_opts=opt.LabelOpts(is_show=False))

      .add("",radius=["30%",center=["75%",rosetype="area")

      .set_global_opts(title_opts=opt.TitleOpts(title="pie-玫瑰圖示例"))

  )
  pie.render()
  os.system("render.html")

def pie4():
  # 多餅圖顯示
  pie = (
    Pie()
    .add(
      "",[list(z) for z in zip(["劇情","其他"],[25,75])],center=["20%","30%"],radius=[40,60]
    )
    .add(
      "",[list(z) for z in zip(["奇幻",[24,76])],center=["55%",'30%'],[list(z) for z in zip(["愛情",[14,86])],"70%"],[list(z) for z in zip(["驚駭",[1,89])],60]
    )
    .set_global_opts(
      title_opts=opt.TitleOpts(title="pie-多餅圖基本示例"),legend_opts=opt.LegendOpts(
        type_="scroll",pos_top="20%",pos_left="80%",orient="vertical"
      )
    )
    .set_series_opts(label_opts=opt.LabelOpts(formatter="{b}:{c}"))
  )
  pie.render()
  os.system("render.html")

直方圖

from pyecharts.charts import Bar
from pyecharts import options as opt
from pyecharts.globals import ThemeType
from pyecharts.faker import Faker as fa
import random


def pye1():
  # 生成隨機資料
  attr = fa.days_attrs
  v1 = [random.randrange(10,150) for _ in range(31)]
  v2 = [random.randrange(10,150) for _ in range(31)]

  # 初始化一個Bar物件,並設定一寫初始化設定
  bar = Bar(init_opts=opt.InitOpts(theme=ThemeType.WHITE))
  # 新增資料
  bar.add_xaxis(attr)
  # is_selected: 開啟圖表時是否預設載入  grap:不同系列的柱間距離,百分比; color:指定柱狀圖Label的顏色
  bar.add_yaxis("test1",v1,gap="0",category_gap="20%",color=fa.rand_color())
  bar.add_yaxis("test2",v2,is_selected=False,gap="0%",color=fa.rand_color())
  # 全域性配置
  # title_opts:圖示標題相關設定
  # toolbox_opts: 工具欄相關設定
  # yaxis_opts/xaxis_opts: 座標軸相關設定
  # axislabel_opts: 座標軸簽字相關設定
  # axisline_opts: 座標軸軸線相關設定
  # datazoom_opts: 座標軸軸線相關設定
  # markpoint_opts: 標記點相關設定
  # markpoint_opts:label_opts=opts.LabelOpts(is_show=False) 標籤值是否疊加
  # markline_opts:標記線相關設定
  bar.set_global_opts(title_opts=opt.TitleOpts(title="主標題",subtitle="副標題"),toolbox_opts=opt.ToolboxOpts(),yaxis_opts=opt.AxisOpts(axislabel_opts=opt.LabelOpts(formatter="{value}/月"),name="這是y軸"),xaxis_opts=opt.AxisOpts(
            axisline_opts=opt.AxisLineOpts(linestyle_opts=opt.LineStyleOpts(color='blue')),name="這是x軸"),datazoom_opts=opt.DataZoomOpts()
            )
  bar.set_series_opts(markpoint_opts=opt.MarkPointOpts(data=[opt.MarkPointItem(type_="max",name="最大值"),opt.MarkPointItem(type_="min",name="最小值"),opt.MarkPointItem(type_="average",name="平均值")]),markline_opts=opt.MarkLineOpts(data=[opt.MarkLineItem(type_="min",opt.MarkLineItem(type_="max",opt.MarkLineItem(type_="average",name="平均值")]))
  # 指定生成html檔案路徑
  bar.render('test.html')
  os.system("test.html")

效果如下

基於Python pyecharts實現多種圖例程式碼解析

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。