Matplotlib 离散图例
在使用数据可视化的过程中,经常会遇到使用heatmaps(热点地图)的情况。这种图形可以通过使用Seaborn来创建,这是一个基于Matplotlib的数据可视化库。在Seaborn heatmap图中,使用legend参数可以生成离散图例,但是对于想要在此基础上更进一步的用户而言,主要问题在于如何一次性定义特定的离散标签。
Matplotlib离散绘图功能为此提供了解决方案。在本文中,我们将学习如何在seaborn heatmap中使用Matplotlib来定义和描绘离散图例。
阅读更多:Matplotlib 教程
制作一个Seaborn Heatmap
我们首先创建一个Seaborn heatmap,以便熟悉这个过程并可以观测到它是如何工作的。在这里,我们使用Seaborn的“flights”数据集,该数据集包含乘机人数的时间序列数据。我们使用这个示例数据集来创建一个显示每个月乘客人数变化情况的heatmap。在这个例子中,我们要显示12个月的乘客人数。
import seaborn as sns
# Load the example flights dataset and convert to long-form
flights_long = sns.load_dataset("flights").pivot("month", "year", "passengers")
# Draw a heatmap with the numeric values in each cell
f, ax = plt.subplots(figsize=(9, 6))
sns.heatmap(flights_long, annot=True, fmt="d", linewidths=.5, ax=ax)
此代码将生成一张出租车流量heatmap,heatmap的x轴标签为年份(1949-1960),y轴标签为月份。每个单元格的数字代表在特定年份的这个月份中,人们的搭乘量。
在Seaborn Heatmap添加自定义离散legend
正如我们在上文中所讲到的,使用Seaborn可以生成离散图例。但是,在使用Matplotlib的离散图例时,则可以添加自定义标签。这个特性允许我们更进一步地控制并更好地表达数据。
在我们的heatmap中,我们想要将每个月的乘客流量分为3个级别:low,medium和high,将每个数字映射到它们所属的级别。
要实现这一点,我们首先定义映射函数,它将把数字映射到标签:
# Define a function to map numbers to labels
def discrete_map(x):
if x < 250:
return 'low'
elif x < 500:
return 'medium'
else:
return 'high'
我们可以使用这个函数来应用标签:
# Apply the function to data
flights_discrete = flights_long.applymap(discrete_map)
现在数据被映射为离散标签,我们需要创建一个自定义legend。为了做到这一点,我们需要定义一个颜色映射(color map),该颜色映射会对每个离散类别赋予唯一的颜色。
我们使用Matplotlib的colors库中的ListedColormap生成自定义颜色映射:
# Define the color map
cmap = colors.ListedColormap(['#d7191c', '#fdae61', '#a6d96a'])
这里,我们选用了一个在色调上并不相似的三色组合,以便使不同标识更易于分辨。
接下来,我们将使用上一步的映射和自定义颜色表来绘制离散图例:
# Draw a heatmap with the numeric values in each cell and a custom legend
f, ax = plt.subplots(figsize=(9, 6))
sns.heatmap(flights_discrete, annot=True, fmt="", linewidths=.5, ax=ax, cmap=cmap)
# Create a color bar legend
color_bar = ax.collections[0].colorbar
color_bar.set_ticks([0.25, 0.75, 1.25])
color_bar.set_ticklabels(['low', 'medium', 'high'])
这段代码将绘制新的heatmap,并使用之前定义的映射函数将每个数字映射到离散标签上。最后,创建一个 color bar legend,以便在图表中添加离散图示。
这里有一个完整的代码:
import matplotlib.pyplot as plt
from matplotlib import colors
import seaborn as sns
# Load the example flights dataset and convert to long-form
flights_long = sns.load_dataset("flights").pivot("month", "year", "passengers")
# Define a function to map numbers to labels
def discrete_map(x):
if x < 250:
return 'low'
elif x < 500:
return 'medium'
else:
return 'high'
# Apply the function to data
flights_discrete = flights_long.applymap(discrete_map)
# Define the color map
cmap = colors.ListedColormap(['#d7191c', '#fdae61', '#a6d96a'])
# Draw a heatmap with the numeric values in each cell and a custom legend
f, ax = plt.subplots(figsize=(9, 6))
sns.heatmap(flights_discrete, annot=True, fmt="", linewidths=.5, ax=ax, cmap=cmap)
# Create a color bar legend
color_bar = ax.collections[0].colorbar
color_bar.set_ticks([0.25, 0.75, 1.25])
color_bar.set_ticklabels(['low', 'medium', 'high'])
这段代码将生成符合要求的heatmap和离散标签。
总结
在本文中,我们学习如何在Seaborn heatmap上使用Matplotlib来生成离散标签。我们首先介绍了如何创建一个heatmap,然后展示了如何将数字数据映射到离散标签,并用自定义颜色显示它们。最后,我们展示了如何将我们的自定义图例应用于 heatmap 中。
在数据可视化中,为了更好地表达和传达数据,仔细控制图像和图例的颜色和标签是至关重要的。本文的技巧可以帮助你更好地实现这一目标,同时保持可读性和易识别性。
极客教程