- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Plot different colors for different categorical levels using matplotlib
We can plot a diagram where a number of students will be plotted on the X-axis and the marks obtained by them will be plotted on the Y-axis. Also, we can set the color for different marks obtained by the students.
Steps
Make a list of the number of students.
Make a list of marks that have been obtained by the students.
To represent the color of each scattered point, we can have a list of colors.
Using Panda, we can have a list representing the axes of the data frame.
Create fig and ax variables using subplots method, where default nrows and ncols are 1.
Set the “Students count” label using the plt.xlabel() method.
Set the “Obtained marks” label using the plt.ylabel() method.
To create a scatter point use a data frame, i.e., created in step 4, points are students_count, marks and color.
To show the figure, use the plt.show() method.
Example
from matplotlib import pyplot as plt import pandas as pd no_of_students = [1, 2, 3, 5, 7, 8, 9, 10, 30, 50] marks_obtained_by_student = [100, 95, 91, 90, 89, 76, 55, 10, 3, 19] color_coding = ['red', 'blue', 'yellow', 'green', 'red', 'blue', 'yellow', 'green', 'yellow', 'green'] df = pd.DataFrame(dict(students_count=no_of_students, marks=marks_obtained_by_student, color=color_coding)) fig, ax = plt.subplots() plt.xlabel('Students count') plt.ylabel('Obtained marks') ax.scatter(df['students_count'], df['marks'], c=df['color']) plt.show()
Output
- Related Articles
- How to specify different colors for different bars in a Python matplotlib histogram?
- How to show an image in Matplotlib in different colors with different channels?
- How to use different markers for different points in a Pylab scatter plot(Matplotlib)?
- How to create density plot for categories filled with different colors in R?
- Setting different error bar colors in barplot in Matplotlib
- Why different flowers bear different colors?
- How to plot with different scales in Matplotlib?
- How to plot histograms of different colors of an image in OpenCV Python?
- How to create different Y-axis for group levels using ggplot2 in R?
- Matplotlib Plot Lines with Colors through Colormap
- How to plot categorical variables in Matplotlib?
- How to name different lines in the same plot of Matplotlib?
- What is biological magnification? Will the levels of this magnification be different at different levels of the ecosystem?
- How to fill histogram bars using ggplot2 in R with different colors?
- What are the different levels of Parallelism?
