Matplotlib scatter marker

In this Python tutorial, we will discuss a scatter plot with a marker using matplotlib in python. And we will also cover the following topics:

  • Matplotlib scatter marker
  • Matplotlib scatter marker how to plot
  • Matplotlib scatter marker example
  • Matplotlib scatter marker multiple plot
  • Matplotlib scatter marker color
  • Matplotlib scatter marker different color for each marker
  • Matplotlib scatter maker size
  • Matplotlib scatter marker different size for each marker
  • Matplotlib scatter marker style
  • Matplotlib scatter marker different style for each marker
  • Matplotlib scatter marker colormap
  • Matplotlib scatter marker outline
  • Matplotlib scatter marker text
  • Matplotlib scatter marker color by value or category
  • Matplotlib scatter marker transparent

Matplotlib scatter marker

  • Matplotlib provides a pyplot module for data visualization.
  • Under the pyplot module, we have a scatter() function to plot a scatter graph.
  • Basically, the scatter() method draws one dot for each observation.
  • In matplotlib, plotted points are known as “markers“.
  • So that’s why it is called as scatter marker.

Matplotlib scatter marker how to plot

  • scatter() method is used to draw a scatter plot.
  • It takes the data in the form of arrays.
  • It takes values in two arrays of the same length one for the x-axis and the other for the y-axis.

The following steps are used to create a matplotlib scatter marker which is outlined below:

  • Defining Libraries: Import the important libraries which are required for the creation of the scatter marker ( For visualization: pyplot from matplotlib, For data creation and manipulation: NumPy).
  • Define X and Y: Define the data values on X-axis and Y-axis. It takes data values in the form of an array. Both the are must be of the same size.
  • Plot a scatter: By using the scatter() method we can plot a graph.
  • Display: At last display the plot by using the show() function.

The syntax to create a matplotlib scatter marker chart is as below:

matplotlib.pyplot.scatter(x, y, s=None, c=None, marker=None, ....)

The above-used parameters are outlined as below:

  • x: specify data position on the x-axis.
  • y: specify data position on the y-axis.
  • s: specify the marker size.
  • c: specify the color of dots.
  • marker: specify the kind of marker.

Matplotlib scatter marker example

In the above sections, we discussed what a scatter marker graph exactly means. And we have also discussed what are the various steps used to create or plot a matplotlib scatter marker. Now, let’s see how to plot a scatter marker using matplotlib.

Let’s understand the concept with the help of an example as below:

# Import Libraries

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.array([5,7,8,7,2])
y = np.array([99,103,87,94,78])

# Plot a scatter marker

plt.scatter(x, y)

# Display a graph

plt.show()
  • In the above, example we import the matplotlib.pyplot and numpy library.
  • Then we define the X-axis and Y-axis points in the array form.
  • plt.scatter() method is used to draw a points for each data points.
  • Then we finally use the method plt.show() to display the plotted graph.
Matplotlib scatter marker example
Matplotlib scatter marker

Read: How to install matplotlib python

Matplotlib scatter marker multiple plot

What happens if we want to draw multiple scatter markers plot in the same figure.

Let’s understand the concept with the help of an example:

# Import Libraries

import matplotlib.pyplot as plt
import numpy as np

# Plot data 

x = np.array([5,7,8,7])
y = np.array([78,77,85,86])
plt.scatter(x, y)

# Plot data

x = np.array([4,7,14,12])
y = np.array([100,105,84,105])
plt.scatter(x, y)

# Display Graph

plt.show()
  • In the above example, we conclude that when we plot two different data, the plots were plotted with two different colors.
  • By default scatter markers are of blue and orange color.
Matplotlib scatter marker multiple plot in the same figure
plt.scatter()

Matplotlib scatter marker color

We can set the color of our choice for each scatter’s plot.

The syntax to change the color is as given below:

matplotlib.pyplot.scatter(x, y, color=None)

The parameter of the given syntax is outlined below:

  • x: specify data position on the x-axis.
  • y: specify data position on the y-axis.
  • color: To set the color of your choice. Shorthand for color is ‘c‘.

Result: All the markers are of the same color.

Let’s understand the concept with the help of an example:

# Import Libraries

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.array([5,7,8,7,2])
y = np.array([99,103,87,94,78])

# Plot a scatter marker

plt.scatter(x, y, color='red')

                       #OR

plt.scatter(x, y, c='cyan')


# Display a graph

plt.show()
  • In the above, example we import the matplotlib.pyplot and numpy library.
  • Then we define the X-axis and Y-axis points in the array form.
  • plt.scatter() method is used to draw points for each data point and we pass an argument color to set the color of the plot.
  • Then we finally use the method plt.show() to display the plotted graph.
Matplotlib scatter marker color
plt.scatter() “Output of case1”
Matplotlib scatter marker having color
plt.scatter() “Output of case2”

Read: Matplotlib plot a line

Matplotlib scatter marker different color for each marker

  • If we want to set a different color for each dot or marker we have to define an array consist of colors as values.
  • Both the array must be of the same size i.e the color one’s or values one’s.

The syntax for setting different colors for each marker is given below:

matplotlib.pyplot.scatter(x,y,c=None)

The parameter of the given syntax is outlined below:

  • x: specifies the position of data on the x-axis.
  • y: specifies the position of data on the y-axis.
  • c: To set the different colors for each dot.

Result: All the markers are of different colors.

Note: For setting different colors for each dot pass ‘c‘ as an argument or color argument.

Let’s understand the concept with the help of an example:

# Import Libraries


import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.array([5, 7, 8, 7, 2, 6, 12, 3, 5, 63, 22, 69])
y = np.array([10, 15, 12, 11, 63, 45, 56, 36, 56, 99, 21, 23])
colors_set = np.array(["orange","purple","beige","brown","gray","cyan","magenta","red","green","blue","yellow","pink"])

# Plot a scatter marker

plt.scatter(x, y, c=colors_set)

# Display a graph

plt.show()
  • In the above, example we import the matplotlib.pyplot and numpy library. Then we define the X-axis and Y-axis points in the array form.
  • plt.scatter() method is used to draw points for each data point and we pass an argument ‘c’ to set the different color for each dot. Then we finally use the method plt.show() to display the plotted graph
Matplotlib scatter marker different color for each dot or marker
plt.scatter()

Read: Python plot multiple lines using Matplotlib

Matplotlib scatter marker size

If we want to set the size of the marker according to our choice. We have to pass the argument ‘s‘.

We can easily increase or decrease the size of the marker according to needs.

The syntax to change the size of the marker is given below:

matplotlib.pyplot.scatter(x, y, s=None)

The parameter of the given syntax is outlined below:

  • x: specifies the position of data on the x-axis.
  • y: specifies the position of data on the y-axis.
  • s: To set the size of the marker.

Result: All the markers are of the same size.

Let’s understand the concept with the help of an example given below:

# Import Libraries

import matplotlib.pyplot as plt
import numpy as np

# Define Data


x = np.array([99,86,88,111,103,87,94,78,77,85,86])
y = np.array([20,50,200,500,1000,60,90,10,300,600,800])

# Define scatter() function

plt.scatter(x, y, s=115)

# Display the graph

plt.show()
  • In the above, example we import the matplotlib.pyplot and numpy library.
  • Then we define the X-axis and Y-axis points in the array form.
  • plt.scatter() method is used to draw points for each data point and we pass an argument ‘s’ to set the size of the marker.
  • Then we finally use the method plt.show() to display the plotted graph.
Matplotlib scatter marker size
Matplotlib scatter marker size

Read: What is matplotlib inline

Matplotlib scatter marker different sizes for each marker

When we want to modify the size of each marker. We have to pass the argument ‘s‘ to the method.

This function increase or decrease the size of each marker.

The syntax for the setting the size of each marker is given below:

matplotlib.pyplot.scatter(x, y, s=None)

The parameter of the given syntax is outlined below:

  • x: specifies the position of data on the x-axis.
  • y: specifies the position of data on the y-axis.
  • s: To set thediiferent sizes for each marker. The default size is rcParams[‘lines.markersize’]**2

Result: Each marker is of a different size.

Let’s understand the concept with the help of an example given below:

# Import Libraries

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.array([99,86,88,111,103,87,94,78,77,85,86])
y = np.array([20,50,200,500,1000,60,90,10,300,600,800])

# Define scatter() function

sizes = (np.random.sample(size=x.size) * 75) ** 2
plt.scatter(x, y, s=sizes)

# Display the graph

plt.show()
  • In the above, example we import the matplotlib.pyplot and numpy library.
  • Then we define the x-axis and y-axis points in the array form.
  • plt.scatter() method is used to draw a marker for each data point. To change the size of each marker we have to pass the argument ‘s’ to the function.
  • Then, we finally use the method plt.show() to display the plotted graph.
Matplotlib scatter marker different sizes for each marker
plt.scatter()

Read: Matplotlib plot bar chart

Mtplotlib scatter marker style

The programmer can customize the style or shape of the marker. Matplotlib provides the feature to change the marker style.

By using the parameter ‘marker‘, we can change the style or shape of the markers whenever we want.

Some common types of marker style:

MarkerDescription
‘.’Point
‘o’Circle
‘+’Plus
‘>’Triangle Right
‘<‘Triangle Left
‘s’Square
‘x’X
Different Markers

The syntax to change the style of the marker is given below:

matplotlib.pyplot.scatter(x, y, marker=None)

The parameter of the given syntax is outlined below:

  • x: specifies the position of data on the x-axis.
  • y: specifies the position of data on the y-axis.
  • marker: To set the diiferent style of the marker. By default the style is Circle.

Result: Markers of different styles.

Let’s discuss the above concept with the help of an example:

# Import Libraries

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.array([99,86,88,111,103,87,94,78,77,85,86])
y = np.array([20,50,200,500,1000,60,90,10,300,600,800])

# Define scatter() function


plt.scatter(x, y, marker='>', s = 65)

                     #OR

plt.scatter(x, y, marker='s', s = 165)


# Display the graph

plt.show()
  • In the above example, we import the matplotlib.pyplot and numpy library.
  • Then we define the x-axis and y-axis points in the form of an array.
  • plt.scatter() method is used to draw markers for each data point and we pass the parameter ‘marker’ to set the style of the marker.
  • Then we finally use the method plt.show() to display the plotted graph.
Mtplotlib scatter marker style
plt.scatter() “Output of Case1”
Mtplotlib scatter marker having style
plt.scatter() “Output of Case2”

Read: Matplotlib subplot tutorial

Matplotlib scatter marker different style for each marker

To differentiate between different groups of data, we have to use different styles of markers. There is no way to define multiple marker styles in a single call to scatter() method.

So, to use different markers styles for different groups’ data we have to scatter() method each time.

The syntax to change the style of each marker:

matplotlib.pyplot.scatter(x, y, marker=None) // Call each time

The parameter of the given syntax is outlined below:

  • x: specifies the position of data on the x-axis.
  • y: specifies the position of data on the y-axis.
  • marker: To set the diiferent style of the each marker. Call the function every time.

Result: Return marker with a different style to easily distinguish between different groups of data.

Let’s understand the concept with the help of an example:

# Import Libraries


import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.array([10, 20, 30, 15, 25, 13, 8, 2])
y1 = np.array([1, 6, 8, 12, 25, 15, 10, 7.5])
y2= np.array([6.5, 3.2, 16, 15, 19, 23, 18.6, 29])

# Define scatter() function

plt.scatter(x, y1, marker='s', s = 265)
plt.scatter(x, y2, marker='d', s = 265)

# Display the graph

plt.show()
  • In the above example, we import the matplotlib.pyplot and numpy library.
  • Then we dine the x-axis and y-axis points in the form of an array.
  • plt.scatter() method is used to draw markers for each data point and we pass the parameter ‘marker’ to set the style of the marker. To set each marker of a different style you have to call the scatter() method each time.
  • Then we finally use the method plt.show() to display the plotted graph.
Matplotlib scatter marker different style for each marker
plt.scatter()

Read: Matplotlib subplots_adjust

Matplotlib scatter marker colormap

Sometimes, we have to plot the data which depends upon some other data. In such cases, we use color maps.

If you want to include a colormap strip in the graph area use the function plt.colorbar()

The syntax for this is given below:

matplotlib.pyplot.scatter(x, y, c=None, cmap=None) 
matplotlib.pyplot.colorbar()

The parameter of the given syntax is outlined below:

  • x: specifies the position of data on the x-axis.
  • y: specifies the position of data on the y-axis.
  • c: specifies the array of colors. It ranges from 0 to 100.
  • cmap: specifies the colour map.

Let’s understand the concept with the help of an example:

# Import Libraries

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.array([99,86,88,111,103,87,94,78,77,85,86,23,15])
y = np.array([20,50,200,500,1000,60,90,10,300,600,800,65,12])
colors = np.array([0, 10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90, 100])

# Define scatter() function


plt.scatter(x, y, c=colors, cmap= 'PiYG')

                   #Or


plt.scatter(x, y, c=colors, cmap= 'Dark2')

plt.colorbar()


# Display the graph

plt.show()
  • In the above example, we import the matplotlib.pyplot and numpy library.
  • Then we dine the x-axis and y-axis points in the form of an array.
  • plt.scatter() method is used to draw markers for each data point and we pass the parameter ‘cmap’ to set the color map.
  • plt.colorbar() method is used to show the color strip in the plot.
  • Then we finally use the method plt.show() to display the plotted graph.
Matplotlib scatter marker colormap
plt.scatter() “Output of case1”
Matplotlib scatter marker having colormap
plt.colorbar() “Output of case2”

Read: Matplotlib log log plot

Matplotlib scatter marker outline

We can change the outline of each marker by changing its edge colors. Basically, it creates the outline around the markers with a specific color.

The syntax for this is given below:

matplotlib.pyplot.scatter(x, y, c=None, s=None, edgecolors=None, cmap=None)

The parameter of the given syntax is outlined below:

  • x: specifies the position of data on the x-axis.
  • y: specifies the position of data on the y-axis.
  • c: specifies the array of colors. It ranges from 0 to 100.
  • s: specifies the size of the marker
  • edgecolors: to set the edge color of the marker
  • cmap: specifies the colour map.

Let’s understand the concept with the help of an example:

# Import Libraries

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.array([99,86,88,111,103,87,94,78,77,85,86,23,15])
y = np.array([20,50,200,500,1000,60,90,10,300,600,800,65,12])
colors = np.array([0, 10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90, 100])

# Define scatter() function

plt.scatter(x, y, c=colors, edgecolors= 'k', s= 120, cmap= 'Dark2')
plt.colorbar()


# Display the graph

plt.show()
  • In the above example, we import the matplotlib.pyplot and numpy library.
  • Then we dine the x-axis and y-axis points in the form of an array.
  • plt.scatter() method is used to draw markers for each data point and we pass the parameter ‘edgecolors’ to set the color of markers edges. Here we set edhe color to black.
  • plt.colorbar() method is used to show the color strip in the plot.
  • Then we finally use the method plt.show() to display the plotted graph.
Matplotlib scatter marker outline
plt.scatter()

Read: Matplotlib plot_date

Matplotlib scatter marker text

When we want to place a text next to the scatter in matplotlib this process is known as Annotation.

There are two types of Annotation depends upon the number of scatter points we want to annotate.

Types of Annotation are as follow:

  • Single point Annotation
  • Multiple point Annotation

Matplotlib scatter marker text – single point annotation

In single-point annotation, we can place text at a specific point.

When can use plt.text() method to mention the text.

The syntax for single point annotation is given below:

matplotlib.plot.text(x, y, s)

The parameter of the given syntax is outlined below:

  • x: specifies x-axis point to place the text.
  • y: specifies y-axis point to place the text.
  • s: specifies the text.

Let’s understand the concept with the help of an example:

# Import Libraries

import matplotlib.pyplot as plt
import numpy as np

# Define Data


x = np.array([99,86,88,111,103,87,94,78,77,85,86])
y = np.array([20,50,200,500,1000,60,90,10,300,600,800])

# Define scatter() function

plt.scatter(x, y)

# Annotate the point

plt.text(99,20, 'Point 1')

# Display the graph

plt.show()
  • In the above example, we import the matplotlib.pyplot and numpy library.
  • Then we dine the x-axis and y-axis points in the form of an array.
  • plt.scatter() method is used to draw markers for each data point.
  • plt.text() method is used to annotate the single point.
  • Then we finally use the method plt.show() to display the plotted graph.
Matplotlib scatter marker text
plt.text()

Matplotlib scatter marker text – all points annotation

If we want to annotate all points in the scatter plot, we have annotate() method in matplotlib.

The syntax for this is given below:

matplotlib.pyplot.annotate(text, (xy))

The parameter of the given syntax is outlined below:

  • text: specifies the text for annotation.
  • xy: specifies points to be annotated.

Let’s understand the concept with the help of an example:

# Import Libraries

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = [x for x in range(5)]
y = np.array([20,50,200,500,1000])
text = ["first", "second", "third", "fourth", "fifth"]

# Define scatter() function

plt.scatter(x, y)

# Annotate the point

for i in range(len(x)):
    plt.annotate(text[i], (x[i], y[i] + 0.2))

# Display the graph

plt.show()
  • In the above example, we import the matplotlib.pyplot and numpy library.
  • Then we dine the x-axis and y-axis points in the form of an array.
  • plt.scatter() method is used to draw markers for each data point.
  • plt.annotate() method is used to annotate the single point.
  • Then we finally use the method plt.show() to display the plotted graph.
Matplotlib scatter marker having text
plt.annotate()

Read: Matplotlib dashed line

Matplotlib scatter marker color by value or category

In this section, we are going to learn how to color the data points or scatter markers by value or category.

For grouping use ‘groupby‘ clause.

Let’s understand the concept with the help of an example:

# Import Libraries

import pandas as pd
import matplotlib.pyplot as plt

#create DataFrame

df = pd.DataFrame({'x': [10, 12, 13, 14, 19, 23, 25, 30],
                   'y': [5, 7, 7, 9, 12, 9, 9, 4],
                   'z': ['A', 'A', 'A', 'B', 'B', 'C', 'C', 'C']})

#view DataFrame

df

#groupby category

groups = df.groupby('z')
for name, group in groups:
    plt.scatter(group.x, group.y, label=name)

# Display the graph

plt.legend()
plt.show()
  • In the above example, we import the matplotlib.pyplot and pandas library.
  • Then we create a data frame and define the x-axis and y-axis points in the form of an array.
  • plt.scatter() method is used to draw markers for each data point.
  • groupby clause for dividing into groups
  • Then we finally use the method plt.show() to display the plotted graph.

Read: Matplotlib plot error bars

Matplotlib scatter marker transparent

When we want to check the density of data plotted and due to lots of overlapping we are not able to see, at that time we need transparency.

The alpha argument is used to make the scatter marker transparent.

alpha parameter removes the outlines of the scatter.

The syntax for transparency is given below:

matplotlib.pyplot.scatter(x, y, alpha=None)

The parameter of the given syntax is outlined below:

  • x: specifies the data point on the x-axis.
  • y: specifies the data point on the y-axis.
  • alpha: set the transparency value. By default value is 1 i.e. no transparency.

Let’s understand the concept with the help of an example:

# Import Libraries

import matplotlib.pyplot as plt
import numpy as np

# Define Data


x = np.array([99,86,88,111,103,87,94,78,77,85,86])
y = np.array([20,50,200,500,1000,60,90,10,300,600,800])

# Define scatter() function

sizes = (np.random.sample(size=x.size) * 75) ** 2
plt.scatter(x, y, s=sizes, alpha=0.3, c='red')

# Display the graph

plt.show()
  • In the above example, we import the matplotlib.pyplot and pandas library.
  • Then we dine the x-axis and y-axis points in the form of an array.
  • plt.scatter() method is used to draw markers for each data poit and pass the argument ‘alpha‘ to set the transparency. We set alpha =0.3
  • Then we finally use the method plt.show() to display the plotted graph.
Matplotlib scatter marker transparent
plt.scatter()

Also, take a look at some more articles:

In this Python tutorial, we have discussed the “Matplotlib scatter marker” and we have also covered some examples related to it. There are the following topics that we have discussed in this tutorial.

  • Matplotlib scatter marker
  • Matplotlib scatter marker how to plot
  • Matplotlib scatter marker example
  • Matplotlib scatter marker multiple plot
  • Matplotlib scatter marker color
  • Matplotlib scatter marker different color for each marker
  • Matplotlib scatter maker size
  • Matplotlib scatter marker different size for each marker
  • Matplotlib scatter marker style
  • Matplotlib scatter marker different style for each marker
  • Matplotlib scatter marker colormap
  • Matplotlib scatter marker outline
  • Matplotlib scatter marker text
  • Matplotlib scatter marker color by value or category
  • Matplotlib scatter marker transparent