How to create scatterplot with both negative and positive axes?
Last Updated :
26 Mar, 2021
Prerequisites: Matplotlib
In Scatter Plot the values of two different numeric variables are represented by dots or circles. It is also known as Scatter Graph or Aka Scatter Chart. The individual data point in the scatter plot is represented by each dot on the horizontal and vertical axis.
Generally, the scatter plot is plotted on the positive values, but what happens when the dataset has both negative and positive values. So in this article, we are creating the scatter plot with both negative and positive axes.
Installation:
For installing the matplotlib library, write the following command in your command prompt.
pip install matplotlib
With the help of some functions, we can plot the scatter plot with both negative and positive axes are as follows:
plt.scatter(x, y, c, cmap)
plt.axvline(x, ymin, ymax, c, ls)
plt.axhline(y, xmin, xmax, c, ls)
Function | Description |
scatter() | Creates the scatter plot on the given data. |
axvline() | Add a vertical line in data coordinates. |
axhline() | Add a horizontal line in data coordinates. |
Steps-by-Step Approach:
- Import necessary library.
- Create or import the dataset for creating the plot.
- Create the scatter plot using plt.scatter() in which pas x and y a parameter.
- Since the plot has negative and positive axes coordinates add a vertical and horizontal line in the plot using plt.axvline() and plt.axhline() function and pass the origin as 0 and color according to you and line style i.e, ls as you want.
- Plot is now separated into positive and negative axes.
- For make the plot more attractive and easy to understand give x label, y label, and title to the plot using plt.xlabel(), plt.ylabel() and plt.title() function in which pass the string as a parameter.
- Add the color scheme and grid style pass color and cmap i.e, colormap inside the scatter() function as discussed above, and for grid-style add plt.style.use() before creating a scatter plot in the program code and pass the style you want as a parameter.
- Add a color bar for visualizing the mapping of numeric values into color using plt.colormap() function.
- Now for visualizing the plot use plt.show() function.
Example 1: Creating Default Scatter plot with negative and positive axes using Matplotlib Library.
Python
# importing libraries
import matplotlib.pyplot as plt
import numpy as np
# creating two array for plotting
x = np.arange(-20, 20, 1)
y = [-30, -20, -10, -3, 1, 11, 10, 5, -20, 20, 15,
30, 20, 2, 4, 3, -7, -8, -13, -16, 16, -15, 32,
-12, -19, 25, -25, 30, -6, -18, -11, -14, -21,
27, -21, -14, -4, -1, 0, 17]
# creating scatter plot with both negative
# and positive axes
plt.scatter(x, y)
# visualizing the plot using plt.show() function
plt.show()
Output:
The above output shows the scatter plot with both negative and positive axes, but in this plot, it is difficult to analyze the points since some are on negative axes and some are on positive axes.
So let's make this plot easier to understand.
Example 2: Creating Scatter Plot with both negative and positive axes.
Python
# importing libraries
import matplotlib.pyplot as plt
import numpy as np
# creating two array for plotting
x = np.arange(-20, 20, 1)
y = [-30, -20, -10, -3, 1, 11, 10, 5, -20, 20, 15, 30, 20,
2, 4, 3, -7, -8, -13, -16, 16, -15, 32, -12, -19, 25,
-25, 30, -6, -18, -11, -14, -21, 27, -21, -14, -4, -1, 0, 17]
# creating scatter plot with both negative
# and positive axes
plt.scatter(x, y)
# adding vertical line in data co-ordinates
plt.axvline(0, c='black', ls='--')
# adding horizontal line in data co-ordinates
plt.axhline(0, c='black', ls='--')
# visualizing the plot using plt.show() function
plt.show()
Output:
In the above output, we have drawn the vertical and horizontal line from the center of data co-ordinates with the help of plt.axvline() function and plt.axhline() function by passing the 0 as a parameter which specifies that the line will be drawn at 0 co-ordinates, color parameter which specifies the color of the line and ls i,e. Line style.
Example 3: Creating Scatter plot with both negative and positive axes by adding color scheme.
Python
# importing libraries
import matplotlib.pyplot as plt
import numpy as np
# creating two array for plotting
x = np.arange(-20, 20, 1)
y = [-30, -20, -10, -3, 1, 11, 10, 5, -20, 20,
15, 30, 20, 2, 4, 3, -7, -8, -13, -16, 16,
-15, 32, -12, -19, 25, -25, 30, -6, -18,
-11, -14, -21, 27, -21, -14, -4, -1, 0, 17]
# creating scatter plot with both negative
# and positive axes
plt.scatter(x, y, c=y, cmap='plasma')
# adding vertical line in data co-ordinates
plt.axvline(0, c='black', ls='--')
# adding horizontal line in data co-ordinates
plt.axhline(0, c='black', ls='--')
# giving X and Y label
plt.xlabel("X axis")
plt.ylabel("Y axis")
# giving title to the plot
plt.title("Scatter Plot with both negative and positive axes")
# visualizing the mapping from values to colors
plt.colorbar()
# visualizing the plot using plt.show() function
plt.show()
Output:
In the above output, we have added the color scheme to plasma and given the color with respect to y values and added the color bar to visualize the mapping from values to color and given the x label, y label, and title to the plot so that plot looks more interactive and easy to understand.
For adding color scheme we had passes the parameter c which refers to give the color and cmap stands for color map having a list of register colormap, added color map by using plt.colormap() function and added X label, Y label, and title using plt.xlabel() function plt.ylabel() function and plt.title() respectively.
Example 4: Creating Scatter Plot with negative and positive axes by adding theme.
Python
# importing libraries
import matplotlib.pyplot as plt
import numpy as np
# creating two array for plotting
x = np.arange(-20, 20, 1)
y = [-30, -20, -10, -3, 1, 11, 10, 5, -20, 20,
15, 30, 20, 2, 4, 3, -7, -8, -13, -16, 16,
-15, 32, -12, -19, 25, -25, 30, -6, -18,
-11, -14, -21, 27, -21, -14, -4, -1, 0, 17]
# adding style theme in scatter plot
plt.style.use('seaborn')
# creating scatter plot with both negative
# and positive axes
plt.scatter(x, y, c=y, cmap='plasma')
# adding vertical line in data co-ordinates
plt.axvline(0, c='black', ls='--')
# adding horizontal line in data co-ordinates
plt.axhline(0, c='black', ls='--')
# giving x label to the plot
plt.xlabel("X axis")
# giving y label to the plot
plt.ylabel("Y axis")
# giving title to the plot
plt.title("Scatter Plot with both negative and positive axes")
# visualizing the mapping from values to colors
plt.colorbar()
# visualizing the plot using plt.show() function
plt.show()
Output:
In the above output, we have added the grid-like style theme in our scatter plot so that the plot looks more interactive and easy to understand. To add the style theme add the plt.style.use() function before creating a scatter plots in the program code.
Similar Reads
How to Create a Scatterplot with a Regression Line in R?
A scatter plot uses dots to represent values for two different numeric variables. Scatter plots are used to observe relationships between variables. A linear regression is a straight line representation of relationship between an independent and dependent variable. In this article, we will discuss h
3 min read
How to Connect Scatterplot Points With Line in Matplotlib?
Prerequisite: Scatterplot using Seaborn in Python Scatterplot can be used with several semantic groupings which can help to understand well in a graph. They can plot two-dimensional graphics that can be enhanced by mapping up to three additional variables while using the semantics of hue, size, and
2 min read
How to create a Scatter Plot with several colors in Matplotlib?
Matplotlib is a plotting library for creating static, animated, and interactive visualizations in Python. Matplotlib can be used in Python scripts, the Python and IPython shell, web application servers, and various graphical user interface toolkits like Tkinter, awxPython, etc. In this article, we w
3 min read
How to Create a Scatterplot in R with Multiple Variables?
In this article, we will be looking at the way to create a scatter plot with multiple variables in the R programming language. Â Using Plot() And Points() Function In Base R: In this approach to create a scatter plot with multiple variables, the user needs to call the plot() function Plot() function:
3 min read
How does a positive slope differ from a negative slope?
Slope defines the steepness of a line and the way in which it tilts. The positive slope differs from the negative slope by the inclination of the line. In this article, we will explore how the positive slope differs from the negative slope. Table of ContentWhat is Slope?What is a Positive Slope?What
5 min read
How to Create a Swarm Plot with Matplotlib
Swarm plots, also known as beeswarm plots, are a type of categorical scatter plot used to visualize the distribution of data points in a dataset. Unlike traditional scatter plots, swarm plots arrange data points so that they do not overlap, providing a clear view of the distribution and density of d
5 min read
How to Do a Scatter Plot with Empty Circles in Python
Scatter plots are a powerful visualization tool that helps in identifying relationships between two quantitative variables. In Python, libraries like Matplotlib and Seaborn provide excellent functionalities for creating scatter plots with various customizations. One common customization is to create
3 min read
How to Color Scatterplot by a variable in Matplotlib?
In this article, we are going to see how to color scatterplot by variable in Matplotlib. Here we will use matplotlib.pyplot.scatter() methods matplotlib library is used to draw a scatter plot. Scatter plots are widely used to represent relations among variables and how change in one affects the othe
2 min read
How to Create an X-Y Scatter Plot in Excel?
Excel is powerful data visualization and data management tool which can be used to store, analyze, and create reports on large data. It can be used to visualize and compare data using a graph plot. In excel we can plot different kinds of graphs like line graphs, bar graphs, etc. to visualize or anal
2 min read
Power BI - How to Create a Scatter Chart?
A Scatter chart is a chart, representing data points, spread on a graph, to show the column values. It helps us analyze and summarise the data very efficiently. We can also add legends, bubble sizes, and animations to this chart. This graph is widely used because it can show better visualizations in
4 min read