- 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 set same color for markers and lines in a Matplotlib plot loop?
To set the same color for markers and lines in a matplotlib, we can take the following Steps −
Initialize m, n and x data points using numpy.
Create a new figure or activate an existing figure using figure() method.
Clear the figure using clf() method.
Add a subplot to the current figure using subplot() method.
Get a marker from a iterable marker type.
Iterate a range from 1 to n.
Plot the lines and markers in the loop using plot() method with the same marker and colors for a line.
To display the figure, use show() method.
Example
import numpy as np import itertools from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True m = 5 n = 5 x = np.zeros(shape=(m, n)) plt.figure() plt.clf() plt.subplot(111) marker = itertools.cycle(('o', 'v', '^', '<', '>', 's', '8', 'p')) for i in range(1, n): x = np.dot(i, [1, 1.1, 1.2, 1.3]) y = x ** 2 plt.plot(x, y, linestyle='', markeredgecolor='none', marker=next(marker), alpha=1) plt.plot(x, y, linestyle='-') plt.show()
Output
- Related Articles
- How to set a line color to orange, and specify line markers in Matplotlib?
- How to make markers on lines smaller in Matplotlib?
- How to create custom markers on a plot in Matplotlib
- How to plot two dotted lines and set marker using Matplotlib?
- How to use different markers for different points in a Pylab scatter plot(Matplotlib)?
- How to change the color and add grid lines to a Python Matplotlib surface plot?
- How to name different lines in the same plot of Matplotlib?
- How to remove lines in a Matplotlib plot?
- How to plot overlapping lines in Matplotlib?
- How to plot a smooth 2D color plot for z = f(x, y) in Matplotlib?
- How to plot arbitrary markers on a Pandas data series using Matplotlib?
- How to plot a gradient color line in matplotlib?
- How to plot contourf and log color scale in Matplotlib?
- How to draw axis lines inside a plot in Matplotlib?
- How to change the color of the axis, ticks and labels for a plot in matplotlib?

Advertisements