Python

Matplotlib Bold Text

This tutorial explores the methods of making the text bold in Matplotlib. The text could be added to a graph to emphasize a particular part or to represent an area of focus. The modifications are made by Matplotlib’s axe.annotate() function. By utilizing annotations, we would specify the labels on the graphs in bold.

The ‘weight’ or ‘fontweight’ argument is used to provide bold letters in Matplotlib. At the start of every program, we must integrate libraries: Numpy for data gathering and Pyplot for graphic visualizations.

Customizing font size of Matplotlib’s bold label

In this step, we are going to see how to customize the font size of the tag in Matplotlib to bold. The following are the parameters that are utilized in this technique. The label of the graph is determined by the label command.

The plot’s size of the text is modified with the fontsize argument. The bold font is specified via the fontweight argument. Consider the following example on how to adjust the label bold:

import matplotlib.pyplot as plt

import numpy as np

a = np.arange(2, 16, 2.2)

b = np.sin(a)

plt.plot(a, b)

plt.title("Figure", fontsize= 20, fontweight='bold')

plt.show()

First, we import two libraries: matplotlib.pyplot as plt and NumPy as np, respectively. In the subsequent step, we evaluate the data set. These values of the dataset are assigned to the np.arrange() function. The np.sin() function is declared, and the value of the ‘a’ variable is passed to it.

Furthermore, we create a graph by calling the plt.plot() function. To add a label to the graph, we use the plt.title() function and specify the fontsize and fontweight parameters, which we adjust to 20 and bold, accordingly.

Finally, for generating the graph, we utilize the plt.show() method.

Inserting boldly labeled caption

In this step, we examine how to insert the bold highlighted tag in Matplotlib. We may utilize the LaTeX notation for annotations in matplotlib to insert boldly labeled text.

In this example, after integrating the libraries, we define two functions to modify the graphic size and also change spacing among and throughout the subplots. By the use of the numpy library, we decide ‘a’ and ‘b’ attribute values.

For this reason, we declare two separate variables. Furthermore, we create a list of titles assigned to every dispersed object. We define the plt.scatter() function, so that we visualize ‘a’ and ‘b’ values. The parameter ‘appoints’ is passed to this method. This parameter is utilized for coloring. Here, we define a for loop and also initialize the variable for the loop. Zipped titles, apoints, and bpoints should all be repeated several times.

In for loop, we are using the annotate() function including a bold LaTeX illustration. We will be using the show() function to present the graph.

Utilizing “Times New Roman” as the font for Matplotlib’s bold label

We could use attribute fontweight=”bold” to get the Matplotlib label bold when utilizing “Times New Roman.”

At the start of the program, we have included three important libraries: NumPy as np, matplotlib.pyplot as plt, and font_manager as fm. We adjust the white spaces between and within the subplots as well as the visual size by calling two separate functions.

Now we are going to make a graph as well as a series of subplots by the use of the plt.subplots() function. We employ the NumPy library and generate x and y extreme values.

To utilize the scatter() approach, we would display x and y data sets. The scatter() method has four arguments. In this function, we also pass a parameter to define the color and marker. The set_title() method is now called to specify the label of the graph, fontname=”Times New Roman” and fontweight=”bold” for the label. To terminate the code, we just use the show() feature to visualize the graph.

Bold title for the graph of sales of fruits

For this scenario, we have to adjust the dataset for visualization. We’ll examine this technique with the help of the succeeding example.

import pandas as pd

import matplotlib.pyplot as plt

revenue = pd.DataFrame({"fruits":['Banana', 'Apple', 'Orange','Grapes']* 2,

"sales": [2498, 1384, 1874, 2094, 3846, 1586, 3590, 4387]})

rev_by_fruits = revenue.groupby('fruits')['sales'].sum()

a = rev_by_fruits.index

b = rev_by_fruits.values

fig, ax = plt.subplots(dpi = 147)

ax.bar(a,b, color='blue');

rev_by_fruits.plot(kind='bar', color='blue');

ax.get_title()

ax.set_title('Sales of fruits');

fig

ax.set_title('Sales of fruits', fontsize=30, color= 'black', fontweight='bold');

fig

We introduce libraries pandas as pd and matplotlib.pyplot as plt. Then we make the dataset. This dataset contains data about various fruits that are available for purchase. We initialize two arrays: one array represents the names of fruits and the other array represents the sales. In addition to this, we have to group that data by executing the function revenue.groupby ().

We passed the array of fruits name and array of sales as a parameter of the function. We acquired a Pandas Array where we’ll illustrate rapidly with Pandas and Matplotlib.

Now, for drawing the Matplotlib graph, we initialize two variables and define plt.subplots() and ax.bar() functions. We are given three parameters (indexes of x and y, color) to the ax.bar() function. The color of the graph is specified in this function.

We have just obtained a very basic graph using this code. Now we customize the label of the graph by using the ax.get_title() function. The outcome contains a blank string, as predicted. So we must use the plt.set_title() function to specify graphic labels. After defining the label of the graph, in the end, we set the font size, hue, and weight of the defined label for the graph.

Conclusion

In this artifact, we talked about the Matplotlib bold text along with a few observations. In Matplotlib, we could alter the font size of the labels to bold. We also looked at how we can utilize Matplotlib’s Times New Roman font style to bold the caption. We discussed the method to insert bold text in our graph.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content