Python-Altair可视化工具
参考文章:python大佬的可视化工具-Altair_qq_21478261的博客-CSDN博客_altair python官方参考文章:Example Gallery — Altair 4.2.0 documentationOverview | Vega-Litepaaas安装轮子:$ pip install altair vega_datasets官网部分案列:# Altair, datase
·
参考文章:
python大佬的可视化工具-Altair_qq_21478261的博客-CSDN博客_altair python
官方参考文章:
Example Gallery — Altair 4.2.0 documentation
安装轮子:
$ pip install altair vega_datasets
官网部分案列:
# Altair, datasets are most commonly provided as a Dataframe
import pandas as pd
import altair as alt
'''https://altair-viz.github.io/user_guide/encoding.html'''
'''https://vega.github.io/vega-lite/docs/'''
from vega_datasets import data
import numpy as np
pd.set_option('display.max_columns', 10)#显示列数
pd.set_option('display.max_rows', 10)#显示行数
pd.set_option('display.width', 5000)#显示宽度
df = data.seattle_weather()
print(df)
scale = alt.Scale(
domain=['sun', 'fog', 'drizzle', 'rain', 'snow'],
range=['#e7ba52', '#c7c7c7', '#aec7e8', '#1f77b4', '#9467bd'])
brush = alt.selection(type='interval')
points = alt.Chart().mark_point().encode(
alt.X('temp_max:Q', title='Maximum Daily Temperature (C)'),
alt.Y('temp_range:Q', title='Daily Temperature Range (C)'),
color=alt.condition(brush,
'weather:N',
alt.value('lightgray'),
scale=scale),
size=alt.Size('precipitation:Q',
scale=alt.Scale(range=[1, 200]))).transform_calculate(
"temp_range",
"datum.temp_max - datum.temp_min").properties(
width=600, height=400).add_selection(brush)
bars = alt.Chart().mark_bar().encode(
x='count()',
y='weather:N',
color=alt.Color('weather:N', scale=scale),
).transform_calculate(
"temp_range",
"datum.temp_max - datum.temp_min").transform_filter(brush).properties(
width=600)
chart_w=alt.vconcat(points, bars, data=df)
chart_w.save("altair-weather.html")
print("-------------- interactive scatter plot--------------")
cars = data.cars()
print("cars:\n",cars)
'''
cars:
Name Miles_per_Gallon Cylinders Displacement Horsepower Weight_in_lbs Acceleration Year Origin
0 chevrolet chevelle malibu 18.0 8 307.0 130.0 3504 12.0 1970-01-01 USA
1 buick skylark 320 15.0 8 350.0 165.0 3693 11.5 1970-01-01 USA
2 plymouth satellite 18.0 8 318.0 150.0 3436 11.0 1970-01-01 USA
3 amc rebel sst 16.0 8 304.0 150.0 3433 12.0 1970-01-01 USA
4 ford torino 17.0 8 302.0 140.0 3449 10.5 1970-01-01 USA
'''
#The key idea is that you are declaring links between data columns
# and visual encoding channels, such as the x-axis, y-axis, color, etc.
chart_car=alt.Chart(cars).mark_point().encode(
x='Horsepower',#马力
y='Miles_per_Gallon',#油耗
# y='average(Miles_per_Gallon)',#油耗
color='Origin',#来自哪里
).interactive()
chart_car.save("altair-cars_y=Miles_per_Gallon.html")
print("-------------- mark_bar--------------")
data_bar = pd.DataFrame({'a': list('CCCDDDEEE'),
'b': [2, 7, 4, 1, 2, 6, 8, 4, 7],
'b_des': [3, 8, 4, 1, 4, 4, 7, 4, 7]
})
chart_bar_xy=alt.Chart(data_bar).mark_bar().encode(
x='a',
y="average(b)"
)
chart_bar_yx=alt.Chart(data_bar).mark_bar(color='firebrick').encode(
alt.X('b', type='quantitative', aggregate='average',title='avg(b) by category'),
alt.Y('a', type='nominal',title='category')
)
#画目标线
tick = alt.Chart(data_bar).mark_tick(
color='red',
thickness=2,
size=20 * 0.9, # controls width of tick.
).encode(
x='a',
y='average(b_des)'
)
(chart_bar_xy+tick).save("chart_bar_xy_tick.html")
chart_bar_yx.save("chart_bar_yx.html")
source = data.wheat()
#画bar图
chart_bar_wheat=alt.Chart(source).mark_bar().encode(
y="wheat:Q",
x='year:O',
# The highlight will be set on the result of a conditional statement
#根据条件设定bar的颜色值
color=alt.condition(
alt.datum.year < 1610, # If the year is 1810 this test returns True,
alt.value('orange'), # which sets the bar orange.
alt.value('steelblue') # And if it's not true it sets the bar steelblue.
)
)
#加上数量
text = chart_bar_wheat.mark_text(
align='center',
baseline='middle',
dy=-7 # Nudges text to right so it doesn't appear on top of the bar
).encode(
text='wheat:Q'
)
# 增加均值线
rule = alt.Chart(source).mark_rule(color='red').encode(
y='mean(wheat):Q')
#增加一个变量,同Y坐标
line = chart_bar_wheat.mark_line(color='red').encode(
y='wages:Q'
)
#增加平均值的趋势
line2= alt.Chart(source).mark_line(color='red').transform_window(
# The field to average
rolling_mean='mean(wheat)',
# The number of values before and after the current value to include.
frame=[-9, 0]
).encode(
x='year:O',
y='rolling_mean:Q'
)
chart_bar_wheat_text_rule_line=(chart_bar_wheat+text+rule+line+line2).properties(height=900,width=600)
chart_bar_wheat_text_rule_line.save("chart_bar_wheat_text_rule_line.html")
print("-------------- mark_bar:叠加图--------------")
source = data.seattle_weather()
print("source:\n",source[:6])
'''
source:
date precipitation temp_max temp_min wind weather
0 2012-01-01 0.0 12.8 5.0 4.7 drizzle
1 2012-01-02 10.9 10.6 2.8 4.5 rain
2 2012-01-03 0.8 11.7 7.2 2.3 rain
'''
mark_bar_round=alt.Chart(source).mark_bar(
cornerRadiusTopLeft=3,#矩形倒角
cornerRadiusTopRight=3,#矩形倒角
opacity=0.8
).encode(
x='month(date):O',
y='count(weather):Q',#weather的计数,根据color中的属性值计数
color='weather:N'
)
mark_bar_round.save("mark_bar_round.html")
print("-------------- mark_tick:标记图--------------")
chart_tick=alt.Chart(cars).mark_tick().encode(
x='Horsepower:Q',
y='Cylinders:O'#作为标记
)
chart_tick.save("chart_tick.html")
print("-------------- mark_bar:joinaggregate transform--------------")
source = pd.DataFrame({'Activity': ['Sleeping', 'Eating', 'TV', 'Work', 'Exercise'],
'Time': [8, 2, 4, 8, 2]})
chart_bar_joinaggregate=alt.Chart(source).transform_joinaggregate(
TotalTime='sum(Time)',
).transform_calculate(
PercentOfTotal="datum.Time / datum.TotalTime"
).mark_bar().encode(
alt.X('PercentOfTotal:Q', axis=alt.Axis(format='.0%')),
y='Activity:N')
chart_bar_joinaggregate.save("chart_bar_joinaggregate.html")
print("-------------- mark_bar:mark_bar_sum--------------")
source2 = data.barley()
print(source2)
mark_bar_sum1=alt.Chart(source2).mark_bar().encode(
x=alt.X('sum(yield)', stack='normalize'),
y='variety',
color=alt.Color('site')
)
text1 = alt.Chart(source2).mark_text(dx=-15, dy=3, color='white').encode(
x=alt.X('sum(yield):Q', stack='normalize'),
y=alt.Y('variety:N'),
detail='site:N',
text=alt.Text('sum(yield):Q', format='.1f')
)
mark_bar_sum2=alt.Chart(source2).mark_bar().encode(
column='year',
x='sum(yield):Q',
y=alt.Y('site:N', sort='x'),
color='variety',
order=alt.Order(
# Sort the segments of the bars by this field
'variety',
sort='ascending'
)
)
text2 = alt.Chart(source2).mark_text(dx=-15, dy=3, color='white').encode(
column='year',
x=alt.X('sum(yield):Q', stack='zero'),
y=alt.Y('site:N'),
detail='variety:N',
text=alt.Text('sum(yield):Q', format='.1f')
)
mark_bar_sum1_text=mark_bar_sum1+text1
mark_bar_sum2_text=mark_bar_sum2
mark_bar_sum=(mark_bar_sum1_text & mark_bar_sum2_text)
mark_bar_sum.save("mark_bar_sum.html")
print("-------------- mark_line:with Confidence Interval Band--------------")
line = alt.Chart(cars).mark_line().encode(
x='Year',
y='mean(Miles_per_Gallon)'
)
band = alt.Chart(cars).mark_errorband(extent='ci').encode(
x='Year',
y=alt.Y('Miles_per_Gallon', title='Miles/Gallon'),
)
mark_line=band + line
mark_line.save("mark_line.html")
print("-------------- mark_area--------------")
source = data.sp500.url
brush = alt.selection(type='interval', encodings=['x'])
base = alt.Chart(source).mark_area().encode(
x = 'date:T',
y = 'price:Q'
).properties(
width=600,
height=200
)
upper = base.encode(
alt.X('date:T', scale=alt.Scale(domain=brush))
)
lower = base.properties(
height=60
).add_selection(brush)
mark_area_A=upper & lower
mark_area_A.save("mark_area_A.html")
source_iowa_electricity = data.iowa_electricity()
print("data.iowa_electricity():\n",source_iowa_electricity)
mark_area_B=alt.Chart(source_iowa_electricity).mark_area(opacity=0.3).encode(
x="year:T",
y=alt.Y("net_generation:Q", stack=None),
color="source:N"
)
mark_area_B.save("mark_area_B.html")
mark_area_B_row=alt.Chart(source_iowa_electricity).mark_area().encode(
x="year:T",
y="net_generation:Q",
color="source:N",
row="source:N"
).properties(
height=100
)
mark_area_B_row.save("mark_area_B_row.html")
source_iris = data.iris()
print("data.iris():\n",source_iris)
'''
data.iris():
sepalLength sepalWidth petalLength petalWidth species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
...
146 6.3 2.5 5.0 1.9 virginica
147 6.5 3.0 5.2 2.0 virginica
'''
mark_area_C=alt.Chart(source_iris).transform_fold(
['petalWidth',
'petalLength',
'sepalWidth',
'sepalLength'],
as_ = ['Measurement_type', 'value']
).transform_density(
density='value',
bandwidth=0.3,
groupby=['Measurement_type'],
extent= [0, 8],
counts = True,
steps=200
).mark_area().encode(
alt.X('value:Q'),
alt.Y('density:Q', stack='zero'),
alt.Color('Measurement_type:N')
).properties(width=400, height=100)
mark_area_C.save("mark_area_C.html")
source_stocks = data.stocks()
print("data.stocks():\n",source_stocks)
'''
data.stocks():
symbol date price
0 MSFT 2000-01-01 39.81
1 MSFT 2000-02-01 36.35
...
557 AAPL 2010-01-01 192.06
558 AAPL 2010-02-01 204.62
'''
mark_area_D=alt.Chart(source_stocks).transform_filter(
alt.datum.symbol != 'GOOG'
).mark_area().encode(
x='date:T',
y='price:Q',
color='symbol:N',
row=alt.Row('symbol:N', sort=['MSFT', 'AAPL', 'IBM', 'AMZN'])
).properties(height=50, width=400)
mark_area_D.save("mark_area_D.html")
print("-------------- mark_point+mark_tick--------------")
# Configure the options common to all layers
brush = alt.selection(type='interval')
base = alt.Chart(cars).add_selection(brush)
# Configure the points
points = base.mark_point().encode(
x=alt.X('Miles_per_Gallon', title=''),
y=alt.Y('Horsepower', title=''),
color=alt.condition(brush, 'Origin', alt.value('grey'))
)
# Configure the ticks
tick_axis = alt.Axis(labels=False, domain=False, ticks=False)
x_ticks = base.mark_tick().encode(
alt.X('Miles_per_Gallon', axis=tick_axis),
alt.Y('Origin', title='', axis=tick_axis),
color=alt.condition(brush, 'Origin', alt.value('lightgrey'))
)
y_ticks = base.mark_tick().encode(
alt.X('Origin', title='', axis=tick_axis),
alt.Y('Horsepower', axis=tick_axis),
color=alt.condition(brush, 'Origin', alt.value('lightgrey'))
)
# Build the chart
cars_tick=y_ticks | (points & x_ticks)
cars_tick.save("cars_tick.html")
print("-------------- mark_circle--------------")
mark_circle_car=alt.Chart(cars).mark_circle().encode(
alt.X(alt.repeat("column"), type='quantitative'),
alt.Y(alt.repeat("row"), type='quantitative'),
color='Origin:N'
).properties(
width=150,
height=150
).repeat(
row=['Horsepower', 'Acceleration', 'Miles_per_Gallon'],
column=['Miles_per_Gallon', 'Acceleration', 'Horsepower']
).interactive()
mark_circle_car.save("mark_circle_car.html")
np.random.seed(1)
source_loess = pd.DataFrame({
'x': np.arange(100),
'A': np.random.randn(100).cumsum(),
'B': np.random.randn(100).cumsum(),
'C': np.random.randn(100).cumsum(),
})
print("-------------- 趋势线--------------")
base1 = alt.Chart(source_loess).mark_circle(opacity=0.5).transform_fold(
fold=['C'],
as_=['category', 'y'],
).encode(
alt.X('x:Q'),
alt.Y('y:Q'),
alt.Color('category:N')
)
base2 = alt.Chart(source_loess).mark_circle(opacity=0.5).transform_fold(
fold=['A', 'B'],
as_=['category2', 'y2'],
).encode(
alt.X('x:Q'),
alt.Y('y2:Q'),
alt.Color('category2:N')
)
mark_circle_loess=base1 + base2+ base2.transform_loess('x', 'y2', groupby=['category2']).mark_line(size=4)
mark_circle_loess.save("mark_circle_loess.html")
print("-------------- MAPS--------------")
# Data generators for the background
sphere = alt.sphere()
graticule = alt.graticule()
# Source of land data
source = alt.topo_feature(data.world_110m.url, 'countries')
# Layering and configuring the components
maps_world=alt.layer(
alt.Chart(sphere).mark_geoshape(fill='lightblue'),
alt.Chart(graticule).mark_geoshape(stroke='white', strokeWidth=0.5),
alt.Chart(source).mark_geoshape(fill='ForestGreen', stroke='black')
).project(
'naturalEarth1'
).properties(width=600, height=400).configure_view(stroke=None)
maps_world.save("maps_world.html")
更多推荐
已为社区贡献1条内容
所有评论(0)