

You can achieve the same scatter plot as the one you obtained in the section above with the following call to plt.plot(), using the same data: Matplotlib’s plt.plot() is a general-purpose plotting function that will allow you to create various different line or marker plots.

You can also produce the scatter plot shown above using another function within matplotlib.pyplot. You can then carry out further analysis, whether it’s using linear regression or other techniques. When using scatter plots in this way, close inspection can help you explore the relationship between variables. However, the drink that costs $4.02 is an outlier, which may show that it’s a particularly popular product. This plot shows that, in general, the more expensive a drink is, the fewer items are sold. In this tutorial, all the examples will be in the form of scripts and will include the call to plt.show(). When you’re using an interactive environment, such as a console or a Jupyter Notebook, you don’t need to call plt.show(). As you’re using a Python script, you also need to explicitly display the figure by using plt.show(). You then create lists with the price and average sales per day for each of the six orange drinks sold.įinally, you create the scatter plot by using plt.scatter() with the two variables you wish to compare as input arguments. This alias is generally used by convention to shorten the module and submodule names. In this Python script, you import the pyplot submodule from Matplotlib using the alias plt. Import matplotlib.pyplot as plt price = sales_per_day = plt. You don’t need to be familiar with Matplotlib to follow this tutorial, but if you’d like to learn more about the module, then check out Python Plotting With Matplotlib (Guide). To get the most out of this tutorial, you should be familiar with the fundamentals of Python programming and the basics of NumPy and its ndarray object.
Create a scatter plot matplotlib how to#
Matplotlib provides a very versatile tool called plt.scatter() that allows you to create both basic and more complex scatter plots.īelow, you’ll walk through several examples that will show you how to use the function effectively. One of the most popular modules is Matplotlib and its submodule pyplot, often referred to using the alias plt. Python has several third-party modules you can use for data visualization. Watch it together with the written tutorial to deepen your understanding: Using plt.scatter() to Visualize Data in PythonĪn important part of working with data is being able to visualize it. Here we discuss an introduction to Matplotlib Scatter, how to create plots with example for better understanding.Watch Now This tutorial has a related video course created by the Real Python team. It helps us in understanding any relation between the variables and also in figuring out outliers if any. Scatter plots become very handy when we are trying to understand the data intuitively. While the linear relation continues for the larger values, there are also some scattered values or outliers. Plt.ylabel('y variable')Įxplanation: We can clearly see in our output that there is some linear relationship between the 2 variables initially. Plt.title('Scatter plot showing correlation') Here we will define 2 variables, such that we get some sort of linear relation between themĪ = ī = Ĭolors = (0,0,0) Example to Implement Matplotlib Scatterįinally, let us take an example where we have a correlation between the variables: Example #1 Z = fig.add_subplot(1, 1, 1, facecolor='#E6E6E6')Įxplanation: So here we have created scatter plot for different categories and labeled them. This is how our input and output will look like in python: Z = fig.add_subplot(1, 1, 1, facecolor='#E6E6E6') įor data, color, group in zip(data, colors, groups): Next let us create our data for Scatter plotĪ1 = (1 + 0.6 * np.random.rand(A), np.random.rand(A))Ī2 = (2+0.3 * np.random.rand(A), 0.5*np.random.rand(A))Ĭolors = (“red”, “green”) Step #2: Next, let us take 2 different categories of data and visualize them using scatter plots.

As we mentioned in the introduction of scatter plots, they help us in understanding the correlation between the variables, and since our input values are random, we can clearly see there is no correlation. Explanation: For our plot, we have taken random values for variables, the same is justified in the output.
