python对坐标轴进行调整

from pylab import *
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import MultipleLocator

if __name__ == '__main__':
    x = np.linspace(-5, 5, 100)
    y1 = np.sin(x)
    y2 = np.cos(x)
    plt.figure(num=1, figsize=(8, 5))  # figsize调整大小
    plt.plot(x, y1)
    plt.plot(x, y2, color='xkcd:sky blue', linewidth=5.0, linestyle='--')
    plt.xlim((0, 5))
    plt.xlabel('axis=0')
    plt.ylim((0, 2))
    plt.xticks(np.arange(-5, 5.5, 0.5))  # plt.xticks(())隐藏坐标轴
    plt.yticks(np.arange(-2, 2.5, 0.5))
    xmajorLocator = MultipleLocator(2)
    xminorLocator = MultipleLocator(1)
    # 调整坐标轴
    xa = subplot(111)
    xa.xaxis.set_major_locator(xmajorLocator)
    xa.xaxis.set_minor_locator(xminorLocator)
    xa.xaxis.grid(True, which='major')
    ax = plt.gca()
    ax.spines['top'].set_color('none')
    ax.spines['right'].set_color('none')
    ax.xaxis.set_ticks_position('bottom')
    ax.spines['bottom'].set_position(('data', -2))
    ax.yaxis.set_ticks_position('left')
    ax.spines['left'].set_position(('data', 0))  # 0表示原点
    plt.show()

效果图:
python对坐标轴进行调整_第1张图片

你可能感兴趣的:(python算法,机器学习,Python-绘图)