- 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
How to plot a single line in Matplotlib that continuously changes color?
To plot a single line that continuously changes color, we can take the following steps−
- Set the figure size and adjust the padding between and around the subplots.
- Create random x and y data points using numpy.
- Create a figure and a set of subplots.
- Iterate the index in the range of 1 to 100.
- Plot x and y data points with random color in a loop.
- To display the figure, use show() method.
Example
import matplotlib.pyplot as plt import numpy as np import random plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(1, 10, 100) y = np.sin(x) fig, ax = plt.subplots() for i in range(0, 100, 5): r = random.random() b = random.random() g = random.random() color = (r, g, b) ax.plot(x[i:i+5+1], y[i:i+5+1], c=color, lw=7) plt.show()
Output
- Related Articles
- How to plot a gradient color line in matplotlib?
- How to change the plot line color from blue to black in Matplotlib?
- How to animate a line plot in Matplotlib?
- How to plot a 3D continuous line in Matplotlib?
- How to plot a smooth line with matplotlib?
- How to change the color of a plot frame in Matplotlib?
- How to plot one single data point in Matplotlib?
- How to plot contourf and log color scale in Matplotlib?
- How to set a line color to orange, and specify line markers in Matplotlib?
- How to plot a smooth 2D color plot for z = f(x, y) in Matplotlib?
- How to customize the color and colormaps of a Plot in Matplotlib
- How can I plot a single point in Matplotlib Python?
- How to change the face color of a plot using Matplotlib?
- How to plot a line graph from histogram data in Matplotlib?
- How to plot a dashed line on a Seaborn lineplot in Matplotlib?

Advertisements