A heatmap is used to create a visual depiction of a matrix. It draws a matrix on the graph, with different color hues representing different values. We can utilize the seaborn to generate heatmap plots in the seaborn module.heatmap() function. The default size of the plot may not provide a good picture of the data when depicting a huge matrix.
Annotations are text lines appearing on a heatmap cell to describe what is represented by the cell.
The annotations’ font size is set by default. However, it can be changed using the heatmap() method’s annot kws option. The annot kws option is of the dictionary type and requires a value for the size key. The value assigned to this key determines the size of the annotations. However, various requirements must be met to increase the annotations’ size, such as setting the annot argument of the heatmap() function to True and selecting the needed length for the annot kws option.
This guide will address this issue and teach you how to change the size of seaborn heatmaps. We can utilize functions from the matplotlib-axes library since heatmap() returns a matplotlib-axes object.
The Heatmap Syntax in Seaborn is:
seaborn.heatmap(data, vmin=None, vmax=None, annot=None,annot_kws=True, linewidths=0,cbar=None, cbar_kws=None,square=False, xticklabels='auto', yticklabels='auto', mask=None, ax=None, kwargs)
- Data: Convert a two-dimensional dataset to an ndarray. The columns and rows will be named using the index/column information from a Pandas DataFrame.
- vmin, vmax: Values that will be deducted from the dataset and other term inputs to anchor the colormap.
- annot: Fill each cell with the data value if True. If it’s an object like an array with the same format as data, use it to annotate the heatmap rather than the data. Instead of using an index, DataFrames will be matched based on their location.
- annot_kws: When annot is True, the keyword parameters are provided to matplotlib.axes.Axes.text ().
- linewidths: The distance between the lines used to divide each cell.
- cbar: A bool argument determines whether or not a colorbar is drawn.
- cbar_ax: The axes from which the colorbar will be created; otherwise, the space on the main axes will be taken up.
- square: If True, change the axes property to “equal” so each cell is square.
- fmt: Use this string formatting code for adding annotations.
- kwargs: All other keyword options are passed Matplotlib.axes.Axes.pcolormesh() as kwargs.
- xticklabels, yticklabels: If True, graph the data frame’s column names with xticklabels and yticklabels. The names of the columns are not plotted if this is False. If the alternate labels are xticklabels, make a list of them. If the number is an integer, use the field names but only plot the first in labels. If you’re using “auto,” attempt to plot as many non-overlapping labels as possible.
- mask: If this option is set to True, data will not be displayed in cells when the mask is True.
- Masked cells are ones with blank values.
- ax: The axes on which the plot will be built; otherwise, utilize the currently active axes
How changing the size of the Seaborn Heatmap using the seaborn.set() function
The seaborn plots’ setup and theme are defined using the set() function. The size of the plot can be specified with the rc parameter.
As an example,
import matplotlib.pyplot as plt import seaborn as sns import pandas as pd rs_df = pd.DataFrame({"First Month": [7,1,5,6,3,10,5,8], "Second Month" : [1,2,8,4,3,9,5,2], "Third Month" : [4,6,5,8,6,1,2,3], "Fourth Month" : [5,8,9,5,1,7,8,9]}) sns.set(rc = {'figure.figsize':(15,8)}) sns.heatmap(rs_df.corr())
It’s worth noting that the rc parameter’s value is supplied as a dictionary. The final width and height are provided as a tuple.
To change the size of the Seaborn Heatmap, use the matplotlib.pyplot.figure() function.
In Python, the method figure() is used to start or modify the current figure. This diagram depicts the heatmap. The figsize parameter in the function can be used to change the size. As an example,
import matplotlib.pyplot as plt import pandas as pd import seaborn as sns rs_df = pd.DataFrame({"First Month": [7,1,5,6,3,10,5,8], "Second Month" : [1,2,8,4,3,9,5,2], "Third Month" : [4,6,5,8,6,1,2,3], "Fourth Month" : [5,8,9,5,1,7,8,9]}) plt.figure(figsize = (15,8)) sns.heatmap(rs_df.corr())
It is worth noting that the function is called before the heatmap() method. To change the size of a Seaborn plot, use the matplotlib.pyplot.gcf() function.
The gcf() function returns the figure’s view instance object. The set_size_inches() method can be used to change the size of this object. We can now change the size of the heatmap visualization on this object. As an example,
import matplotlib.pyplot as plt import pandas as pd import seaborn as sns rs_df = pd.DataFrame({"First Month": [7,1,5,6,3,10,5,8], "Second Month" : [1,2,8,4,3,9,5,2], "Third Month" : [4,6,5,8,6,1,2,3], "Fourth Month" : [5,8,9,5,1,7,8,9]}) sns.heatmap(rs_df.corr()) plt.gcf().set_size_inches(15, 8)
This method is called after the heatmap() function. It should also be noticed that the amount of annotations in the heatmap is not significantly influenced by any of the ways mentioned above.
To increase the number of annotations, set the annot parameter in the heatmap() function to True. The font size can then be specified as a key-value pair in the annot kws parameter, such as annot kws =’size’:15.
Example 1: Exploring the figure() function
The figure() function in Python starts or alters the current figure. The heatmap is depicted in this diagram. The figsize parameter of the function can be used to adjust the size. We must first generate data to generate the plot with the chosen figure size. We created a data frame with four columns, List1, List2, List3, and List4, and filled them with random values. Then we have a figure() method in which we have defined the size of the figure. Finally, the heatmap function is used to apply the correct technique to the data frame.
import pandas as pd import matplotlib.pyplot as plt import seaborn as sns lst_dt = pd.DataFrame({"First List": [5,8,9,5,1,7,8,9], "Second List" : [4,6,5,8,6,1,2,3], "Third List" : [1,2,8,4,3,9,5,2], "Fourth List" : [7,1,5,6,3,10,5,8]}) plt.figure(figsize = (15,7)) sns.heatmap(lst_dt.corr()) plt.show()
Example 2: What to consider before determining the size
Consideration must be applied when determining the size. When you enter a large number, the annotations will be significantly magnified, making them impossible to read and decipher. They might even collapse on top of each other. As a result, the heatmap is no longer functional.
We chose the iris data frame and loaded it into the load_dataset method. Call the heatmap function with the annot argument set to true and annot_kws set to 20.
import matplotlib.pyplot as plt Import seaborn as sns var_data = sns.load_dataset("iris") sns-heatmap(var_data.corr(), annot=Truc, annot_kws={'size': 20}) plt.show()
Example: Using set() to establish the layout and theme
The set() function determines the layout and theme of the Seaborn plots. The RC option can be used to determine the size of the plot. In the following example, we’ve defined the modules used in the Python script. Following that, we created data in the variable kpis and ran the data frame function. We recorded the employees’ kpis in four student columns in the data frame function. We had already entered the data for the plot.
The set function is now defined, and the size of the plot is specified in the figsize. The Seaborn heatmap function is then called, and the corr function is applied to the kpis. The function corr() returns all of the data frame’s columns, showing a pairwise correlation.
import pandas as pd import matplotlib.pyplot as plt import seaborn as sns kpis = pd.DataFrame({"Employee One": [6,3,1,7,3,10,5,4], "Employee Two" : [3,7,2,1,8,2,4,2], "Employee Three" : [1,6,9,8,6,4,9,3], "Employee Four" : [5,5,1,9,4,7,8,3]}) sns.set(rc = {'figure.figsize':(10,5)}) sns .heatmap(kpis.corr()) plt.show()
Example 4: In Seaborn, how can you change the size of the heatmaps?
We’ll utilize the seaborn dataset flights for this example, which contains the number of airline passengers who traveled each month from 1949 to 1960:
import seaborn as sns import matplotlib.pyplot as plt #loading the dataset "flights." var_data = sns.load_dataset("flights") var_data = var_data.pivot("month", "year", "passengers") #view first five rows of dataset print(var_data.head())
Example 5: Using the annot_kws and annot for heatmap size
For the heatmap size, we use the annot and annot_kws options. We used the Seaborn load_dataset option to load the sample dataset “tips,” which is saved in the variable data. The heatmap function was then called, and the corr function was provided for the dataset. Then we added the annot option and set it to true. The size of the annot kws parameter is set to 12.
import numpy as np import seaborn as sns import matplotlib.pyplot as plt var_data = sns.load_dataset("tips") sns-heatmap(var_data .corr(), annot=True, annot_kws={'size': 12}) plt.show()
Conclusion
Seaborn is a Python data visualization package based on the matplotlib library. The latter represents the data in a statistically relevant and appealing graphical format. A heatmap uses a color palette to illustrate the variation in linked data, one of Seaborn’s capabilities. To create heatmap charts in the Seaborn module, use the seaborn.heatmap() method.
Annotations are pieces of text that appear on heatmap cells to represent what the cell represents. In addition, the font size of the annotations is set by default, but it is alterable, and this is done by the annot_kws parameter of the heatmap() function. annot_kws is a dictionary-type parameter that allows the entry named size values. The value assigned to this key determines the size of the annotations. However, certain conditions must be followed to raise the size of the annotations.