📜  如何在Python中使用 Altair 制作简单的直方图?

📅  最后修改于: 2022-05-13 01:54:41.958000             🧑  作者: Mango

如何在Python中使用 Altair 制作简单的直方图?

先决条件:牛郎星

简单直方图是通过矩形来表示频率分布,矩形的宽度代表类间隔。直方图是将分组数据点组织到指定范围内的图形表示。通过使用直方图,我们可以将大量数据及其频率可视化为一个连续的图。

安装:

为了安装 altair 库和 vega_datasets,我们在命令提示符中运行以下命令。

pip install altair
pip install vega-datasets

在本文中,我们将在以下各种示例中使用汽车和虹膜数据集,借助 altair 库绘制简单直方图。

循序渐进的方法:

  • 导入库。
  • 创建或加载数据集。
  • 从数据集中选择要制作直方图的列。
  • 为了制作直方图,在 altair 库中,我们必须给出三个重要元素(altair.Chart()、mark_bar()、encode())。
  • 将其放入名为“hist”的变量中。
  • 然后要查看情节,我们必须编写一行代码 hist.show() 就完成了。

示例 1:打印 cars() 的数据集

Python
# importing libraries
import altair as alt
from vega_datasets import data
import vega_datasets
  
# importing cars dataset form
# vega_datasets provided by altair
car_data  = data.cars()
  
# printing the dataset
display(car_data)


Python
# importing libraries
import altair as alt
from vega_datasets import data
  
# importing cars dataset
# form vega_datasets provided by altair
car_data  = data.cars()
  
# making the simple histogram on Acceleration
hist = alt.Chart(car_data).mark_bar().encode(x = 'Acceleration',
                                             y = 'count()')
# showing the histogram
hist.show()


Python
# importing libraries
import altair as alt
from vega_datasets import data
  
# importing cars dataset
# form vega_datasets provided by altair
car_data  = data.cars()
  
# making the simple histogram
# on Acceleration by setting the bin
hist = alt.Chart(car_data).mark_bar().encode(x = alt.X('Acceleration',
                                                       bin = alt.BinParams(maxbins = 30)),
                                             y = 'count()')
# showing the histogram
hist.show()


Python
# importing libraries
import altair as alt
from vega_datasets import data
  
# importing cars dataset
# form vega_datasets provided by altair
car_data  = data.cars()
  
# making the simple histogram
# on Horsepower by setting the bin
hist = alt.Chart(car_data).mark_bar().encode(x = alt.X('Horsepower',
                                                       bin = alt.BinParams(maxbins = 20)),
                                             y = 'count()')
# showing the histogram
hist.show()


Python
# importing libraries
import altair as alt
from vega_datasets import data
  
# importing cars dataset
# form vega_datasets provided by altair
iris_data  = data.iris()
  
# making the simple histogram on sepal length
hist = alt.Chart(iris_data).mark_bar().encode(x = 'sepalLength',
                                              y = 'count()')
  
# showing the histogram
hist.show()


Python
# importing libraries
import altair as alt
from vega_datasets import data
  
# importing cars dataset
# form vega_datasets provided by altair
iris_data  = data.iris()
  
# making the simple histogram
# on sepal length by setting bin
# and color on the basis of species
hist = alt.Chart(iris_data).mark_bar().encode(x = alt.X('sepalLength',
                                                        bin = alt.BinParams(maxbins = 20)),
                                              y = 'count()',color = 'species')
# showing the histogram
hist.show()


Python
# importing libraries
import altair as alt
from vega_datasets import data
  
# importing cars dataset
# form vega_datasets provided by altair
iris_data  = data.iris()
  
# making the simple histogram
# on petal width by setting bin
# and color on the basis of species
hist = alt.Chart(iris_data).mark_bar().encode(x = alt.X('petalWidth',
                                                        bin = alt.BinParams(maxbins = 10)),
                                              y = 'count()', color = 'species')
# showing the histogram
hist.show()


输出:

示例 2:使用汽车数据集制作默认的简单直方图。

Python

# importing libraries
import altair as alt
from vega_datasets import data
  
# importing cars dataset
# form vega_datasets provided by altair
car_data  = data.cars()
  
# making the simple histogram on Acceleration
hist = alt.Chart(car_data).mark_bar().encode(x = 'Acceleration',
                                             y = 'count()')
# showing the histogram
hist.show()

输出:

示例 3:通过将 bin 设置为 Acceleration,使用汽车数据集制作简单直方图。

Python

# importing libraries
import altair as alt
from vega_datasets import data
  
# importing cars dataset
# form vega_datasets provided by altair
car_data  = data.cars()
  
# making the simple histogram
# on Acceleration by setting the bin
hist = alt.Chart(car_data).mark_bar().encode(x = alt.X('Acceleration',
                                                       bin = alt.BinParams(maxbins = 30)),
                                             y = 'count()')
# showing the histogram
hist.show()

输出:

示例 4:通过将 bin 设置为“马力”,使用汽车数据集制作简单直方图。

Python

# importing libraries
import altair as alt
from vega_datasets import data
  
# importing cars dataset
# form vega_datasets provided by altair
car_data  = data.cars()
  
# making the simple histogram
# on Horsepower by setting the bin
hist = alt.Chart(car_data).mark_bar().encode(x = alt.X('Horsepower',
                                                       bin = alt.BinParams(maxbins = 20)),
                                             y = 'count()')
# showing the histogram
hist.show()

输出:

示例 5:使用 sepalLength 上的 iris 数据集制作默认的简单直方图。

Python

# importing libraries
import altair as alt
from vega_datasets import data
  
# importing cars dataset
# form vega_datasets provided by altair
iris_data  = data.iris()
  
# making the simple histogram on sepal length
hist = alt.Chart(iris_data).mark_bar().encode(x = 'sepalLength',
                                              y = 'count()')
  
# showing the histogram
hist.show()

输出:

我们也可以在加载虹膜数据集后,在程序中使用print(iris_data)来打印虹膜数据集。打印后的虹膜数据集如下所示。

示例 6:通过设置 bin 和颜色制作简单直方图。

Python

# importing libraries
import altair as alt
from vega_datasets import data
  
# importing cars dataset
# form vega_datasets provided by altair
iris_data  = data.iris()
  
# making the simple histogram
# on sepal length by setting bin
# and color on the basis of species
hist = alt.Chart(iris_data).mark_bar().encode(x = alt.X('sepalLength',
                                                        bin = alt.BinParams(maxbins = 20)),
                                              y = 'count()',color = 'species')
# showing the histogram
hist.show()

输出:

以同样的方式,我们可以在数据集的任何值上制作简单的直方图,并可以相应地设置颜色。

示例 7:通过设置 bin 和颜色,在花瓣宽度上使用 iris 数据集制作简单直方图。

Python

# importing libraries
import altair as alt
from vega_datasets import data
  
# importing cars dataset
# form vega_datasets provided by altair
iris_data  = data.iris()
  
# making the simple histogram
# on petal width by setting bin
# and color on the basis of species
hist = alt.Chart(iris_data).mark_bar().encode(x = alt.X('petalWidth',
                                                        bin = alt.BinParams(maxbins = 10)),
                                              y = 'count()', color = 'species')
# showing the histogram
hist.show()

输出: