在同一个窗口中清晰地画出三条线并点缀一条线?

时间:2015-09-26 13:53:15

标签: python matplotlib

如何在同一窗口中清晰地绘制三条线?此外,我还希望能够将这些线条点缀。

这是我到目前为止所做的:

# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np 
import math
# opgave 4
def f_opgave4 (x):
    return x**x

def area_under(f, a, b, N):
    return (b-a) / N * (sum ([f_opgave4(a + (b-a)*k/N) for k in range(1,N)]) )

print area_under(f_opgave4, 0.0, 1.0, 100000)

# opgave 5
def f_opgave5(x):
    return math.sin(x)

def area_under(f, a, b, N):
    return (b-a) / N * (sum ([f_opgave5(a + (b-a)*k/N) for k in range(1,N)]) )

    print area_under(f_opgave5, 0.1, 2.0 , 100000)

    # opgave 6
    def f(x):
        return math.sin(x**2)

    def area_under(f, a, b, N):
        return (b-a) / N * (sum ([f(a + (b-a)*k/N) for k in range(1,N)]) )

    print area_under(f, 0.0, math.pi , 100000)

    # nu laten we tekenen 
    x_all = []
    y_opgave4 = []
    y_opgave5 = []
    y_opgave6 = []
    for x in np.arange(-10,10,0.01):
       y4 = f_opgave4(x)
      y5 = f_opgave5(x)
      y6 = f(x)
     x_all.append(x)
     y_opgave4.append(y4)
     y_opgave5.append(y5)
      y_opgave6.append(y6)
    plt.plot(x_all, y_opgave4, 'g-')
    plt.show()
    plt.plot(x_all, y_opgave5, 'r-')
    plt.show()
    plt.plot(x_all, y_opgave6, 'b-')
    plt.show()

2 个答案:

答案 0 :(得分:0)

plt.plot(x_all, y_opgave4, 'ro', x_all, y_opgave5, 'bo', x_all, y_opgave6, 'go')
plt.show()

答案 1 :(得分:0)

哈利路亚 谢谢Stackoverflow 这是结局代码 但我还是有问题 我需要在同一个窗口中绘制所有函数 现在每件事情都很好,但我收到了错误(double_scalars) 但我不能使用相同的x范围值或我所拥有的所有f的想法 如果你试过(我的意思是一个x列表),图形将不清晰,因为(c ^ x)提升得如此之快并且会使y轴变大,而结果其他f函数将显示为直线< / p>

    # -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np 
import math
    # opgave 4
def f_opgave4 (x):
    return x**x

    def opp_under(f, a, b, N):
    return (b-a) / N * (sum ([f_opgave4(a + (b-a)*k/N) for k in range(1,N)]) )

print opp_under(f_opgave4, 0.0, 1.0, 100000)

# opgave 5
def f_opgave5(x):
    return math.sin(x)

def areaa_under(f, a, b, N):
    return (b-a) / N * (sum ([abs(f_opgave5(a + (b-a)*k/N)) for k in range(1,N)]) )

    print areaa_under(f_opgave5, 0.1, 2.0 , 100000)

# opgave 6
def f(x):
        return math.sin(x**2)

def area_under(f, a, b, N):
    return (b-a) / N * (sum ([abs(f_opgave5(a + (b-a)*k/N)) for k in range(1,N)]) )

print area_under(f, 0.0, math.pi , 100000)

# nu laten we tekenen 
    x1_all = []
y_opgave4 = []
for x in np.arange(-2,2,0.01):
    y4 = f_opgave4(x)
        x1_all.append(x)
    y_opgave4.append(y4)

plt.plot(x1_all, y_opgave4, 'r')

x2_all = []
y_opgave5 = []
y_opgave6 = []
    for x in np.arange(-4,4,0.01):
    y5 = f_opgave5(x)
    y6 = f(x)
    x2_all.append(x)
        y_opgave5.append(y5)
    y_opgave6.append(y6)

plt.plot(x2_all, y_opgave5, 'b', x2_all, y_opgave6, 'g')

plt.axvline(x=0.,color='k',ls='dashed')
plt.axhline(y=0.,color='k',ls='dashed')
for line in [0,0.1,2,math.pi]:
        plt.axvline(x=line ,color='y',ls='dashed')
plt.text( 2, 3.5, " $f(x)=x^x$  ", color = 'red', fontsize = 20) 
plt.text( 1.2, 1.2, " $f(x)= sin(x)$  ", color = 'blue', fontsize = 20) 
plt.text( -3.8, 1.5, " $f(x)=sin(x^2)$  ", color = 'green', fontsize = 20) 
    plt.show()