{"id":1004,"date":"2023-12-15T05:00:00","date_gmt":"2023-12-15T05:00:00","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=1004"},"modified":"2023-12-17T07:23:36","modified_gmt":"2023-12-17T07:23:36","slug":"pandas-scatter-plot","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/pandas-scatter-plot\/","title":{"rendered":"DataFrame.plot.scatter() | How to Scatter Plot in Pandas?"},"content":{"rendered":"\n<p>Pandas library in Python provides a lot of tools required for our analysis. In data analysis and visualization, a scatter plot is a powerful tool to understand the relationship between two numbers or variables. A scatter plot allows us to represent each data point and identify patterns, correlations, and outliers in the data. In this article, we will explore how to create a scatter plot using the popular Python library, Pandas.\u00a0<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is the plot.scatter() in Pandas?<\/strong><\/h2>\n\n\n\n<p>A scatter plot is a method to display data that shows the relationship between two numerical variables. Each dot on the scatter plot can represent a single data point, with the x-axis representing one variable and the y-axis representing the other variable. By plotting the data points on a graph, we can visually analyse the relationship between the two variables.<\/p>\n\n\n\n<p><strong>The plot.scatter() method in Pandas allows us to make scatter for our data visualisation needs.<\/strong><\/p>\n\n\n\n<p>Following is the syntax of the plot.scatter():<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>DataFrame.plot.scatter(x, y, s=None, c=None, **kwargs)<\/code><\/pre>\n\n\n\n<p><strong>Parameters:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>x<\/strong>: The column name or position to be used as the horizontal coordinates for each point.<\/li>\n\n\n\n<li><strong>y<\/strong>: The column name or position to be used as the vertical coordinates for each point.<\/li>\n\n\n\n<li><strong>s<\/strong>: The size of each point. It can be a single scalar, a string with the name of the column to be used for marker size, or a sequence of scalars.<\/li>\n\n\n\n<li><strong>c<\/strong>: The colour of each point. It can be a single colour string, a sequence of colour strings, or a column name or position whose values will be used to colour the marker points according to a colourmap.<\/li>\n\n\n\n<li>**kwargs: Additional keyword arguments to pass on to the underlying plotting function.<br><\/li>\n<\/ul>\n\n\n\n<p>Now, let us look at how to use the method to draw scatter plots of our data.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Creating a Scatter Plot from a DataFrame<\/strong><\/h3>\n\n\n\n<p>We can simply use a DataFrame and then apply plot.scatter. To create a scatter plot from the DataFrame, we can use the plot.scatter() function and specify the &#8216;x&#8217; and &#8216;y&#8217; columns.<\/p>\n\n\n\n<p>Let us look at an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\n\n# Create DataFrame\ndf = pd.DataFrame({'A': [1, 2, 3, 4, 5], 'B': [2, 4, 6, 8, 10]})\n\n# Display the DataFrame\nprint('Original DataFrame:\\n', df)\n\n# Create scatter plot\ndf.plot.scatter(x='A', y='B')<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Original DataFrame:\n    A   B\n0  1   2\n1  2   4\n2  3   6\n3  4   8\n4  5  10\n<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/D3n8Cmwate0ZpP_QbBf1DnH3gIRyXP6tGJfr8rnVv1rVWnPpQi6-JkJ7msR0tjz5zGWosS8Y18XHIfOxBo0S51Yy7SGBBkKIVat55OhlEcqy5zOTUYeg3LzSD8I8AreWKA7Gi4cOp5Hxiihos00N-PI\" alt=\"Creating a Scatter Plot from a DataFrame\"\/><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>Customising our Scatter Plot<\/strong><\/h3>\n\n\n\n<p>We can also customise our scatter to meet our needs. Pandas provide various options to customize the appearance of the scatter plot.<strong> We can modify the size, colour, and other properties of the data points to improve the visualization.<\/strong><\/p>\n\n\n\n<p>Let us first see how to modify the size of the dots. <strong>To provide customised sizes for each dot, we can pass a sequence of scalars with the sizes. <\/strong>A sequence of scalars is similar to a list of integers.<\/p>\n\n\n\n<p>See the Python code below:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\n\n# Create DataFrame\ndf = pd.DataFrame({'A': [1, 2, 3, 4, 5], 'B': [2, 4, 6, 8, 10]})\n\n# Display the DataFrame\nprint('Original DataFrame:\\n', df)\n\n# Create scatter plot with customized size\ndf.plot.scatter(x='A', y='B', s=[50, 100, 150, 200, 250])<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Original DataFrame:\n    A   B\n0  1   2\n1  2   4\n2  3   6\n3  4   8\n4  5  10\n<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/io18T49z4iLw2reCmrTQ1H1pZNgkZazKbYx9K9MzRiQPTUbiROB_pKTYCqfZdNeoKm9mcHWpKhBIbYsP2peSGviqo1AUJyyOQDFnmR9GE1B_AwZJUjYp31Armc1kpsdQHhwQywgpPFmkgX8sLGI2ygw\" alt=\"Customising our Scatter Plot\"\/><\/figure>\n<\/div>\n\n\n<p>In the above example, we assigned different sizes to every dot based on <strong>a sequence of scalars<\/strong> [50, 100, 150, 200, 250]. The first dot will have a size of 50, the second dot will have a size of 100, and so on.<\/p>\n\n\n\n<p>Now, let us learn how to change the colour of the dots:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\n\n# Create DataFrame\ndf = pd.DataFrame({'A': [1, 2, 3, 4, 5], 'B': [2, 4, 6, 8, 10]})\n\n# Display the DataFrame\nprint('Original DataFrame:\\n', df)\n\n# Create scatter plot with customized color\ndf.plot.scatter(x='A', y='B', c='red')<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Original DataFrame:\n    A   B\n0  1   2\n1  2   4\n2  3   6\n3  4   8\n4  5  10\n<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/VQ8mTXZUtLhgHmrYkTJqy4KnqBdIvXO8HD30R2F6CfYRFJgcA4zi1OTAWJ3_4Nz-O9_ttG07FPrYZk39gyLMbL8z8M-XjTC7tNdZ3dERY9NQpbswruMewmdQns5-9RP9IcvtuwOfcJ0iV74kU1zSb3I\" alt=\"Changing color of dotes\"\/><\/figure>\n<\/div>\n\n\n<p>We can define various colours in the \u2018c\u2019 parameter of the plot.scatter method. In the example, we defined our scatter points to be red.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Using Matplotlib to Create Scatter Plots<\/strong><\/h3>\n\n\n\n<p>We can also use the traditional library for plotting graphs, matplotlib to plot scatter plots.<\/p>\n\n\n\n<p><strong>Matplotlib is a powerful library for data visualization and provides extensive customization options.<\/strong><\/p>\n\n\n\n<p>Let us look at an example of how to draw a scatter plot using matplotlib:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import matplotlib.pyplot as plt\nimport pandas as pd\n\n# Create DataFrame\ndf = pd.DataFrame({'A': [1, 2, 3, 4, 5], 'B': [2, 4, 6, 8, 10]})\n\n# Display the DataFrame\nprint('Original DataFrame:\\n', df)\n\n# Create scatter plot using Matplotlib\nplt.scatter(df['A'], df['B'])\nplt.xlabel('A')\nplt.ylabel('B')\nplt.title('Scatter Plot')\nplt.show()<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Original DataFrame:\n    A   B\n0  1   2\n1  2   4\n2  3   6\n3  4   8\n4  5  10\n<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh7-us.googleusercontent.com\/Rq6GXSeZP_uthPNmg0wugPSukBybY7nqRTcTcIS-c1fZeRPvyJaz6vCY_BSBCFiLntYiBr2jFIvxQYn1qt1jN_HAOyKadMjk2yWV3WLTkVRBSH2hLNls15h5JUlDbdQXrhEG9D4hMLAVDRsMm6uEYdE\" alt=\"Create Scatter Plots using Matplotlib\"\/><\/figure>\n<\/div>\n\n\n<p>You can learn about other <a href=\"https:\/\/favtutor.com\/blogs\/top-python-data-science-library\" data-type=\"link\" data-id=\"https:\/\/favtutor.com\/blogs\/top-python-data-science-library\">Python libraries for Data Science<\/a> as well.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In this article, we learned how to create scatter plots using the Pandas library in Python. Scatter plots are a powerful tool for seeing the relationship between two numerical variables in a DataFrame. We learned about the syntax and parameters of the plot.scatter() function and saw examples of creating basic scatter plots as well as customizing the size and colour of the dots. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to make scatter plots in pandas using plot.scatter() for DataFrames and how to customize them with examples.<\/p>\n","protected":false},"author":10,"featured_media":1007,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jnews-multi-image_gallery":[],"jnews_single_post":null,"jnews_primary_category":{"id":"","hide":""},"footnotes":""},"categories":[35],"tags":[37],"class_list":["post-1004","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-science","tag-pandas"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1004","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/comments?post=1004"}],"version-history":[{"count":3,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1004\/revisions"}],"predecessor-version":[{"id":1070,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1004\/revisions\/1070"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/1007"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=1004"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=1004"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=1004"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}