Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Scatter Plot in Python

Scatter Plot in Python

Scatter Plot in Python using Matplotlib

A scatter plot is a type of plot that shows the relationship between two variables. Each point on the plot represents one observation in the dataset, with the x-coordinate and y-coordinate corresponding to values of the two variables.

To create a scatter plot in Python, you can use the popular matplotlib library. Here is how you can create one.


1. Basic Scatter Plot

import matplotlib.pyplot as plt# Example datax = [1, 2, 3, 4, 5]y = [2, 3, 5, 7, 11]# Create a scatter plotplt.scatter(x, y)# Add title and labelsplt.title('Basic Scatter Plot')plt.xlabel('X values')plt.ylabel('Y values')# Show the plotplt.show()

Output: A scatter plot with x-values on the horizontal axis and y-values on the vertical axis.


2. Scatter Plot with Customizations

You can customize your scatter plot by changing the colors, sizes, and shapes of the points.

import matplotlib.pyplot as plt# Example datax = [1, 2, 3, 4, 5]y = [2, 3, 5, 7, 11]# Customizing the scatter plotplt.scatter(x, y, color='red', s=100, marker='o')  # 'o' for circle markers# Add title and labelsplt.title('Customized Scatter Plot')plt.xlabel('X values')plt.ylabel('Y values')# Show the plotplt.show()
  • color='red': Sets the color of the points.

  • s=100: Sets the size of the points.

  • marker='o': Sets the shape of the points (circle in this case).


3. Scatter Plot with Different Colors and Sizes

You can also vary the color and size of the points based on additional variables.

import matplotlib.pyplot as plt# Example datax = [1, 2, 3, 4, 5]y = [2, 3, 5, 7, 11]sizes = [20, 50, 100, 200, 500]  # Size of each pointcolors = [1, 2, 3, 4, 5]  # Color intensity of each point# Create a scatter plot with varying size and colorplt.scatter(x, y, s=sizes, c=colors, cmap='viridis', alpha=0.6)# Add title and labelsplt.title('Scatter Plot with Varying Size and Color')plt.xlabel('X values')plt.ylabel('Y values')# Show the plotplt.show()
  • s=sizes: Varies the size of each point.

  • c=colors: Varies the color of each point.

  • cmap='viridis': Specifies the color map (you can use other color maps such as plasma, inferno, etc.).

  • alpha=0.6: Sets the transparency level of the points (0 is fully transparent, 1 is fully opaque).


4. Scatter Plot with Legends and Gridlines

You can also add legends and gridlines to your scatter plot for better readability.

import matplotlib.pyplot as plt# Example datax = [1, 2, 3, 4, 5]y = [2, 3, 5, 7, 11]# Create the scatter plotplt.scatter(x, y, label='Data Points', color='blue')# Add title, labels, and legendplt.title('Scatter Plot with Legends')plt.xlabel('X values')plt.ylabel('Y values')plt.legend()# Add gridlinesplt.grid(True)# Show the plotplt.show()
  • plt.legend(): Displays the legend for the data points.

  • plt.grid(True): Enables gridlines.


5. Scatter Plot with Annotations

You can annotate points in a scatter plot with labels or other markers.

import matplotlib.pyplot as plt# Example datax = [1, 2, 3, 4, 5]y = [2, 3, 5, 7, 11]# Create the scatter plotplt.scatter(x, y, color='green')# Annotate the pointsfor i, txt in enumerate(y):    plt.annotate(f'Point {i+1}', (x[i], y[i]))# Add title and labelsplt.title('Scatter Plot with Annotations')plt.xlabel('X values')plt.ylabel('Y values')# Show the plotplt.show()
  • plt.annotate(): Adds annotations to each point in the scatter plot.


6. 3D Scatter Plot

If you want to create a 3D scatter plot, you can use Axes3D from mpl_toolkits.mplot3d.

import matplotlib.pyplot as pltfrom mpl_toolkits.mplot3d import Axes3D# Example datax = [1, 2, 3, 4, 5]y = [2, 3, 5, 7, 11]z = [1, 4, 9, 16, 25]# Create a figure and 3D axisfig = plt.figure()ax = fig.add_subplot(111, projection='3d')# Create a 3D scatter plotax.scatter(x, y, z, color='purple')# Add title and labelsax.set_title('3D Scatter Plot')ax.set_xlabel('X values')ax.set_ylabel('Y values')ax.set_zlabel('Z values')# Show the plotplt.show()
  • This creates a 3D scatter plot with x, y, and z coordinates.


Conclusion

  • plt.scatter() is the key function for creating scatter plots in matplotlib.

  • You can customize scatter plots by modifying the appearance of points (e.g., color, size, shape).

  • Scatter plots are useful for visualizing relationships between two variables, identifying trends, and spotting outliers.

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql