6步實現(xiàn)python炫酷柱狀圖
↑↑↑關(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>
掃碼回復(fù):2021
獲取最新學(xué)習(xí)資源

▲點擊上方卡片,一起學(xué)Python
學(xué)習(xí)更多: 整理了我開始分享學(xué)習(xí)筆記到現(xiàn)在超過250篇優(yōu)質(zhì)文章,涵蓋數(shù)據(jù)分析、爬蟲、機器學(xué)習(xí)等方面,別再說不知道該從哪開始,實戰(zhàn)哪里找了
“點贊”傳統(tǒng)美德不能丟 
評論
圖片
表情
