首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带Matplotlib的三角子图

带Matplotlib的三角子图
EN

Code Review用户
提问于 2018-03-18 14:44:42
回答 1查看 447关注 0票数 3

该代码由4个函数组成,其结果是一个包含3个子图的图形:

  • 正弦波
  • 余弦波
  • 和两者的叠加(双色波)。

我已经用四种方式展示了,前两种方法基本上是相同的,但是波的产生是不同的。我还展示了不同的方法,直接使用plt.subplots(3, 1)添加子图,使用plt.subplot(3,1,i)一次添加一个子图,最后一个函数使用正弦和余弦图数据来生成双色波。

我想知道如何改进代码,以及如何改进使用matplotlib的方法..?

代码语言:javascript
复制
import matplotlib.pyplot as plt
import math
pi = math.pi


ex_1 = "1. Using lists (comprehension) to contain A*sin(a*x), B*cos(b*x), and the superposition. \
Using plt.subplots to created the plots."

def trig_subplots_1(x=[], amplitude=(1, 1), frequency=(1, 1)):

    sine = [amplitude[0]*math.sin(frequency[0]*i) for i in x]
    cosine = [amplitude[1]*math.cos(frequency[1]*i) for i in x]
    superposition = [s+c for s,c in zip(sine, cosine)]

    fig, ax = plt.subplots(3,1)

    ax[0].plot(x, sine)
    ax[0].set_title('Sine wave')

    ax[1].plot(x, cosine)
    ax[1].set_title('Cosine wave')

    ax[2].plot(x, superposition)
    ax[2].set_title('Bichromatic wave')

    plt.tight_layout()
    plt.show()


ex_2 = "2. Generate A*sin(a*x), B*cos(b*x), and the superposition by list.append in a for-loop. \
Using plt.subplots to created the plots."

def trig_subplots_2(x=[], amplitude=(1, 1), frequency=(1, 1)):

    sine, cosine, superposition = [], [], []
    for i in x:
        sine.append( amplitude[0]*math.sin(frequency[0]*i) )
        cosine.append( amplitude[1]*math.cos(frequency[1]*i) )
        superposition.append( sine[-1] + cosine[-1] )

    fig, ax = plt.subplots(3,1)

    ax[0].plot(x, sine)
    ax[0].set_title('Sine wave')

    ax[1].plot(x, cosine)
    ax[1].set_title('Cosine wave')

    ax[2].plot(x, superposition)
    ax[2].set_title('Bichromatic wave')

    plt.tight_layout()
    plt.show()


ex_3 = "3. Similar as no.1, but creating each subplot axes by using plt.subplot(nrows, ncols, index)."

def trig_subplots_3(x=[], amplitude=(1, 1), frequency=(1, 1)):

    sine = [amplitude[0]*math.sin(frequency[0]*i) for i in x]
    cosine = [amplitude[1]*math.cos(frequency[1]*i) for i in x]
    superposition = [s+c for s,c in zip(sine, cosine)]

    plt.subplot(3,1,1)
    plt.title('Sine wave')
    plt.plot(x, sine)

    plt.subplot(3,1,2)
    plt.title('Cosine wave')
    plt.plot(x, cosine)

    plt.subplot(3,1,3)
    plt.title('Bichromatic wave')
    plt.plot(x, superposition)

    plt.tight_layout()
    plt.show()


ex_4 = "4. Similar as no.1, but the superposition wave is generated after plotting the sine and cosine then and \
get the y values from each plot using .get_ydata()."

def trig_subplots_4(x=[], amplitude=(1, 1), frequency=(1, 1)):

    sine = [amplitude[0]*math.sin(frequency[0]*i) for i in x]
    cosine = [amplitude[1]*math.cos(frequency[1]*i) for i in x]

    fig, ax = plt.subplots(3,1)

    sine_plot = ax[0].plot(x, sine)
    ax[0].set_title('Sine wave')

    cosine_plot = ax[1].plot(x, cosine)
    ax[1].set_title('Cosine wave')

    superposition = sine_plot[0].get_ydata() + cosine_plot[0].get_ydata()
    ax[2].plot(x, superposition)
    ax[2].set_title('Bichromatic wave')

    plt.tight_layout()
    plt.show()



x = [i*0.01*pi for i in range(1000)]
amplitude = (1, 1)
frequency = (1, 0.75)

print(ex_1)
trig_subplots_1(x, amplitude, frequency)
print(ex_2)
trig_subplots_2(x, amplitude, frequency)    
print(ex_3)
trig_subplots_3(x, amplitude, frequency)    
print(ex_4)
trig_subplots_4(x, amplitude, frequency)    
EN

回答 1

Code Review用户

发布于 2018-03-19 00:03:08

你的代码重复了一点。例如,

代码语言:javascript
复制
ax[0].plot(x, sine)
ax[0].set_title('Sine wave')

ax[1].plot(x, cosine)
ax[1].set_title('Cosine wave')

ax[2].plot(x, superposition)
ax[2].set_title('Bichromatic wave')

可以使用助手函数(或者是for-循环)重写它,它可能如下所示:

代码语言:javascript
复制
def plot_subroutine(axis, xdata, ydata, title):
    axis.plot(xdata, ydata)
    axis.set_title(title)

y1 = ... # sine 
y2 = ... # cosine
y3 = ... # bichromatic
yn = (y1, y2, y3)

title1 = 'Sine Wave'
title2 = 'Cosine Wave'
title3 = 'Bichromatic Wave'
titles = (title1, title2, title3)

fig, ax = plt.subplots(3,1)

然后,而不是单独调用每个绘图子例程(如下所示)

代码语言:javascript
复制
plot_subroutine(ax[0], x, yn[0])
plot_subroutine(ax[1], x, yn[1])
plot_subroutine(ax[2], x, yn[2])

相反,你可以做这样的事情

代码语言:javascript
复制
if len(ax) == len(yn):
    for i in range(len(ax)):
        plot_subroutine(ax[i], x, yn[i])
plt.show()

for-循环的优点是以后可以在不更改原始代码的情况下更改输出图的数量(如果您想要输出2或4个样条,或者可能将ydata更改为完全不同的代码)。

这可能是相关的,也可能不是相关的,但将每个单独的情节与特定的图例标签、标记样式和/或颜色关联起来可能更好,所有这些都可以使用matplotlib来处理。这对于重叠的地块尤其有用(特别是考虑到您的数据由sin(x)cos(x)在相同的x间隔上组成)。

票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/189873

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档