Plots in Matplotlib are used to represent the visualization of a given data. These plots require the need for labels (x and y-axis) as well as the title of the plot. Every plot has the x and y-axis. These axes represent the values of the data set plotted against the other.
Now, these titles and labels can also be given different font properties to enhance the visuals of the plot.
In Matplotlib, we use the fontdict
parameter of the pyplot.xlabel()
, pyplot.ylabel()
and pyplot.title()
functions to set font properties for the titles and labels of a plot.
The fontdict
function on its own takes parameters such as:
serif
, cursive
, sans-serif
, fantasy
, monospace
.blue
, green
, cyan
, white
, yellow
, etc.Now, let’s create a plot and set the font properties for the titles and labels of the plot.
import matplotlib.pyplot as pltimport numpy as npx = np.array([1, 5])y = np.array([10, 20])# creating the font propertiesfont1 = {'family':'fantasy','color':'blue','size':20}font2 = {'family':'serif','color':'darkred','size':15}font3 = {'family':'cursive','color':'green','size':20}# passing the fontdict parameter to the title as well as the x and y labelsplt.title('Y against X', fontdict = font3 )plt.xlabel("x axis", fontdict = font1)plt.ylabel("y axis", fontdict = font2)plt.plot(x, y)plt.show()
pyplot
module from the matplot library
.numpy
module.x
using the numpy.array()
method.y
using the numpy.array()
method.fontdict
. We passed key value items to its parameters such as family
, color
, and size
.pyplot.title()
method, we gave a title to the plot and also passed the value of our fontdict
parameter as font3
.pyplot.xlabel()
method, we gave a label to the x-axis of our plot and also passed the value of our fontdict
parameter as font1
.pyplot.ylabel()
method, we gave a label to the y-axis of our plot and also passed the value of our fontdict
parameter as font2
.pyplot.plot()
method, we plotted the y
against x
(i.e., x
on the x-axis and y
on the y-axis.pyplot.show()
method, we asked Python to show us our plot.