- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Matplotlib Plot Lines with Colors through Colormap
To plot lines with colors through colormap, we can take the following steps−
- Create x and y data points using numpy
- Plot x and y data points using plot() method.
- Count n finds, number of color lines has to be plotted.
- Iterate in a range (n) and plot the lines.
- Limit the x ticks range.
- Use show() method to display the figure.
Example
import numpy as np import matplotlib.pylab as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(0, 2 * np.pi, 64) y = np.exp(x) plt.plot(x, y) n = 20 colors = plt.cm.rainbow(np.linspace(0, 1, n)) for i in range(n): plt.plot(x, i * y, color=colors[i]) plt.xlim(4, 6) plt.show()
Output
- Related Articles
- Plot a histogram with colors taken from colormap in Matplotlib
- How to plot data into imshow() with custom colormap in Matplotlib?
- Plot different colors for different categorical levels using matplotlib
- Plot horizontal and vertical lines passing through a point that is an intersection point of two lines in Matplotlib
- How to plot overlapping lines in Matplotlib?
- Plot a polar color wheel based on a colormap using Python/Matplotlib
- How to remove lines in a Matplotlib plot?
- 3D scatterplots in Python Matplotlib with hue colormap and legend
- Extract Matplotlib colormap in hex-format
- Draw axis lines or the origin for Matplotlib contour plot.
- How to draw axis lines inside a plot in Matplotlib?
- How to plot two dotted lines and set marker using Matplotlib?
- How to plot the lines first and points last in Matplotlib?
- Best way to plot an angle between two lines in Matplotlib
- How to name different lines in the same plot of Matplotlib?

Advertisements