【问题标题】:How to invert axes of a function with matplotlib如何使用 matplotlib 反转函数的轴
【发布时间】:2020-04-27 19:38:52
【问题描述】:

我有以下功能,我正在寻找的是反转x和y轴。

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

major_ticks = np.arange(-2, 10+2, 1)
minor_ticks = np.arange(-2, 10+2, 0.25)

ax.set_xticks(major_ticks)
ax.set_xticks(minor_ticks, minor=True)
ax.set_yticks(major_ticks)
ax.set_yticks(minor_ticks, minor=True)

ax.grid(which='both')

ax.grid(which='minor', alpha=0.2)
ax.grid(which='major', alpha=0.5)

plt.title('Image')
plt.xlabel('Height (m)')
plt.ylabel('Length (m)')
plt.axis('equal')

function = np.array([0, 0.52, 0.92, 0.8])    
plt.plot(function)

下图说明了它的外观,最好的问候。

【问题讨论】:

    标签: python python-3.x numpy matplotlib


    【解决方案1】:

    plot 命令允许您同时指定xy,但是当您使用单个向量调用它时,它假定第一个向量是整数列表。所以要反转情节,创建默认的x,然后交换xy

    plt.plot(function, np.arange(4), '.-') # function is now treated as x, and I've created the default x and am using it for y.
    

    另外,我重命名了坐标轴并删除了大部分格式,所以这看起来是正确的,但唯一有趣的变化是 plot 命令。

    【讨论】:

    • 非常感谢您非常有用的回答。
    猜你喜欢
    • 2023-02-08
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多