📜  plt.scatter cmap (1)

📅  最后修改于: 2023-12-03 14:45:32.691000             🧑  作者: Mango

Pyplot.scatter using cmap

The matplotlib.pyplot.scatter function creates a scatter plot with a specified colormap (cmap). Colormap is a series of colors in a particular order that can be used to visualize data in a more meaningful way.

Syntax:
plt.scatter(x, y, c=None, cmap=None, ...,)
Parameters:
  • x: the horizontal positions of the points to plot
  • y: the vertical positions of the points to plot
  • c: the color of the points. It can be a single color format string like 'r', 'red', or a sequence of colors using a colormap (cmap)
  • cmap: a colormap object or name of a colormap. It can be any of the colormaps provided by Matplotlib such as 'viridis', 'plasma', 'inferno', etc.
Example:
import numpy as np
import matplotlib.pyplot as plt

# Generate random data
x = np.random.randn(100)
y = np.random.randn(100)

# Create a scatter plot and color each point based on the y-value using 'coolwarm' colormap
plt.scatter(x, y, c=y, cmap='coolwarm')

# Add a colorbar to show the mapping of colors to values
plt.colorbar()

# Set axis labels
plt.xlabel('X Label')
plt.ylabel('Y Label')

# Set plot title
plt.title('Scatter plot with colormap')

# Display the plot
plt.show()
Output:

scatter_with_cmap.png

The above code creates a scatter plot with a 'coolwarm' colormap, where the colors range from blue for low y-values to red for high y-values. The colorbar on the side shows the mapping of colors to values.

Conclusion:

plt.scatter with cmap is a powerful tool to create scatter plots that can convey a lot of information in a single plot. It allows the visualization of multi-dimensional data in a visually appealing way.