[Python] How to draw a scatter plot with Matplotlib

Use scatter to draw a scatter plot. Some examples are shown below.

Simple scatter plot

Below is an example of the simplest scatter plot.

import numpy as np
import matplotlib.pyplot as plt

# generate data
x = np.random.rand(100)
y = np.random.rand(100)

fig = plt.figure()

ax = fig.add_subplot(1,1,1)

ax.scatter(x,y)

ax.set_title('first scatter plot')
ax.set_xlabel('x')
ax.set_ylabel('y')

fig.show()

scatter01.png

Change the color for each data

You can change the color by specifying c ='red' in the parameter.

import numpy as np
import matplotlib.pyplot as plt

# generate data
x1 = np.random.rand(100)*0.5
y1 = np.random.rand(100)

x2 = np.random.rand(100)*0.5 + 0.5
y2 = np.random.rand(100)

fig = plt.figure()

ax = fig.add_subplot(1,1,1)

ax.scatter(x1,y1, c='red')
ax.scatter(x2,y2, c='blue')

ax.set_title('second scatter plot')
ax.set_xlabel('x')
ax.set_ylabel('y')

fig.show()

scatter02.png

The same result is obtained even if specified in RGB. At this time, specify a value of 0.0 --1.0.

ax.scatter(x1,y1, c=(1.0,0,0))
ax.scatter(x2,y2, c=(0, 0, 1.0))

Add legend and grid line

The legend uses legend. You can change the display position with an argument. If you want to draw a grid line, use grid (True).

position
upper right
upper left
lower left
lower right
right
center left
center right
lower center
upper center
center
import numpy as np
import matplotlib.pyplot as plt

# generate data
x1 = np.random.rand(100)*0.5
y1 = np.random.rand(100)*0.5

x2 = np.random.rand(100)*0.5 + 0.5
y2 = np.random.rand(100)*0.5

x3 = np.random.rand(100)*0.5
y3 = np.random.rand(100)*0.5 + 0.5

x4 = np.random.rand(100)*0.5 + 0.5
y4 = np.random.rand(100)*0.5 + 0.5

fig = plt.figure()

ax = fig.add_subplot(1,1,1)

ax.scatter(x1,y1, c='red', label='group1')
ax.scatter(x2,y2, c='blue', label='group2')
ax.scatter(x3,y3, c='green', label='group3')
ax.scatter(x4,y4, c='yellow', label='group4')

ax.set_title('third scatter plot')
ax.set_xlabel('x')
ax.set_ylabel('y')

ax.grid(True)

ax.legend(loc='upper left')
fig.show()

scatter03.png

Change the marker

Markers are specified as marker ='o'. Four typical markers were used as an example. There are many others. See here.

import numpy as np
import matplotlib.pyplot as plt

# generate data
x1 = np.random.rand(100)*0.5
y1 = np.random.rand(100)*0.5

x2 = np.random.rand(100)*0.5 + 0.5
y2 = np.random.rand(100)*0.5

x3 = np.random.rand(100)*0.5
y3 = np.random.rand(100)*0.5 + 0.5

x4 = np.random.rand(100)*0.5 + 0.5
y4 = np.random.rand(100)*0.5 + 0.5

fig = plt.figure()

ax = fig.add_subplot(1,1,1)

ax.scatter(x1,y1, c='red', marker='.', label='group1')
ax.scatter(x2,y2, c='blue',marker='o', label='group2')
ax.scatter(x3,y3, c='green',marker='^', label='group3')
ax.scatter(x4,y4, c='yellow',marker='s', label='group4')

ax.set_title('fourth scatter plot')
ax.set_xlabel('x')
ax.set_ylabel('y')

ax.grid(True)

ax.legend(loc='upper left')
fig.show()

scatter04.png

Change the size of the marker

The size of the marker is a parameter such as s = 20. The default size is 20.

import numpy as np
import matplotlib.pyplot as plt

# generate data
x1 = np.random.rand(100)*0.5
y1 = np.random.rand(100)*0.5

x2 = np.random.rand(100)*0.5 + 0.5
y2 = np.random.rand(100)*0.5

x3 = np.random.rand(100)*0.5
y3 = np.random.rand(100)*0.5 + 0.5

x4 = np.random.rand(100)*0.5 + 0.5
y4 = np.random.rand(100)*0.5 + 0.5

fig = plt.figure()

ax = fig.add_subplot(1,1,1)

ax.scatter(x1,y1, c='red', s=20, marker='o', label='group1')
ax.scatter(x2,y2, c='blue',s=40, marker='o', label='group2')
ax.scatter(x3,y3, c='green',s=80, marker='o', label='group3')
ax.scatter(x4,y4, c='yellow',s=120, marker='o', label='group4')

ax.set_title('fifth scatter plot')
ax.set_xlabel('x')
ax.set_ylabel('y')

ax.grid(True)

ax.legend(loc='upper left')
fig.show()

scatter05.png

Recommended Posts

[Python] How to draw a scatter plot with Matplotlib
[Python] How to draw a line graph with Matplotlib
[Python] How to draw multiple graphs with Matplotlib
[Python] How to draw a histogram in Matplotlib
[Python] How to create a 2D histogram with Matplotlib
Draw a line / scatter plot on the CSV file (2 columns) with python matplotlib
How to draw a graph using Matplotlib
How to read a CSV file with Python 2/3
How to draw a 2-axis graph with pyplot
Try to draw a life curve with python
Create 3D scatter plot with SciPy + matplotlib (Python)
[Python] I want to make a 3D scatter plot of the epicenter with Cartopy + Matplotlib!
[Python] If you want to draw a scatter plot of multiple clusters
How to draw a bar graph that summarizes multiple series with matplotlib
How to draw a vertical line on a heatmap drawn with Python seaborn
I tried to draw a route map with Python
Forcibly draw something like a flowchart with Python, matplotlib
[Python] Road to a snake charmer (5) Play with Matplotlib
How to write a Python class
Create plot animation with Python + Matplotlib
Python: How to use async with
A python graphing manual with Matplotlib.
Draw a loose graph with matplotlib
How to get started with Python
How to calculate date with python
Easy to draw graphs with matplotlib
Continuously color with matplotlib scatter plot
Draw Lyapunov Fractal with Python, matplotlib
Lognormal probability plot with Python, matplotlib
Study math with Python: Draw a sympy (scipy) graph with matplotlib
How to install NPI + send a message to line with python
How to build a python2.7 series development environment with Vagrant
How to batch start a python program created with Jupyter notebook
How to title multiple figures with matplotlib
How to add a package with PyCharm
[Python] How to make a class iterable
[Introduction to Python] How to split a character string with the split function
[Python] How to convert a 2D list to a 1D list
[Python 3.8 ~] How to define a recursive function smartly with a lambda expression
How to make a surveillance camera (Security Camera) with Opencv and Python
How to work with BigQuery in Python
[Python] How to invert a character string
How to create a heatmap with an arbitrary domain in Python
How to get a stacktrace in python
How to do portmanteau test with python
[Python] How to get a value with a key other than value with Enum
How to enter Japanese with Python curses
[Python] How to deal with module errors
[ROS2] How to play a bag file with python format launch
How to send a request to the DMM (FANZA) API with python
How to display legend marks in one with Python 2D plot
How to plot a lot of legends by changing the color of the graph continuously with matplotlib
How to run a Maya Python script
How to install python3 with docker centos
[Small story] How to save matplotlib graphs in a batch with Jupyter
[Python] Explains how to use the range function with a concrete example
(Matplotlib) I want to draw a graph with a size specified in pixels
[Introduction to Python] How to sort the contents of a list efficiently with list sort
3. Natural language processing with Python 1-2. How to create a corpus: Aozora Bunko
How to upload with Heroku, Flask, Python, Git (4)
Draw a flat surface with a matplotlib 3d graph