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!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.