Scatter Plots -How to Plot Black Points?

Black Points In Scatter Plots

Scatter plots are the most used visualization techniques to plot and visualize the relationship between variables. Visualizing the data can help us to interpret, analyze and observe the trends of data which helps in storytelling because you have a grip on your data. This is especially useful for data scientists.

Machine Learning also needs visualization to understand the data that later helps in feature engineering and model selection. Visualization can also be used to convey the performance of a model, like its accuracy, prediction rate, and so on. So if you have a good visualization library by your side, your work will be done easily.

We have many such libraries available in Python that provide good visualization and support scatter plots. To name them, we have Matplotlib, Seaborn,ggplot,plotly, pandas, and so on.

This article focuses on creating a scatter plot with black points with the help of different libraries available in Python.

Before that, let us understand what a scatter plot is.

What Is a Scatter Plot?

Like any other visualizing medium, a scatter plot is also used to display the data to understand visually. Scatter plots are the most frequently used plots of this library by data scientists and analysts. These plots are used to determine the relationship between the variables plotted and how one point changes when the other variable changes. They are also used to observe the trends in the data points.

Let us see an example of a scatter plot using the matplotlib library.

import matplotlib.pyplot as plt
x=[1,2,3,4,5,6]
y=[13,11,10,17,23,15]
plt.scatter(x,y)
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.title("Example scatter plot")
plt.show()

In the first line, we are importing the matplotlib library as plt.

Next, we define the data points, x, and y, which are to be plotted.

The plt.scatteris used to plot the two points, x and y.

We have also specified the labels for the axes. The title of the plot is Example scatter plot.

The show the method is used to display the plot.

Example Scatter Plot
Example Scatter Plot

Refer to this article to learn how to plot multiple datasets on the same plot

Now that we have understood the basic functionality of scatter plots let us dive into the main issue at hand!

Plotting Black Points on a Scatter Plot Using Matplotlib

Before we move on with the code, we need to first install the matplotlib library.

pip install matplotlib

The plt.scatter method is used to plot the scatter plot, and the c argument of this method is used to specify the color of the points.

Let us see the syntax of the method.

matplotlib.pyplot.scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, *, edgecolors=None, plotnonfinite=False, data=None, **kwargs)

Refer to this article to learn more about this function

Let us get to the code.

import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [3, 6, 2, 8, 5]
plt.scatter(x, y, color='black')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Scatter Plot with Black Points')
plt.show()

As usual, we imported the matplotlib library, which we installed earlier.

The next two lines define the data points we need to plot. The x and y variables contain a list of points to be plotted.

The plt.scatter is used to plot the points with the color black.

The labels for the axes X and Y are set with the help of plt.label.

Next, the title of the graph is set using plt.title.

Lastly, the graph is packed and displayed with the help of show.

Black Points on Scatter Plot using Matplotlib
Black Points on Scatter Plot using Matplotlib

Plotting Black Points on a Scatter Plot Using Seaborn

The seaborn library cannot be treated differently from the matplotlib library as it is built on top of matplotlib to provide enhanceability. It is safe to say Seaborn is an extension of the matplotlib library. It uses seaborn.scatterplot to work with scatter plots.

To know more about the seaborn library, scroll through this article!

We need to install the library using the command given below.

pip install seaborn

The syntax of the function is given below.

seaborn.scatterplot(data=None, *, x=None, y=None, hue=None, size=None, style=None, palette=None, hue_order=None, hue_norm=None, sizes=None, size_order=None, size_norm=None, markers=True, style_order=None, legend='auto', ax=None, **kwargs)

To explain the function’s arguments briefly, the data argument is the data set we want to plot, for example, a data frame. The x and y arguments of the function take the columns of the data frame we want to plot, respectively. hue represents the various ranges of colors we wish to use for multiple data points. Style and palette give the colors palettes and styles of the colors to choose from.

import seaborn as sns
import matplotlib.pyplot as plt
x1 = [1, 2, 3, 4, 5, 6]
y1 = [3, 5, 2, 7, 1, 6]
sns.scatterplot(x=x1, y=y1, color='black')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Scatter Plot with Black Points Using Seaborn')
plt.show()

We are importing the seaborn and matplotlib libraries in the first two lines.

The x1 and y1 are the data points that we want to plot. The sns.scatterplot function is used to plot the points on a scatter plot. As said above, the seaborn library is built on the matplotlib library and depends on it for displaying the graphs. So we use the functionalities of the matplotlib library to make the plot complete. We specified the labels for the axes in the plot. and the title.

show is used to display the plot.

Black Points on Scatter Plot using Seaborn
Black Points on Scatter Plot using Seaborn

Black Points on Scatter Plot using Pandas

Up until now, we have observed how to plot two data points. But with Pandas Library, we can also plot data frames. Let us take a data frame and plot it using the pandas library.

import pandas as pd
import matplotlib.pyplot as plt
points_dictionary = {
    'x_points': [6,7,1,2,3],
    'y_points': [1,3,5,7,11]
}
df = pd.DataFrame(points_dictionary)
df.plot.scatter(x='x_points', y='y_points', c='black')
plt.xlabel('X-AXIS')
plt.ylabel('Y-AXIS')
plt.title('Black Points On a Scatter Plot')
plt.show()

Here is a brief explanation of the code.

The Pandas library is imported with its alias name pd in the very first line.

The points_dictionary is a dictionary of points that are stored in a data frame.

With the help of pd.DataFrame, we obtain a data frame called df from the dictionary above. The plot. scatter function is used to plot the data frame.

Key Observations: Since the Pandas library does not have any methods to display the graph, we used the matplotlib library’s methods to give a name to the labels of the graph, the title for the plot, and to display the plot.

Black Points On a Scatter Plot Using Pandas
Black Points On a Scatter Plot Using Pandas

Black Points on Scatter Plot using Bokeh

bokeh is one of the coolest visualization libraries, as it is famous for interactive visualizations. Bokeh is a Python library for creating interactive visualizations for modern web browsers. It helps you build beautiful graphics, ranging from simple plots to complex dashboards with streaming datasets. With Bokeh, you can create JavaScript-powered visualizations without writing any JavaScript yourself.

Learn more about the Bokeh Library here

Let us use this library to plot black points.

Before that, we need to install the library using the below command.

pip install bokeh

Let us see the code.

from bokeh.plotting import figure, show
from bokeh.io import output_notebook
output_notebook()
x = [1, 2, 3, 4, 5, 6]
y = [3, 5, 2, 7, 1, 6]
p = figure(plot_width=400, plot_height=400)
p.circle(x, y, size=8, color='black')
p.xaxis.axis_label = 'X-axis'
p.yaxis.axis_label = 'Y-axis'
p.title.text = 'Scatter Plot with Black Points'
show(p)

From the Bokeh library, we are importing figure and show.

Next, we import the output_notebook to display the output in our notebook environment.

The output_notebook is called, so the output is displayed in the notebook itself.

The data points are defined in the variables x and y.

We use figure for the outline of the plot.

The circle is used to define the points in black color. The labels are set using axis_label. The title is set with the help of title.text.

The show method is used to display the plot.

Black Points on Scatter Plot using Bokeh
Black Points on Scatter Plot using Bokeh

Summary

I hope you have enjoyed reading through the article! To recap what we have done. we recapitulated what is a scatter plot and how we can plot it using the popular visualization library – Matplotlib with the help of an example.

Coming to the main topic at hand, which is to plot black-colored points on the scatter plot, we focused on how to achieve this using the different visualization libraries available in Python.

First, we started with Matplotlib, where we used the plt.scatter to plot the graph using the color argument to set the points to black,

Next, we went with the extension of matplotlib called seaborn. Seaborn is built on top of the matplotlib library and heavily depends on matplotlib for displaying the plots. We used sns.scatterplot to plot black-colored points on a scatter plot.

The third library we used is the Pandas Library. We created a data frame and plotted it with the help of df.plot.scatter.

Last but not least, we used an interactive library called Bokeh to plot a scatter plot with black points.

References

Matplotlib Documentation

Seaborn Documentation

Pandas Documentation

Bokeh Documentation

Stack Overflow