<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          6步實現(xiàn)python炫酷柱狀圖

          共 8598字,需瀏覽 18分鐘

           ·

          2021-03-04 09:06

          ↑↑↑關(guān)注后"星標"簡說Python
          人人都可以簡單入門Python、爬蟲、數(shù)據(jù)分析
           簡說Python推薦 
          來源/python數(shù)據(jù)分析之禪
          作者/小dull鳥

          前一段時間給大家介紹了pygal可視化庫,很受讀者喜歡,今天我想深挖一下pygal的柱狀圖用法,一起感受pygal的魅力

          文末可獲取本文所有相關(guān)數(shù)據(jù)和代碼。

          以北京二手房數(shù)據(jù)為例,計算各區(qū)平均房價:

          import pandas as pd
          data=pd.read_excel('各區(qū)平均房價.xlsx')
          data['各區(qū)平均房價(萬元)']=[int(i) for i in data['各區(qū)平均房價(萬元)'].values.tolist()]
          data

          畫出各區(qū)平均房價基本柱狀圖

          import pygal
          #設(shè)置pygal與jupyter notebook交互
          from IPython.display import display, HTML
          base_html = """
          <!DOCTYPE html>
          <html>
            <head>
            <script type="text/javascript" src="http://kozea.github.com/pygal.js/javascripts/svg.jquery.js"></script>
            <script type="text/javascript" src="https://kozea.github.io/pygal.js/2.0.x/pygal-tooltips.min.js""></script>
            </head>
            <body>
              <figure>
                {rendered_chart}
              </figure>
            </body>
          </html>
          """

          import pygal
          from pygal.style import *
          bar_chart = pygal.Bar(show_legend=False)  #show_legend=False不顯示圖例
          bar_chart.add('',data['各區(qū)平均房價(萬元)'].values.tolist())
          HTML(base_html.format(rendered_chart=bar_chart.render(is_unicode=True)))#圖片渲染


          添加標題、橫坐標及X、Y軸坐標名稱

          import pygal
          from pygal.style import *
          bar_chart = pygal.Bar(
              show_legend=False,     #show_legend=False不顯示圖例
              x_title='北京各城區(qū)',
              y_title='房價(元)',
          )  
          bar_chart.title='北京各城區(qū)二手房單價'
          bar_chart.x_labels=data['行政區(qū)'].values.tolist()  #以列表的形式添加
          bar_chart.add('',data['各區(qū)平均房價(萬元)'].values.tolist())
          HTML(base_html.format(rendered_chart=bar_chart.render(is_unicode=True)))#圖片渲染

          設(shè)置橫坐標樣式(旋轉(zhuǎn)角度)

          import pygal
          from pygal.style import *
          bar_chart = pygal.Bar(
              show_legend=False,        #show_legend=False不顯示圖例
              x_label_rotation=20,        #旋轉(zhuǎn)橫坐標角度
              x_title='北京各城區(qū)',
              y_title='房價(元)',
          )  
          bar_chart.title='北京各城區(qū)二手房單價'
          bar_chart.x_labels=data['行政區(qū)'].values.tolist()
          bar_chart.add('',data['各區(qū)平均房價(萬元)'].values.tolist())
          HTML(base_html.format(rendered_chart=bar_chart.render(is_unicode=True)))#圖片渲染

          給條形圖增加一個效果

          import pygal
          from pygal.style import *
          bar_chart = pygal.Bar(
              show_legend=False,        #show_legend=False不顯示圖例
              x_label_rotation=20,        #旋轉(zhuǎn)橫坐標角度
              rounded_bars=20,
              x_title='北京各城區(qū)',
              y_title='房價(元)',
              
          )  
          bar_chart.title='北京各城區(qū)二手房單價'
          bar_chart.x_labels=data['行政區(qū)'].values.tolist()
          bar_chart.add('',data['各區(qū)平均房價(萬元)'].values.tolist())
          HTML(base_html.format(rendered_chart=bar_chart.render(is_unicode=True)))#圖片渲染

          添加數(shù)值并設(shè)置樣式

          import pygal
          from pygal.style import *
          bar_chart = pygal.Bar(
              show_legend=False,        #show_legend=False不顯示圖例
              x_label_rotation=20,        #旋轉(zhuǎn)橫坐標角度
              rounded_bars=20,
              x_title='北京各城區(qū)',
              y_title='房價(元)',
              print_values=True,         #是否添加數(shù)值
              print_values_position='top'#數(shù)值位置
              style=DefaultStyle(
                    value_font_family='googlefont:Raleway'#設(shè)置字體
                    value_font_size=10,                     #設(shè)置大小
                    value_colors=('red',))                  #設(shè)置顏色
          )  
          bar_chart.title='北京各城區(qū)二手房單價'
          bar_chart.x_labels=data['行政區(qū)'].values.tolist()
          bar_chart.add('',data['各區(qū)平均房價(萬元)'].values.tolist())
          HTML(base_html.format(rendered_chart=bar_chart.render(is_unicode=True)))#圖片渲染

          設(shè)置柱形圖顏色

          import pygal
          from pygal.style import *
          import random
          colors = ['red','yellow','green','blue','gray','purple','orange','plum','Indigo','SlateBlue','Navy']
          bar_chart = pygal.Bar(
              show_legend=False,        #show_legend=False不顯示圖例
              x_label_rotation=20,        #旋轉(zhuǎn)橫坐標角度
              x_title='北京各城區(qū)',
              y_title='房價(元)',
              rounded_bars=20,
              print_values=True,         #是否添加數(shù)值
              print_values_position='top'#數(shù)值位置
              style=DefaultStyle(
                    value_font_family='googlefont:Raleway'#設(shè)置字體
                    value_font_size=10,                     #設(shè)置大小
                    value_colors=('red',))                  #設(shè)置顏色
          )  
          bar_chart.title='北京各城區(qū)二手房單價'
          bar_chart.x_labels=data['行政區(qū)'].values.tolist()
          list_values=[]
          for i in data['各區(qū)平均房價(萬元)'].values.tolist():
              list_values.append({'value':i,'color':random.choice(colors)})
          bar_chart.add('',list_values)
          HTML(base_html.format(rendered_chart=bar_chart.render(is_unicode=True)))#圖片渲染

          除了上面這種,pygal還有一種更加簡單漂亮的畫法,代碼如下:

          from pygal.style import *
          bar_chart = pygal.Bar(
              width=1000,   #寬度
              height=800,   #高度
              print_values=True,  #是否顯示數(shù)值
              print_labels_position='bottom',
              x_title='北京各城區(qū)',
              y_title='房價(元)',
              print_values_position='top',  #數(shù)值位置
              legend_at_bottom=True,       #是否顯示圖例
              style=DefaultStyle)
          bar_chart.title = '北京各城區(qū)二手房單價柱狀圖'
          for i,j in zip(data['行政區(qū)'].values.tolist(),data['各區(qū)平均房價(萬元)'].values.tolist()):
              bar_chart.add(
                  i,j,rounded_bars=10)
          HTML(base_html.format(rendered_chart=bar_chart.render(is_unicode=True)))#圖片渲染

          這種畫法相當(dāng)于將所有數(shù)值畫到同一個橫坐標中,所以不能添加橫坐標,只能靠圖例辨別,不過可以在圖形上添加圖例文本來彌補:

          from pygal.style import *
          bar_chart = pygal.Bar(
              width=1300,   #寬度
              height=800,   #高度
              print_values=True,
              print_labels=True,
              print_values_position='top',
              print_labels_position='bottom',
              x_title='北京各城區(qū)',
              y_title='房價(元)',
              legend_at_bottom=True,       #是否顯示圖例
              style=DefaultStyle)
          bar_chart.title = '北京各城區(qū)二手房單價柱狀圖'
          for i,j in zip(data['行政區(qū)'].values.tolist(),data['各區(qū)平均房價(萬元)'].values.tolist()):
              bar_chart.add(
                  i,[{'value': int(j), 'label': i}],rounded_bars=10)
          HTML(base_html.format(rendered_chart=bar_chart.render(is_unicode=True)))#圖片渲染

          你更喜歡哪種呢?

          本文數(shù)據(jù)集和代碼獲?。?/span>

          掃下方二維碼,添加我好友后,觀看朋友圈最新動態(tài)即可獲取。

          掃碼回復(fù):2021

          獲取最新學(xué)習(xí)資源

          【圖書推薦】
          《Python數(shù)據(jù)分析全流程指南》主要圍繞整個數(shù)據(jù)分析方法論的常規(guī)流程,介紹了Python常用的工具包,包括科學(xué)計算庫Numpy、數(shù)據(jù)分析庫Pandas、數(shù)據(jù)挖掘庫Scikit-Learn,以及數(shù)據(jù)可視化庫Matplotlib和Seaborn的基本知識,并從數(shù)據(jù)分析挖掘的實際業(yè)務(wù)應(yīng)用出發(fā),講解了互聯(lián)網(wǎng)、金融及零售等行業(yè)的真實案例,比如客戶分群、產(chǎn)品精準營銷、房價預(yù)測、特征降維等,深入淺出、循序漸進地介紹了Python數(shù)據(jù)分析的全過程。

          點擊上方卡片,一起學(xué)Python


          學(xué)習(xí)更多:
          整理了我開始分享學(xué)習(xí)筆記到現(xiàn)在超過250篇優(yōu)質(zhì)文章,涵蓋數(shù)據(jù)分析、爬蟲、機器學(xué)習(xí)等方面,別再說不知道該從哪開始,實戰(zhàn)哪里找了

          點贊”傳統(tǒng)美德不能丟 

          瀏覽 113
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  三级经典在线视频 | 潮喷网站 | 福利十八禁免费网站 | 韩国大鸡巴| 先锋久久资源 |