在图形的边缘绘制曲线

时间:2018-11-27 15:32:57

标签: python matplotlib

我有一个函数f的图,它以不连续的方式取决于时间。更确切地说,它对t1<=t<t2和其他所有地方都有特定的行为,例如下面的示例

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

l1=1.
l2=5.
t1=20.
t2=50.
tf=120.
def f1(t):
    if t<t1:
        L = l1
    elif t1<=t<t2:
        L = l2
    else:
        L=l1
    g=L*t
    return g
a=np.linspace(0.,100,1000)
values1=map(f1,a)
fig1=plt.figure(1)
plt.plot(a,values1,color='red')
plt.show()

脉冲图如下

def f2(t):
    if t<t1:
        L = l1
    elif t1<=t<t2:
        L = l2
    else:
        L=l1
    return L
values2=map(f2,a)
fig2=plt.figure(2)
plt.plot(a,values2,color='blue')

plt.show()

我想制作一个以红色曲线为主要图形的图形,并在图形的顶部边缘显示一个小插图以显示蓝色曲线,而没有任何x轴或y轴,只是为了使观看者了解何时进行更改参数L中发生了

2 个答案:

答案 0 :(得分:1)

也许您可以使用inset_axes中的mpl_toolkits.axes_grid1.inset_locator

例如参见:https://matplotlib.org/gallery/axes_grid1/inset_locator_demo.html

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes

fig, axs = plt.subplots(1, 1)

# Create inset of width 1.3 inches and height 0.9 inches
# at the default upper right location
axins = inset_axes(axs, width='20%', height='20%', loc=2)

然后在axins中绘制数据:

axins.plot(data)

您还可以使用以下方法关闭刻度线和标签:

axins.axes.get_yaxis().set_visible(False)
axins.axes.get_xaxis().set_visible(False)

答案 1 :(得分:1)

我认为子图将做您想要的。如果使顶部子图变小,并去除刻度线/标签,则它看起来像处于空白处。这是设置情节的代码段。

f = plt.figure()
# Make 2 subplots arranged vertically with different ratios
(ax, ax2) = f.subplots(2,1, gridspec_kw={'height_ratios':[1,4]})

#remove the labels on your top subplot
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)

ax.plot(a, f2(a))
ax2.plot(a, f1(a), 'r:') #red curve main plt

plt.show()

我使用此代码绘制了一些正弦曲线,结果如下:

enter image description here

这是您要找的吗?

相关问题