Sunday, February 7, 2021

Python Pool: Pedaling Around with Matplotlib Font

Hello geeks and welcome in this article we will cover Matplotlib Font. Along with that, we will look at different methods through which we can change the font size. We will also look at methods through which we can change the font colour and font family.

When dealing with any type of graph font is an integral part of it. As we require the font for the purpose of labelling axes. The title, legend everything is nothing but font. These things help in increasing the overall understanding of the graph. In this article, we will look at how we can modify it according to our need.

Playing with fonts in Matplotlib

In this section, we will look at the different methods using which we can change the font size. Following which we will also discuss how to change the font colour and family. Let us consider a sample graph over which we will perform the changes.

from matplotlib import pyplot as plt
x=["MON","TUE","WED","THU","FRI"]
y=[3,7,4,3,11]
plt.xlabel("Day")
plt.ylabel("Sales-Made")
plt.bar(x,y)
plt.show()
Matplotlib Font

Here we have considered a simple Bar-graph for observation purpose. The graph is the representation of a number of sales made by the salesman for a week. Here we have our x and y-axis labels. Now if we wish to change it then the easiest method will be to just add the size parameter in the label tag.

from matplotlib import pyplot as plt
x=["MON","TUE","WED","THU","FRI"]
y=[3,7,4,3,11]
plt.xlabel("Day",size=16)
plt.ylabel("Sales-Made",size=18)
plt.bar(x,y)
plt.show()

When you run this code you can spot the difference between the 2 outputs. This is really and flexible way of changing the font-size. Let us now discuss some other methods as well.

Changing font size globally

In this section, we will see how to change the font size globally. By default in matplotlib, the font size is equal to 10. Using globally we can set the size once and for all the things involving font.

In order to do so, we will be using the function rcParams. This function particularly is used for the purpose of changing the default values.

from matplotlib import pyplot as plt
plt.rcParams.update({'font.size': 16})
x=["MON","TUE","WED","THU","FRI"]
y=[3,7,4,3,11]
plt.xlabel("Day")
plt.ylabel("Sales-Made")
plt.bar(x,y)
plt.show()

Output:

Change Font golablly

Here we have successfully performed the font size increase. We can notice one more change over here which is that not just the x and y label font size has increased. But also the x-axis that text as markers, Its size has also increased.

Changing Font-Family

from matplotlib import pyplot as plt
plt.rcParams.update({'font.size': 14})
plt.rcParams["font.family"] = "serif"
x=["MON","TUE","WED","THU","FRI"]
y=[3,7,4,3,11]
plt.xlabel("Day")
plt.ylabel("Sales-Made")
plt.bar(x,y)
plt.show()

Output:

Matplotlib Font

The same way using the rcParams we can change the font-style. Not every font that we use in our day to day life or in different word software’s will be found here.

By default, the font is set to “DejaVu Sans” in matplotlib. Let us try to see how can make the font bold and Italic. Also, will we will play with the font-weight for a bit. In order to implement it, we will use the same program.

from matplotlib import pyplot as plt
plt.rcParams.update({'font.size': 14})
plt.rcParams["font.family"] = "serif"
plt.rcParams["font.weight"]=500
plt.rcParams["font.style"]="italic"

x=["MON","TUE","WED","THU","FRI"]
y=[3,7,4,3,11]
plt.xlabel("Day")
plt.ylabel("Sales-Made")
plt.bar(x,y)
plt.show()

Output:

font style matplotlib

The difference in the font of the graph as compared to the first one is quite evident. Here to make the text Italic we have used the “font. style” property. Whereas we have used to font-weight to change the brightness of the font. By default, the font-weight equal to 700 will give you results similar to that of “Bold”. Also in you can type Bold next to font-weight as per syntax.

Changing the colour

from matplotlib import pyplot as plt
plt.rcParams.update({'font.size': 14})
plt.rcParams["font.family"] = "serif"


x=["MON","TUE","WED","THU","FRI"]
y=[3,7,4,3,11]
plt.xlabel("Day",color="#32a6a8")
plt.ylabel("Sales-Made",color="#f518df")
plt.bar(x,y,color="#18f5ed")
plt.show()

Output:

Color Change Matplotlib

Above we have successfully performed the change in colour. In a similar manner, you can also do it. Here I have used Hexadecimal, you can use rbga or just solid colours.

Conclusion

In this article, we covered the Matplotlib Font. We looked at several different methods through which we can change the font size. We also looked at how to change the font style and colour. You can implement a similar method for Tittle and legend in the plot.

I hope this article was able to clear all of your doubts. But in case you have any unsolved queries feel free to write them below in the comment section. Done reading this, why not read about the roll function next.

The post Pedaling Around with Matplotlib Font appeared first on Python Pool.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...