Responsive Navbar with Google Search
☰ Menu
Home
Python
LaTeX
GNUPlot
Arduino
Feedback
Contact Us
Plotting in Python: Colormaps in Plots
Plotting in Python
Basic Plot and formatting
Subplot, Multiplot
Colormaps in Plots
Bar Plot
Pie Chart
Histogram
Quiver plot to show Vector Fields
Stream plot to Visualize Flow Data
Polar, Parametric Plot
Contour Plot
3D Plot
Density Plot
Animated Plot
Miscellaneous
Colormaps in Plots: Program 1
Lineplot with Colormap
import matplotlib.pyplot as plt import numpy as np import matplotlib.cm as cm x = np.linspace(0, 10, 100) y = np.sin(x) colors = cm.viridis(np.linspace(0, 1, 10)) # Using the 'viridis' colormap plt.figure(figsize=(8, 6)) for i, color in enumerate(colors): plt.plot(x, y + i*0.1, color=color, label=f'Line {i+1}') plt.title('Line Plot with Colormap') plt.xlabel('x') plt.ylabel('y') plt.legend() plt.grid(True) plt.savefig('colormap_1.png') plt.show()
Run Code
Output 1
Colormaps in Plots: Program 2
Scatter Plot with Colormap
import matplotlib.pyplot as plt import numpy as np import matplotlib.cm as cm x = np.random.rand(100) y = np.random.rand(100) colors = np.sqrt(x**2 + y**2) # Color by the distance from origin plt.figure(figsize=(8, 6)) scatter = plt.scatter(x, y, c=colors, cmap='plasma', edgecolor='k') plt.colorbar(scatter, label='Color intensity') plt.title('Scatter Plot with Colormap') plt.xlabel('x') plt.ylabel('y') plt.grid(True) plt.savefig('colormap_2.png') plt.show()
Run Code
Output 2