Python drawing with horizontal and vertical axes

directly on the code

import math
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.axisartist as axisartist

def f(x):
    return x ** 2 - 3 * x + 2 - math.e ** x
    # return math.e ** x

if __name__ == '__main__':
    x = np.linspace(-1, 1, 100)
    y = f(x)
    fig = plt.figure()
    ax = axisartist.Subplot(fig, 111)
    ax.axis[:].set_visible(False)  # 通过set_visible方法设置绘图区所有坐标轴隐藏
    ax.axis["x"] = ax.new_floating_axis(0, 0)  # ax.new_floating_axis代表添加新的坐标轴
    ax.axis["x"].set_axisline_style("->", size=1.0)  # 给x坐标轴加上箭头
    # 添加y坐标轴,且加上箭头
    ax.axis["y"] = ax.new_floating_axis(1, 0)
    ax.axis["y"].set_axisline_style("-|>", size=1.0)
    # 设置x、y轴上刻度显示方向
    ax.axis["x"].set_axis_direction("top")
    ax.axis["y"].set_axis_direction("right")
    fig.add_axes(ax)
    plt.plot(x, y)
    plt.show()

Guess you like

Origin blog.csdn.net/weixin_50497501/article/details/128586588