【问题标题】:How do I create a secondary axis for a second function in matplotlib?如何为 matplotlib 中的第二个函数创建辅助轴?
【发布时间】:2015-11-26 13:36:50
【问题描述】:

我有两个功能。两者都使用相同的 x 轴,但与另一个函数相比,一个函数的 y 轴值太高,因此一个函数会保持平缓。如何为第二个函数创建辅助 y 轴,以便我可以在一个绘图上查看这两个函数?

这是我的代码:

import math
import matplotlib.pyplot as plt

g = 9.81
vo = 14.5
zo = 2.35
m = 7.257

sin = (vo/((2*((vo**2)+g*zo))**(1/2)))
alpha = math.asin(sin)
tan = math.tan(alpha)

z= []; x = []; E = []
for i in range(1000):
    x.append(i*(1/41))
    z.append(-(g/2)*((x[i]**2)/(vo**2))*(1+tan**2)+tan*x[i]+zo)
    E.append(m*g*z[i])
    if len(z)>=3:
        if z[-1]<=0 and z[-2]>0:
            x0 = (x[-1] + x[-2])/2
        if z[-2]>z[-1] and z[-2]>z[-3]:
            zmax=z[-2]
            xmax=x[-2]


fig = plt.figure()
plt.title("Wurf der Kugel")
plt.ylim(-1,700)
plt.xlim(0,25)
plt.xlabel("Distanz in m")
plt.ylabel("Hoehe in m")
dotx0=(x0,0)
dotmax=(xmax,zmax)
print("Die Wurfweite ist bei {}. Das Maximum ist bei {}".format(dotx0,dotmax))
plt.plot(x0,0,"o", markersize = 8, label=dotx0)
plt.plot(xmax,zmax,"o", markersize = 8, label=dotmax)
plt.grid(True)
plt.legend()
plt.plot(x,z)
plt.plot(x,E)

plt.show()

非常感谢!

【问题讨论】:

标签: python matplotlib


【解决方案1】:

您可以使用twinx 在同一轴上拥有两个不同的 y 刻度。

只需在具有不同比例的绘图命令之间调用 plt.twinx()。

例如:

import numpy as np
from matplotlib import pyplot as plt

x = np.linspace(-10,10,100)
y1 = x**2
# plot the first line
plt.plot(x,y1,'k') # large values
plt.ylabel('y1') # axis label on the left
# create a second y scale on the other side
plt.twinx()
y2 = 0.2*x
# now plot the second line on the second y-scale
plt.plot(x,y2,'r') # small values
plt.ylabel('y2') # axis label on the right
plt.show()

【讨论】:

  • 如何在代码中使用 twinx?对不起,我是初学者!
  • 谢谢!我试试看!
猜你喜欢
  • 2020-11-09
  • 1970-01-01
  • 2020-11-26
  • 2019-12-04
  • 1970-01-01
  • 2022-01-08
  • 2020-11-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多