- 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
Show Matplotlib plots (and other GUI) in Ubuntu
Use the plot method of matplotlib and set the legend with different sets of colors.
Steps
Set the X-axis label using plt.xlabel() method.
Set the Y-axis label using plt.ylabel() method.
Plot the lines using plt.plot() method with [9, 5], [2, 5] and [4, 7, 8] array.
Initialize two variables; location = 0 for the best location and border_drawn_flag = True (True, if border to be drawn for legend. False, if border is not drawn).
Use plt.legend() method for the legend and set the location and border_drawn_flag accordingly to get the perfect legend in the diagram.
Show the figure using plt.show() method.
Example
import matplotlib.pyplot as plt plt.ylabel("Y-axis ") plt.xlabel("X-axis ") plt.plot([9, 5], [2, 5], [4, 7, 8]) location = 0 # For the best location legend_drawn_flag = True plt.legend(["blue", "orange"], loc=0, frameon=legend_drawn_flag) plt.show()
Output
- Related Articles
- Scroll backwards and forwards through Matplotlib plots
- How to make semilogx and semilogy plots in Matplotlib?
- How to reuse plots in Matplotlib?
- Fixing color in scatter plots in Matplotlib
- How to add annotations in Matplotlib Plots?
- Embedding small plots inside subplots in Matplotlib
- Logscale plots with zero values in Matplotlib
- Drawing lines between two plots in Matplotlib
- Tweaking axis labels and names orientation for 3D plots in Matplotlib
- Save the plots into a PDF in matplotlib
- How to save Matplotlib 3d rotating plots?
- Explain about the anatomy of Matplotlib plots in Python?
- Setting the spacing between grouped bar plots in Matplotlib
- How to show legend and label axes in 3D scatter plots in Python Plotly?
- How can multiple plots be plotted in same figure using matplotlib and Python?

Advertisements