View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
In this article, the task is to use multiple columns in a Matplotlib legend in Python. Before starting the discussion on “Use multiple columns in a Matplotlib legend”, firstly we should know briefly about matplotlib, pyplot and legend.
- Matplotlib: The “matplotlib” library basically used to create so many graphical things such like high quality graphs, charts, figures and many things. The library is extensive and capable of changing very small to the smallest details of a figure. This library was introduced by John Hunter and his team in 2002.
- Pyplot: In Python, “pyplot” is a plotting library used for 2-dimensional graphics. It is used in python scripts, shell, web application servers and other GUI toolkits.
- Legend: A legend is an area that describes the elements of a graph. In the matplotlib, there is a function called legend[] which is used to place a legend on the mentioned axis.
Note: Before declaring matplotlib and pyplot, it is better to declare numpy library also.
Basically, we can import pyplot with matplotlib as we generally import other libraries in python, such like
import matplotlib.pyplot as plt
or
from matplotlib import pyplot as plt
Use multiple column in a Matplotlib Legend
In many cases, the main thing we may have to encounter that as the legend number gets increased, it may consume a lot of vertical spaces which may create problem for visualization of the graph. So, in this situation, we need to organize legend labels into multiple columns. For this, it will be easy to place non-disruptively all the legends.
Using the ncol argument inside plt.legend[] in below defined manner to specify the number of columns which the legend should have.
plt.legend[ncol=k]
Here, k is the number of columns the legend should have in the graph.
Example:
Python3
import
matplotlib.pyplot as plt
plt.plot[[
0
,
3
], [
0
,
2.0
], label
=
'Label 1'
]
plt.plot[[
0
,
3
], [
0
,
2.1
], label
=
'Label 2'
]
plt.plot[[
0
,
3
], [
0
,
2.2
], label
=
'Label 3'
]
plt.plot[[
0
,
3
], [
0
,
2.3
], label
=
'Label 4'
]
plt.plot[[
0
,
3
], [
0
,
2.4
], label
=
'Label 5'
]
plt.legend[ncol
=
3
]
plt.show[]
Output:
OUTPUT
How do i print multiple columns in python?
View DiscussionImprove ArticleSave ArticleReadDiscussView DiscussionImprove ArticleSave ArticlePython is a great language for doing data analysis, primarily because of the fantastic ecosystem of ...
Hướng dẫn dùng df.rename python
DataFrame.rename[mapper=None, *, index=None, columns=None, axis=None, copy=True, inplace=False, level=None, errors=ignore][source]¶Alter axes labels.Function / dict values must be unique [1-to-1]. ...
Hướng dẫn dùng iterrow python
Python là một ngôn ngữ được sử dụng vô cùng rộng rãi trong phân tích dữ liệu. Nhờ vào hệ sinh thái đa dạng các packages chuyên dụng cho dữ liệu, Python giúp ...
How to print rows and columns in python
Hello, readers! In this article, we will be focusing on different ways to print column names in Python.So, let us get started!First, where do you find columns in Python?We often come across questions ...
Hướng dẫn dùng python iloc python
Tiếp tục phần 2 của series Pandas DataFrame nào. Lets go!!!Truy cập và sử dụng Data trong DataFrameỞ phần trước, các bạn đã biết được cách lấy dữ liệu một ...
Hướng dẫn mask trong python
Trong bài trước ta đã học được các thao tác tính toán trên mảng từ cơ bản đến nâng cao với NumPy. Trong việc tính toán và xử lý dữ liệu, thì lọc dữ ...
Hướng dẫn dùng df tolist python
Trụ sở chính:Nội dung chínhĐọc file csv sử dụng thư viện pandasThao tác với dataframe trong pandasXem thông tin của dataframeTruy xuất dữ liệu trên dataframeThêm, ...