📌  相关文章
📜  如何在Python中为 seaborn 热图添加框架?

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

如何在Python中为 seaborn 热图添加框架?

热图是数据的图形表示,其中值用颜色表示。它们可以让您一目了然地轻松理解复杂的数据。在Python中使用 seaborn 可以轻松绘制热图。在本文中,我们将在Python中为 seaborn 热图图添加一个框架。

创建热图

为了绘制热图,我们将使用 seaborn 的内置数据集。 Seaborn 有许多内置的数据集,如titanic.csv、penguins.csv、flights.csv、exction.csv。我们也可以让我们的数据集应该只是一个矩形的 ndarray。

Python3
# Import libraries
import seaborn as sns
import matplotlib.pyplot as plt
  
# Preparing dataset
example = sns.load_dataset("flights")
example = example.pivot("month", "year",
                        "passengers")
  
# Creating plot
res = sns.heatmap(example)
  
# show plot
plt.show()


Python3
# Import libraries
import seaborn as sns
import matplotlib.pyplot as plt
  
# Preparing dataset
example = sns.load_dataset("flights")
example = example.pivot("month", "year",
                        "passengers")
  
# Creating plot
res = sns.heatmap(example, cmap = "BuPu")
  
# Drawing the frame
res.axhline(y = 0, color='k',linewidth = 10)
res.axhline(y = example.shape[1], color = 'k',
            linewidth = 10)
  
res.axvline(x = 0, color = 'k',
            linewidth = 10)
  
res.axvline(x = example.shape[0], 
            color = 'k', linewidth = 10)
  
# show plot
plt.show()


Python3
# Import libraries
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
  
# Preparing dataset
example = np.random.rand(10, 12)
  
# Creating plot
res = sns.heatmap(example, cmap = "magma", 
                  linewidths = 0.5)
  
# Drawing the frame
res.axhline(y = 0, color = 'k', 
            linewidth = 15)
  
res.axhline(y = 10, color = 'k',
            linewidth = 15)
  
res.axvline(x = 0, color = 'k',
            linewidth = 15)
  
res.axvline(x = 12, color = 'k',
            linewidth = 15)
# show plot
plt.show()


Python3
# Import libraries
import seaborn as sns
import matplotlib.pyplot as plt
  
# Preparing dataset
example = sns.load_dataset("flights")
example = example.pivot("month", "year", 
                        "passengers")
  
# Creating plot
res = sns.heatmap(example, cmap = "Purples")
  
# Drawing the frame
for _, spine in res.spines.items():
    spine.set_visible(True)
    spine.set_linewidth(5)
  
# show plot
plt.show()


Python3
# Import libraries
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
  
# Preparing dataset
example = np.random.rand(10, 12)
  
# Creating plot
res = sns.heatmap(example, cmap = "Greens",
                  linewidths = 2,
                  linecolor = "white")
  
# Drawing the frame
for _, spine in res.spines.items():
    spine.set_visible(True)
    spine.set_linewidth(3)
    spine.set_linestyle("dashdot")
  
# show plot
plt.show()


输出:

基本热图

有两种方法可以围绕热图绘制框架:

  1. 使用 axhline 和 axvline。
  2. 使用刺(更佳)

方法一:使用axhline和axvline

matplotlib 库的axes 模块中的Axes.axhline() 和Axes.axvline()函数用于分别添加横贯轴的水平线和垂直线。

我们可以从 y=0 和 y= 数据集中的行数绘制两条水平线,它将绘制一个覆盖热图两侧的框架。然后我们可以从数据集中的 x=0 和 x=number of columns 绘制两条垂直线,它将绘制一个覆盖其余两侧的框架,因此我们的热图将有一个完整的框架。

注意:这不是绘制框架的最佳方式,因为当我们增加线宽时不会考虑它何时与热图重叠。

示例 1。

蟒蛇3

# Import libraries
import seaborn as sns
import matplotlib.pyplot as plt
  
# Preparing dataset
example = sns.load_dataset("flights")
example = example.pivot("month", "year",
                        "passengers")
  
# Creating plot
res = sns.heatmap(example, cmap = "BuPu")
  
# Drawing the frame
res.axhline(y = 0, color='k',linewidth = 10)
res.axhline(y = example.shape[1], color = 'k',
            linewidth = 10)
  
res.axvline(x = 0, color = 'k',
            linewidth = 10)
  
res.axvline(x = example.shape[0], 
            color = 'k', linewidth = 10)
  
# show plot
plt.show()

输出:

示例 2:

蟒蛇3

# Import libraries
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
  
# Preparing dataset
example = np.random.rand(10, 12)
  
# Creating plot
res = sns.heatmap(example, cmap = "magma", 
                  linewidths = 0.5)
  
# Drawing the frame
res.axhline(y = 0, color = 'k', 
            linewidth = 15)
  
res.axhline(y = 10, color = 'k',
            linewidth = 15)
  
res.axvline(x = 0, color = 'k',
            linewidth = 15)
  
res.axvline(x = 12, color = 'k',
            linewidth = 15)
# show plot
plt.show()

输出:

方法 2:使用刺

脊线是连接轴刻度线并注明数据区域边界的线。它们可以放置在任意位置。

示例 1:

可以使用 set_linewidth 参数更改线条的宽度,该参数接受一个浮点值作为参数。

蟒蛇3

# Import libraries
import seaborn as sns
import matplotlib.pyplot as plt
  
# Preparing dataset
example = sns.load_dataset("flights")
example = example.pivot("month", "year", 
                        "passengers")
  
# Creating plot
res = sns.heatmap(example, cmap = "Purples")
  
# Drawing the frame
for _, spine in res.spines.items():
    spine.set_visible(True)
    spine.set_linewidth(5)
  
# show plot
plt.show()

输出:

示例 2:

我们可以使用脊椎的 set_linestyle 参数(实线、虚线、虚线、虚线)指定框架的样式。

蟒蛇3

# Import libraries
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
  
# Preparing dataset
example = np.random.rand(10, 12)
  
# Creating plot
res = sns.heatmap(example, cmap = "Greens",
                  linewidths = 2,
                  linecolor = "white")
  
# Drawing the frame
for _, spine in res.spines.items():
    spine.set_visible(True)
    spine.set_linewidth(3)
    spine.set_linestyle("dashdot")
  
# show plot
plt.show()

输出: