Cheap VPS host selection
Provide server host evaluation information

How to set the coordinate axis in python plot (sample code sharing)

01. Load Library

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

02. Sample Data

x = np.linspace(-np.pi*2, np.pi*2)
y1 = np.sin(x)
Y2=np.power (x, 2) * 0.05 # Exponential operation
df = pd.Dataframe({‘a’: y1, ‘b’: y2}, index=x)

1. Graphics at Default Settings

fig = plt.figure()
df.plot()
plt.show()

2. Set the font size and color of axis labels

fig = plt.figure()
df.plot()
plt.yticks(size=14, color=’grey’)
plt.xticks(size=14, color=’grey’)
plt.show()

3. Change vertical axis scale range

fig = plt.figure()
df.plot()
plt.ylim(-2, 3)
plt.yticks(size=14, color=’grey’)
plt.xticks(size=14, color=’grey’)
plt.show()

4. Replace horizontal axis scale label

xticks = [-2*np.pi, -3*np.pi/2, -np.pi, -np.pi/2, 0, np.pi/2, np.pi, 3*np.pi/2, 2*np.pi] xticklabes = [‘-2π’, ‘-3π/2’, ‘-π’, ‘-π/2’, 0, ‘π/2’, ‘π’, ‘3π/2’, ‘2π’ ] fig = plt.figure()
df.plot()
plt.ylim(-2, 3)
plt.yticks(size=14, color=’grey’)
plt.xticks(xticks, xticklabes, size=14, color=’grey’)
plt.show()

5. Move Axis to Center

xticks = [-2*np.pi, -3*np.pi/2, -np.pi, -np.pi/2, 0, np.pi/2, np.pi, 3*np.pi/2, 2*np.pi] xticklabes = [‘-2π’, ‘-3π/2’, ‘-π’, ‘-π/2’, 0, ‘π/2’, ‘π’, ‘3π/2’, ‘2π’ ] fig = plt.figure()

df.plot()
ax = plt.gca()
plt.ylim(-2, 3)
plt.yticks(size=14, color=’grey’)
plt.xticks(xticks, xticklabes, size=14, color=’grey’)

ax.spines[‘right’].set_color(‘none’)
ax.spines[‘top’].set_color(‘none’)
ax.spines[‘left’].set_position((‘data’, 0))
ax.spines[‘bottom’].set_position((‘data’, 0))

plt.show()

6. Set horizontal axis scale label to display at an oblique angle

xticks = [-2*np.pi, -3*np.pi/2, -np.pi, -np.pi/2, 0, np.pi/2, np.pi, 3*np.pi/2, 2*np.pi] xticklabes = [‘-2π’, ‘-3π/2’, ‘-π’, ‘-π/2’, 0, ‘π/2’, ‘π’, ‘3π/2’, ‘2π’ ] fig = plt.figure()

df.plot()
ax = plt.gca()

ax.spines[‘right’].set_color(‘none’)
ax.spines[‘top’].set_color(‘none’)
ax.spines[‘left’].set_position((‘data’, 0))
ax.spines[‘bottom’].set_position((‘data’, 0))

plt.ylim(-2, 3)
plt.yticks(size=14, color=’grey’)
plt.xticks(xticks, xticklabes, rotation=-30, size=14, color=’grey’)

plt.show()

This is how python plot sets the coordinate axis.

Do not reprint without permission: Cheap VPS evaluation » How to set the coordinate axis in python plot (sample code sharing)