Matplotlib Tutorial - Axis Titles
In this tutorial, we will learn about axis titles in Matplotlib.
Matplotlib axis titles
matplotlib.pyplot.title(label, fontdict=None, loc=None, **kwargs)
It is used to set the title of the current axis.
parameter
name | Data Types | describe |
---|---|---|
label |
str |
Label text |
fontdict |
dict |
Label text font dictionary, such as font family, color, weight, and size |
loc |
str |
The position of the title. It has three options, {'center', 'left', 'right'} . The default option iscenter |
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 4 * np.pi, 1000)
y = np.sin(x)
plt.figure(figsize=(4, 3))
plt.plot(x, y, "r")
plt.xlabel(
"Time (s)",
size=16,
)
plt.ylabel("Value", size=16)
plt.title(
"Title Example",
fontdict={"family": "serif", "color": "darkblue", "weight": "bold", "size": 18},
)
plt.grid(True)
plt.show()
plt.title(
"Title Example",
fontdict={"family": "serif", "color": "darkblue", "weight": "bold", "size": 18},
)
Multiple titles on axes
An axis can contain up to three titles left
, , center
and right
. The position of a particular title is specified by loc
the parameter.
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 4 * np.pi, 1000)
y = np.sin(x)
plt.figure(figsize=(8, 6))
plt.plot(x, y, "r")
plt.xlabel(
"Time (s)",
size=16,
)
plt.ylabel("Value", size=16)
plt.title(
"Left title",
fontdict={"family": "serif", "color": "darkblue", "weight": "bold", "size": 16},
loc="left",
)
plt.title(
"Center title",
fontdict={"family": "monospace", "color": "red", "weight": "bold", "size": 16},
loc="center",
)
plt.title(
"Right title",
fontdict={"family": "fantasy", "color": "black", "weight": "bold", "size": 16},
loc="right",
)
plt.grid(True)
plt.show()
Place axis titles inside the plot
You can also place a title inside the plot using positon=(m, n)
or equivalent options . Here, and are numbers between 0.0 and 1.0.x = m, y = n
m
n
Position (0, 0)
is the lower left corner of the graph, and position (1.0, 1.0)
is the upper right corner.
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 4 * np.pi, 1000)
y = np.sin(x)
plt.figure(figsize=(6, 4.5))
plt.plot(x, y, "r")
plt.xlabel("Time (s)", size=16)
plt.ylabel("Value", size=16)
plt.title(
"Title Example",
position=(0.5, 0.9),
fontdict={"family": "serif", "color": "darkblue", "weight": "bold", "size": 16},
)
plt.show()
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Enumerating a dictionary in Python
Publish Date:2025/05/05 Views:98 Category:Python
-
The function in Python enumerate() returns an object of enumeration type and adds a counter variable to iterate over a list or other type of collection. It makes looping over such objects easier. When we pass an enumeration object to list()
Changing dictionary values in Python
Publish Date:2025/05/05 Views:108 Category:Python
-
This tutorial will discuss various ways to change the value of a particular key in Python dictionary. We can do this by using the following methods. dict.update() method for cycle. Dictionary Unpacking dict.update() How to change dictionary
Finding the maximum value in a Python dictionary
Publish Date:2025/05/05 Views:60 Category:Python
-
This tutorial explains how to get a key with the maximum value in Python. Since the method has changed from the previous Python versions, it also lists some sample codes to clarify the concepts. Use operator.itemgetter() the method to get t
How to read input from stdin in Python
Publish Date:2025/05/05 Views:124 Category:Python
-
This tutorial discussed stdin the methods of reading input from in Python. You can read directly from the console or from a file name specified in the console. In Python, fileinput.input() use stdin fileinput We can use the read module in P
Maximum integer in Python
Publish Date:2025/05/05 Views:55 Category:Python
-
This tutorial will discuss the maximum integer value in different versions of Python and how we can get it. In Python 2, integers and long integers are different data types. The maximum value of an integer is 2 31 -1. If the value exceeds t
Get a list of time zones using Python
Publish Date:2025/05/05 Views:107 Category:Python
-
When developing real-world applications, software developers must ensure that the application can support users from both their own country and other parts of the world. Since most countries have different time zones and many people around
Convert NumPy array to list in Python
Publish Date:2025/05/05 Views:101 Category:Python
-
Lists and arrays are the two most basic and commonly used collection objects in Python. They are both mutable and are used to store a collection of elements under a common name and each element has a specific location that can be used to ac
Appending 2D Arrays in Python
Publish Date:2025/05/05 Views:64 Category:Python
-
In Python, we can have ND arrays. We can use NumPy module to process arrays in Python. This tutorial demonstrated the different methods you can use to append values to a two-dimensional array in Python. Use append() the function to ap
Sliding average of NumPy arrays in Python
Publish Date:2025/05/05 Views:190 Category:Python
-
The sliding average is often used to study time series data by calculating the average of data at a specific time interval. It is used to eliminate some short-term fluctuations and study data trends. When studying stock price trends, the si