1566204694

# Seaborn Heatmap Tutorial | Python Data Visualization

You will learn what is a heatmap, how to create it, how to change its colors, adjust its font size, and much more, so let’s get started.

## What is a heatmap?

The heatmap is a way of representing the data in a 2-dimensional form. The data values are represented as colors in the graph. The goal of the heatmap is to provide a colored visual summary of information.

## Create a heatmap

To create a heatmap in Python, we can use the seaborn library. The seaborn library is built on top of Matplotlib. Seaborn library provides a high-level data visualization interface where we can draw our matrix.

For this tutorial, we will use the following Python components:

• Python 3 (I’ll use Python 3.7)
• Pandas
• Matplotlib
• Numpy
• Seaborn

To install seaborn, run the pip command as follows:

``````pip install seaborn

``````

Seaborn supports the following plots:

• Python 3 (I’ll use Python 3.7)
• Pandas
• Matplotlib
• Numpy
• Seaborn

Okay, let’s create a heatmap now:

Import the following required modules:

``````import numpy as np
&nbsp;
import seaborn as sb
&nbsp;
import matplotlib.pyplot as plt

``````

We imported the numpy module to generate an array of random numbers between a given range which will be plotted as a heatmap.

``````data = np.random.rand(4, 6)

``````

A 2-dimensional array is created with 4 rows and 6 columns. Now let’s store these array values in the heatmap. We can create a heatmap by using the heatmap function of the seaborn module. Then we will pass the data as follows:

``````heat_map = sb.heatmap(data)

``````

Using matplotlib, we will display the heatmap in the output:

``````plt.show()

``````

Congratulations! Your first heatmap is just created!

## Remove heatmap x tick labels

The values in the x axis and y axis for each block in the heatmap are called tick labels. The tick labels are added by default. If we want to remove the tick labels, we can set the xticklabel or ytickelabel attribute of seaborn heatmap to False as below:

``````heat_map = sb.heatmap(data, xticklabels=False, yticklabels=False)

``````

## Set heatmap x axis label

We can add a label in x axis by using the xlabel attribute of Matplotlib as shown in the following code:

``````>>> plt.xlabel("Values on X axis")

``````

The result will be as follows:

## Remove heatmap y tick labels

The labels for y axis are added by default. To remove them, we can set the yticklabels to false.

``````heat_map = sb.heatmap(data, yticklabels=False)

``````

## Set heatmap y axis label

You can add the label in y axis by using the ylabel attribute of Matplotlib as shown:

``````>>> data = np.random.rand(4, 6)
&nbsp;
>>> heat_map = sb.heatmap(data)
&nbsp;
>>> plt.ylabel('Values on Y axis')

``````

## Changing heatmap color

You can change the color of seaborn heatmap by using the color map using the cmap attribute of the heatmap.

Consider the code below:

``````>>> heat_map = sb.heatmap(data, cmap="YlGnBu")
&nbsp;
>>> plt.show()

``````

Here cmap equals YlGnBu which represents the following color:

In Seaborn heatmap, we have three different types of colormaps.

1. Sequential colormaps
2. Diverging color palette
3. Discrete Data

### Sequential colormap

The sequential color map is used when the data range from a low value to a high value. The sequential colormap color codes can be used with the heatmap() function or the kdeplot() function.

The sequential color map contains the following colors:

This image is taken from Matplotlib.org.

#### Sequential cubehelix palette

The cubehelix is a form of the sequential color map. The cubehelix is used when there the brightness is increased linearly and when there is a slight difference in hue.

The cubehelix palette looks like the following:

You can implement this palette in the code using the cmap attribute:

``````>>> heat_map = sb.heatmap(data, cmap="cubehelix")

``````

The result will be:

### Diverging color palette

You can use the diverging color palette when the high and low values are important in the heatmap.

The divergent palette creates a palette between two HUSL colors. It means that the divergent palette contains two different shades in a graph.

You can create the divergent palette in seaborn as follows:

``````import seaborn as sb
&nbsp;
import matplotlib.pyplot as plt
&nbsp;
>>> sb.palplot(sb.diverging_palette(200, 100, n=11))
&nbsp;
>>> plt.show()

``````

Here 200 is the value for palette on the left side and 100 is the code for palette on the right side. The variable n defines the number of blocks. In our case, it is 11. The palette will be as follows:

### Discrete Data

In Seaborn, there is a built-in function called mpl_palette which returns discrete color patterns. The mpl_palette method will plot values in a color palette. This palette is a horizontal array.

The diverging palette looks like the following:

This output is achieved using the following line of code:

``````>>> sb.palplot(sb.mpl_palette("Set3", 11))
&nbsp;
>>> plt.show()

``````

The argument Set3 is the name of the palette and 11 is the number of discrete colors in the palette. The palplot method of seaborn plots the values in a horizontal array of the given color palette.

To add text over the heatmap, we can use the annot attribute. If annot is set to True, the text will be written on each cell. If the labels for each cell is defined, you can assign the labels to the annot attribute.

Consider the following code:

``````>>> data = np.random.rand(4, 6)
&nbsp;
>>> heat_map = sb.heatmap(data, annot=True)
&nbsp;
>>> plt.show()

``````

The result will be as follows:

We can customize the annot value as we will see later.

We can adjust the font size of the heatmap text by using the font_scale attribute of the seaborn like this:

``````>>> sb.set(font_scale=2)

``````

Now define and show the heatmap:

``````>>> heat_map = sb.heatmap(data, annot=True)
&nbsp;
>>> plt.show()

``````

The heatmap will look like the following after increasing the size:

## Seaborn heatmap colorbar

The colorbar in heatmap looks like the one as below:

The attribute cbar of heatmap is a Boolean attribute which if set to true tells if it should appear in the plot or not. If the cbar attribute is not defined, the color bar will be displayed in the plot by default. To remove the color bar, set cbar to False:

``````>>> heat_map = sb.heatmap(data, annot=True, cbar=False)
&nbsp;
>>> plt.show()

``````

To add a color bar title, we can use the cbar_kws attribute.

The code will look like the following:

``````>>> heat_map = sb.heatmap(data, annot=True, cbar_kws={'label': 'My Colorbar'})
&nbsp;
>>> plt.show()

``````

In the cbar_kws, we have to specify what attribute of the color bar we are referring to. In our example, we are referring to the label (title) of the color bar.

Similarly, we can change the orientation of the color. The default orientation is vertical as in the above example.

To create a horizontal color bar define the orientation attribute of the cbar_kws as follows:

``````>>> heat_map = sb.heatmap(data, annot=True, cbar_kws={'label': 'My Colorbar', 'orientation': 'horizontal'})
&nbsp;
>>> plt.show()

``````

The resultant color bar will be like the following:

## Change heatmap colorbar font size

If we need to change the font size of all the components of seaborn, you can use the font_scale attribute of Seaborn.

Let’s set the scale to 1.8 and compare a scale 1 with 1.8:

``````>>> sb.set(font_scale=1.8)
&nbsp;
>>> heat_map = sb.heatmap(data, annot=True, cbar_kws={'label': 'My Colorbar', 'orientation': 'horizontal'})
&nbsp;
>>> plt.show()

``````

This result for scale 1:

And the scale of 1.8 will look like this:

## Change the rotation of tick axis

We can change the tick labels rotation by using the rotation attribute of the required ytick or xtick labels.

First, we define the heatmap like this:

``````>>> heat_map = sb.heatmap(data)
&nbsp;
>>> plt.show()

``````

This is a regular plot with random data as defined in the earlier section.

Notice the original yticklabels in the following image:

To rotate them, we will first get the yticklabels of the heatmap and then set the rotation to 0:

``````>>> heat_map.set_yticklabels(heat_map.get_yticklabels(), rotation=0)

``````

In the set_yticklabels, we passed two arguments. The first one gets the yticklabels of the heatmap and the second one sets the rotation. The result of the above line of code will be as follows:

The rotation attribute can be any angle:

``````>>> heat_map.set_yticklabels(heat_map.get_yticklabels(), rotation=35)

``````

## Add text and values on the heatmap

In the earlier section, we only added values on the heatmap. In this section, we will add values along with the text on the heatmap.

Consider the following example:

Create random test data:

``````>>> data = np.random.rand(4, 6)

``````

Now create an array for the text that we will write on the heatmap:

``````>>> text = np.asarray([['a', 'b', 'c', 'd', 'e', 'f'], ['g', 'h', 'i', 'j', 'k', 'l'], ['m', 'n', 'o', 'p', 'q', 'r'], ['s', 't', 'u', 'v', 'w', 'x']])

``````

Now we have to combine the text with the values and add the result onto heatmap as a label:

``````>>> labels = (np.asarray(["{0}\n{1:.2f}".format(text,data) for text, data in zip(text.flatten(), data.flatten())])).reshape(4,6)

``````

Okay, so here we passed the data in the text array and in the data array and then flatten both arrays into simpler text and zip them together. The resultant is then reshaped to create another array of the same size which now contains both text and data.

variable will be added to heatmap using annot:

``````>>> heat_map = sb.heatmap(data, annot=labels, fmt='')

``````

The attribute fmt is a string which should be added when adding annotation other than True and False.

On plotting this heatmap, the result will be as follows:

Working with seaborn heatmaps is very easy. I hope you find the tutorial useful.

Thank you.

#python

1619518440

## top 30 Python Tips and Tricks for Beginners

Welcome to my Blog , In this article, you are going to learn the top 10 python tips and tricks.

### 8) Check The Memory Usage Of An Object.

#python #python hacks tricks #python learning tips #python programming tricks #python tips #python tips and tricks #python tips and tricks advanced #python tips and tricks for beginners #python tips tricks and techniques #python tutorial #tips and tricks in python #tips to learn python #top 30 python tips and tricks for beginners

1620466520

If you accumulate data on which you base your decision-making as an organization, you should probably think about your data architecture and possible best practices.

If you accumulate data on which you base your decision-making as an organization, you most probably need to think about your data architecture and consider possible best practices. Gaining a competitive edge, remaining customer-centric to the greatest extent possible, and streamlining processes to get on-the-button outcomes can all be traced back to an organization’s capacity to build a future-ready data architecture.

In what follows, we offer a short overview of the overarching capabilities of data architecture. These include user-centricity, elasticity, robustness, and the capacity to ensure the seamless flow of data at all times. Added to these are automation enablement, plus security and data governance considerations. These points from our checklist for what we perceive to be an anticipatory analytics ecosystem.

#big data #data science #big data analytics #data analysis #data architecture #data transformation #data platform #data strategy #cloud data platform #data acquisition

1593156510

## Basic Data Types in Python | Python Web Development For Beginners

At the end of 2019, Python is one of the fastest-growing programming languages. More than 10% of developers have opted for Python development.

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.

I Mutable objects

II Immutable objects

III Built-in data types in Python

## Mutable objects

The Size and declared value and its sequence of the object can able to be modified called mutable objects.

Mutable Data Types are list, dict, set, byte array

## Immutable objects

The Size and declared value and its sequence of the object can able to be modified.

Immutable data types are int, float, complex, String, tuples, bytes, and frozen sets.

id() and type() is used to know the Identity and data type of the object

a**=25+**85j

type**(a)**

output**:<class’complex’>**

b**={1:10,2:“Pinky”****}**

id**(b)**

output**:**238989244168

## Built-in data types in Python

a**=str(“Hello python world”)****#str**

b**=int(18)****#int**

c**=float(20482.5)****#float**

d**=complex(5+85j)****#complex**

e**=list((“python”,“fast”,“growing”,“in”,2018))****#list**

f**=tuple((“python”,“easy”,“learning”))****#tuple**

g**=range(10)****#range**

h**=dict(name=“Vidu”,age=36)****#dict**

i**=set((“python”,“fast”,“growing”,“in”,2018))****#set**

j**=frozenset((“python”,“fast”,“growing”,“in”,2018))****#frozenset**

k**=bool(18)****#bool**

l**=bytes(8)****#bytes**

m**=bytearray(8)****#bytearray**

n**=memoryview(bytes(18))****#memoryview**

## Numbers (int,Float,Complex)

Numbers are stored in numeric Types. when a number is assigned to a variable, Python creates Number objects.

#signed interger

age**=**18

print**(age)**

Output**:**18

Python supports 3 types of numeric data.

int (signed integers like 20, 2, 225, etc.)

float (float is used to store floating-point numbers like 9.8, 3.1444, 89.52, etc.)

complex (complex numbers like 8.94j, 4.0 + 7.3j, etc.)

A complex number contains an ordered pair, i.e., a + ib where a and b denote the real and imaginary parts respectively).

## String

The string can be represented as the sequence of characters in the quotation marks. In python, to define strings we can use single, double, or triple quotes.

# String Handling

‘Hello Python’

#single (') Quoted String

“Hello Python”

# Double (") Quoted String

“”“Hello Python”“”

‘’‘Hello Python’‘’

# triple (‘’') (“”") Quoted String

In python, string handling is a straightforward task, and python provides various built-in functions and operators for representing strings.

The operator “+” is used to concatenate strings and “*” is used to repeat the string.

“Hello”+“python”

output**:****‘Hello python’**

"python "*****2

'Output : Python python ’

#python web development #data types in python #list of all python data types #python data types #python datatypes #python types #python variable type

1619510796

## Lambda, Map, Filter functions in python

Welcome to my Blog, In this article, we will learn python lambda function, Map function, and filter function.

Lambda function in python: Lambda is a one line anonymous function and lambda takes any number of arguments but can only have one expression and python lambda syntax is

Syntax: x = lambda arguments : expression

Now i will show you some python lambda function examples:

#python #anonymous function python #filter function in python #lambda #lambda python 3 #map python #python filter #python filter lambda #python lambda #python lambda examples #python map

1617988080

## How To Blend Data in Google Data Studio For Better Data Analysis

Using data to inform decisions is essential to product management, or anything really. And thankfully, we aren’t short of it. Any online application generates an abundance of data and it’s up to us to collect it and then make sense of it.

Google Data Studio helps us understand the meaning behind data, enabling us to build beautiful visualizations and dashboards that transform data into stories. If it wasn’t already, data literacy is as much a fundamental skill as learning to read or write. Or it certainly will be.

Nothing is more powerful than data democracy, where anyone in your organization can regularly make decisions informed with data. As part of enabling this, we need to be able to visualize data in a way that brings it to life and makes it more accessible. I’ve recently been learning how to do this and wanted to share some of the cool ways you can do this in Google Data Studio.

#google-data-studio #blending-data #dashboard #data-visualization #creating-visualizations #how-to-visualize-data #data-analysis #data-visualisation