Python | Rotating Text in Plots

Rotating Text in Plots in Python. Here, we are going to learn about the rotating text in plots and its Python implementation.
Submitted by Anuj Singh, on July 19, 2020

In this tutorial, we are going to rotate the text. We can manually align the text by adding a command for the angle of rotation within matplotlib.pyplot.text().

The following are the illustrations of the rotated text.

plt.text(30,20,'Matplotlib', rotation=90.)
#Rotation of 90 degree
Python | Rotating Text in Plots (1)
plt.text(30,20,'Matplotlib', rotation=180.)
#Rotation of 180 degree
Python | Rotating Text in Plots (2)
plt.text(30,20,'Matplotlib', fontsize=15, color='g', rotation=-30.)
#Rotation of -30 degree
Python | Rotating Text in Plots (3)
plt.text(0,80, 'Matplotlib', bbox=dict(facecolor='yellow', alpha=0.5), fontsize=15, rotation=45.)
#Rotation of 45 degree
Python | Rotating Text in Plots (4)

Python code for rotating text in plots

# Data Visualization using Python
# Rotating Text Box

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(50)
y1 = np.arange(50)
for i in range(50):
    y1[i] = 2*x[i] + np.random.randint(0,5)

# Adding Text Illustration 1
plt.figure()
plt.plot(x,y1)
plt.xlabel('Number Line')
plt.ylabel('Function')
plt.title('Rotating 90 degree')
plt.text(30,20,'Matplotlib', rotation=90.)

# Adding Text Illustration 2
plt.figure()
plt.plot(x,y1)
plt.xlabel('Number Line')
plt.ylabel('Function')
plt.title('Rotating 180 degree')
plt.text(30,20,'Matplotlib', fontsize=15, rotation=180.)

# Adding Text Illustration 3
plt.figure()
plt.plot(x,y1)
plt.xlabel('Number Line')
plt.ylabel('Function')
plt.title('Rotating -30 degree')
plt.text(30,20,'Matplotlib', fontsize=15, color='g', rotation=-30.)

# Adding Text Illustration 4
plt.figure()
plt.plot(x,y1)
plt.xlabel('Number Line')
plt.ylabel('Function')
plt.title('Rotating 45 degree')
plt.text(0,80, 'Matplotlib', bbox=dict(facecolor='yellow', alpha=0.5),
         fontsize=15, rotation=45.)

Output:

Output is as figure





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.