A scatter plot is used to display values for two variables in a set of data. Each data point is represented by a dot, where the position on the horizontal axis corresponds to one variable and the position on the vertical axis corresponds to another variable. Scatter plots are commonly used to visualize the relationship or correlation between these two variables.
Creating a Basic Scatter Plot
You can create a scatter plot using the plt.scatter() function. Here’s a basic example:
In this example, s=100 changes the size of the points, c="red" changes their color, and marker="^" changes the marker shape to a triangle. This allows for a high degree of customization, making your plots more informative and visually appealing.
Adding Transparency
To make your scatter plot points semi-transparent, you can use the alpha parameter. This is useful when dealing with overlapping points:
Python
plt.scatter(x, y, alpha=0.5)
plt.title("Scatter Plot with Transparency")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
The alpha parameter controls the opacity of the points, where alpha=1 is fully opaque and alpha=0 is fully transparent.
Using Color Maps
You can also use color maps to add another dimension to your scatter plot, such as representing the magnitude of a third variable:
Python
import numpy as np
colors = np.random.rand(len(x))
plt.scatter(x, y, c=colors, cmap="viridis", s=100)
plt.colorbar() # Show color scale
plt.title("Scatter Plot with Color Map")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
This example assigns a random color to each point using a colormap (in this case, "viridis"), and the colorbar() function adds a color scale to the plot.
Conclusion
Scatter plots are powerful tools for visualizing the relationships between two variables. By customizing the size, color, and transparency of the points, and by using color maps, you can enhance your scatter plots to convey more information and provide clearer insights into your data.
Matplotlib Scatter
Scatter plot ka use do variables ke values ko ek dataset me display karne ke liye hota hai. Har data point ek dot se represent hota hai, jisme horizontal axis ka position ek variable ko show karta hai aur vertical axis ka position doosre variable ko. Scatter plots commonly use hote hain relationship ya correlation ko visualize karne ke liye do variables ke beech.
Creating a Basic Scatter Plot
Aap plt.scatter() function ka use karke scatter plot bana sakte hain. Yaha ek basic example hai:
Python
import matplotlib.pyplot as plt
x = [5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6]
y = [99, 86, 87, 88, 100, 86, 103, 87, 94, 78, 77, 85, 86]
# Scatter plot create karna
plt.scatter(x, y)
plt.title("Basic Scatter Plot") # Plot ka title set karna
plt.xlabel("X-axis") # X-axis ka label set karna
plt.ylabel("Y-axis") # Y-axis ka label set karna
plt.show()
Is code se ek simple scatter plot banta hai jisme x aur y coordinates points ke liye use hote hain. Plot me titles aur labels bhi include hote hain taaki clarity rahe.
Customizing Scatter Plots
Matplotlib aapko scatter plots ko customize karne ka option deta hai, jaise size, color, aur shape change karna. Yaha kaise kar sakte hain:
Python
# Customized scatter plot create karna
plt.scatter(x, y, s=100, c="red", marker="^")
plt.title("Customized Scatter Plot") # Plot ka title set karna
plt.xlabel("X-axis") # X-axis ka label set karna
plt.ylabel("Y-axis") # Y-axis ka label set karna
plt.show()
Is example me, s=100 points ka size change karta hai, c="red" points ka color set karta hai, aur marker="^" marker shape ko triangle bana deta hai. Isse aapke plots aur zyada informative aur visually appealing ban jate hain.
Adding Transparency
Agar aap apne scatter plot points ko thoda transparent banana chahte hain, to alpha parameter ka use kar sakte hain. Yeh overlapping points ke sath useful hota hai:
Python
# Scatter plot me transparency add karna
plt.scatter(x, y, alpha=0.5)
plt.title("Scatter Plot with Transparency") # Plot ka title set karna
plt.xlabel("X-axis") # X-axis ka label set karna
plt.ylabel("Y-axis") # Y-axis ka label set karna
plt.show()
alpha parameter points ki opacity control karta hai, jisme alpha=1 fully opaque hota hai aur alpha=0 fully transparent.
Using Color Maps
Aap color maps ka use karke scatter plot me ek aur dimension add kar sakte hain, jaise teesre variable ke magnitude ko represent karna:
Python
import numpy as np
colors = np.random.rand(len(x))
# Scatter plot with color map create karna
plt.scatter(x, y, c=colors, cmap="viridis", s=100)
plt.colorbar() # Color scale dikhana
plt.title("Scatter Plot with Color Map") # Plot ka title set karna
plt.xlabel("X-axis") # X-axis ka label set karna
plt.ylabel("Y-axis") # Y-axis ka label set karna
plt.show()
Is example me, ek random color har point ko assign hota hai colormap ke sath (is case me "viridis"), aur colorbar() function plot me color scale add karta hai.
Conclusion
Scatter plots powerful tools hote hain do variables ke beech ke relationships ko visualize karne ke liye. Size, color, transparency customize karke aur color maps ka use karke aap apne scatter plots ko zyada informative bana sakte hain aur apne data ke insights ko aur clear kar sakte hain.
Import Links
Here are some useful import links for further reading: