search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

Drawing a scatterplot in Matplotlib

schedule Aug 10, 2023
Last updated
local_offer
PythonMatplotlib
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

Using the scatter function

To draw a basic 2D scatter plot in Matplotlib, we could use the scatter(~) function like so:

fig, ax = plt.subplots()
ax.scatter([5,2,3], [1,2,4])
plt.show()

The first argument is an array of your xs, and the second is an array of your ys.

This produces the following:

Using the plot function

To draw a basic 2D scatter plot in Matplotlib, we could also use the plot(~) function like so:

plt.plot([5,2,3], [1,2,4], "o")
plt.show()

Here, the third argument "o" indicates that you don't want the data points to be connected. This is needed since the default behavior is to connect the dots.

This produces the following:

Customizing our plots

Both the scatter(~) and plot(~) methods allow for flexible customization.

Marker size

Pass in the markersize parameter for plot(~) and the s parameter for scatter(~):

plt.scatter([3,4],[5,6], s=10)
plt.plot([3,4],[5,6], markersize=10)

Marker color

Pass in the color parameter for both plot(~) and scatter(~):

plt.plot([3,4], [5,6], color="r")
plt.plot([3,4], [5,6], color="red")
plt.plot([3,4], [5,6], color="#FF0000")
plt.plot([3,4], [5,6], color=(1,0,0)) # Warning: this isn't your typical (0-255) interval

Marker style

Pass in the marker parameter for both plot(~) and scatter(~):

plt.scatter([1], [1], marker="*", s=300) # star
plt.scatter([1], [2], marker="o", s=300) # circle
plt.scatter([1], [3], marker="+", s=300) # plus
plt.scatter([2], [1], marker="^", s=300) # triangle
plt.scatter([2], [2], marker="D", s=300) # diamond
plt.scatter([2], [3], marker="s", s=300) # square

This produces the following:

The colors are automatically chosen by Matplotlib.

Difference between functions scatter and plot

Both functions can be used to draw scatter plots, but the difference is that the plot(~) function is more efficient but less flexible than the scatter(~) function. The plot(~) function does not allow for customization of individual points (e.g. changing color and size), while the scatter(~) allows for this. The implication of such a difference is that the plot(~) function has far less jobs to handle, making it more efficient.

As a rule of thumb, if you do not need to customize individual points, then opt for the plot(~) function.

robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!