[Python] Gráfico em branco

Discussão em 'Python' iniciado por Stack, Março 2, 2025.

  1. Stack

    Stack Membro Participativo

    O programa esta rodando normal, porém, os gráficos de posição, linha 339 e 345 ficam em branco, os outros ficam normal. Gostaria de saber o que fazer para resolver?

    Segue código:

    # -*- coding: utf-8 -*-
    """
    Created on Wed Jan 30 23:46:08 2019

    @author: Kratos
    """


    '------------------- Bibliotecas Importadas -------------------'

    import math
    import matplotlib.pyplot as plt


    '------------------- Condições Iniciais -------------------'
    R = 5.0E-15 #Raio de um átomo de Ferro
    d = 1.0E-15 #1 fm
    z = 24 #Número de prótons -2
    q = 1.6E-19
    k = 9E9
    m = 1.672E-27
    M = 24 * m
    t = 1E-21
    conttempo = 0.0
    dt = 0.1E-25


    #Parâmetros iniciais do próton e do núcleo.
    #Estabelecemos 1 como o núcleo, 2 como o próton "positivo" e 3 como o próton "negativo".
    x1 = 0.0
    y1 = 0.0
    vo_n = 0.0
    x2 = R
    y2 = d/2
    vox_p2 = 0.0
    voy_p2 = 0.0
    x3 = -R
    y3 = -d/2
    vox_p3 = 0.0
    voy_p3 = 0.0

    '-------------------------- Definição de Funções -----------------------------'

    #Função que retorna o valor do módulo da diferença r - R.
    def modulo_diferenca_posicao(par1_p, par2_p, par1_n, par2_n):

    #Criamos uma lista para armazenar os valores de D.
    lista_D = []

    #Definimos os vetores r e R em função dos parâmetros passados para a função.
    r = [par1_p, par2_p]
    R = [par1_n, par2_n]

    #Fazemos um loop para adicionar na lista D cada par r - R calculado.
    for r, R in zip(r, R):
    lista_D.append(r - R)

    #Elevamos cada item da lista D ao quadrado.
    lista_D_quadrado = [item*item for item in lista_D]

    #Calculamos o módulo de D.
    modulo_D = math.sqrt(sum(lista_D_quadrado))

    return modulo_D


    #Função para calcular a aceleração do próton no eixo x.
    def aceleracao_proton_x(x, modulo_diferenca):
    ax = (z*k*(q**2)*x) / (modulo_diferenca**3)
    return ax


    #Função para calcular a aceleração do próton no eixo y.
    def aceleracao_proton_y(y, modulo_diferenca):
    ay = (z*k*(q**2)*y) / (modulo_diferenca**3) + (k*(q**2)) / (y**2)
    return ay


    #Função para calcular a velocidade
    def velocidade_proton(velocidade_inicial, aceleracao):
    velocidade = velocidade_inicial + (aceleracao * dt)
    return velocidade


    #Função para calcular a posição do proton ou do nucleo.
    def posicao_equacao(posicao_inicial, velocidade_inicial, aceleracao):
    pos = posicao_inicial + (velocidade_inicial*dt) + ((aceleracao*(dt**2))/2)
    return pos


    #Função para calcular a velocidade ou aceleração do nucleo.
    def par_nucleo(par):
    par_nucleo = (m / M)*par
    return par_nucleo


    #Função para transformar as listas em arquivos txt.
    def tabela(nome, lista):
    arquivo = open(nome + '.txt', 'a')
    for i in range(len(lista)):
    arquivo.write(str(lista) + '\n')
    arquivo.close


    #Função para plotar o gráfico da trajetoria.
    def plot_grafico_trajetoria(x, y, xlabel, ylabel, title):
    plt.plot(x, y)
    plt.xlabel(xlabel)
    plt.ylabel(ylabel)
    plt.title(title)
    # plt.scatter(0,0, color='red')
    plt.show()

    #Função para plotar gráficos com 2 linhas.
    def plot_grafico(x, y, xlabel, ylabel):
    plt.plot(x, y)
    plt.xlabel(xlabel)
    plt.ylabel(ylabel)
    plt.show()

    #Função para plotar gráficos com 3 linhas.
    def plot_grafico2(par_1, par_2, par_3, xlabel, ylabel, legend1, legend2, title):
    plt.plot(par_1, par_2, label = legend1)
    plt.plot(par_1, par_3, label = legend2)
    plt.xlabel(xlabel)
    plt.ylabel(ylabel)
    plt.legend()
    plt.title(title)
    plt.show()


    '------------------- Listas Criadas -------------------'

    posx_p2 = []
    posy_p2 = []
    posr_p2 = []
    posx_p3 = []
    posy_p3 = []
    posr_p3 = []
    posx_n = []

    velx_p2 = []
    vely_p2 = []
    velmodulo_p2 = []
    velx_p3 = []
    vely_p3 = []
    velmodulo_p3 = []
    velx_n = []

    acelx_p2 = []
    acely_p2 = []
    acelmodulo_p2 = []
    acelx_p3 = []
    acely_p3 = []
    acelmodulo_p3 = []
    acelx_n = []

    tempo = []

    '-----------------------------------'

    posx_p2_new = []
    posy_p2_new = []
    posr_p2_new = []
    posx_p3_new = []
    posy_p3_new = []
    posr_p3_new = []
    posx_n_new = []

    velx_p2_new = []
    vely_p2_new = []
    velmodulo_p2_new = []
    velx_p3_new = []
    vely_p3_new = []
    velmodulo_p3_new = []
    velx_n_new = []

    acelx_p2_new = []
    acely_p2_new = []
    acelmodulo_p2_new = []
    acelx_p3_new = []
    acely_p3_new = []
    acelmodulo_p3_new = []
    acelx_n_new = []


    '------------------- Laço de Repetição -------------------'

    print('--------------------- Início do Processamento de Dados -------------------------')

    while (t > conttempo):

    modulo_diferenca_p2 = modulo_diferenca_posicao(x2, y2, x1, y1)
    ax_p2 = aceleracao_proton_x(x2, modulo_diferenca_p2)
    ay_p2 = aceleracao_proton_y(y2, modulo_diferenca_p2)
    amodulo_p2 = math.sqrt(ax_p2**2 + ay_p2**2)
    modulo_diferenca_p3 = modulo_diferenca_posicao(x3, y3, x1, y1)
    ax_p3 = aceleracao_proton_x(x3, modulo_diferenca_p3)
    ay_p3 = aceleracao_proton_y(y3, modulo_diferenca_p3)
    amodulo_p3 = math.sqrt(ax_p3**2 + ay_p3**2)
    ax_n = par_nucleo(ax_p2)

    vx_p2 = velocidade_proton(vox_p2, ax_p2)
    vy_p2 = velocidade_proton(voy_p2, ay_p2)
    vmodulo_p2 = math.sqrt(vx_p2**2 + vy_p2**2)
    vx_p3 = velocidade_proton(vox_p3, ax_p3)
    vy_p3 = velocidade_proton(voy_p3, ay_p3)
    vmodulo_p3 = math.sqrt(vx_p3**2 + vy_p3**2)
    vx_n = par_nucleo(vx_p2)

    x_p2 = posicao_equacao(x2, vox_p2, ax_p2)
    y_p2 = posicao_equacao(y2, voy_p2, ay_p2)
    r_p2 = math.sqrt(x_p2**2 + y_p2**2)
    x_p3 = posicao_equacao(x3, vox_p3, ax_p3)
    y_p3 = posicao_equacao(y3, voy_p3, ay_p3)
    r_p3 = math.sqrt(x_p3**2 + y_p3**2)
    x_n = posicao_equacao(x1, vo_n, ax_n)

    vetor_p2_loop = [x_p2, y_p2]
    vetor_p3_loop = [x_p3, y_p3]
    vetor_n_loop = [x_n, 0]

    print('--------------------- t =', conttempo, '-------------------------' + '\n')
    print('Posição do Próton 2:', vetor_p2_loop)
    print('Posição do Próton 3:', vetor_p3_loop)
    print('Posição do Núcleo:', vetor_n_loop)
    print('----------------------------------------------------------')


    #Preenchendo as listas
    posx_p2.append(x_p2)
    posy_p2.append(y_p2)
    posr_p2.append(r_p2)
    posx_p3.append(x_p3)
    posy_p3.append(y_p3)
    posr_p3.append(r_p3)
    posx_n.append(x_n)

    velx_p2.append(vx_p2)
    vely_p2.append(vy_p2)
    velmodulo_p2.append(vmodulo_p2)
    velx_p3.append(vx_p3)
    vely_p3.append(vy_p3)
    velmodulo_p3.append(vmodulo_p3)
    velx_n.append(vx_n)

    acelx_p2.append(ax_p2)
    acely_p2.append(ay_p2)
    acelmodulo_p2.append(amodulo_p2)
    acelx_p3.append(ax_p3)
    acely_p3.append(ay_p3)
    acelmodulo_p3.append(amodulo_p3)
    acelx_n.append(ax_n)

    tempo.append(conttempo)

    #Atualizando os valores
    vox_p2 = vx_p2
    voy_p2 = vy_p2
    vox_p3 = vx_p3
    voy_p3 = vy_p3
    vo_n = vx_n

    x2 = x_p2
    y2 = y_p2
    x3 = x_p3
    y3 = y_p3
    x1 = x_n

    #Atualizando o tempo
    conttempo = conttempo + dt

    print('--------------------- Fim do Processamento de Dados -------------------------')


    posx_p2_new = [i * 1.0E15 for i in posx_p2]
    posy_p2_new = [i * 1.0E15 for i in posy_p2]
    posr_p2_new = [i * 1.0E15 for i in posr_p2]
    posx_p3_new = [i * 1.0E15 for i in posx_p3]
    posy_p3_new = [i * 1.0E15 for i in posy_p3]
    posr_p3_new = [i * 1.0E15 for i in posr_p3]
    posx_n_new = [i * 1.0E15 for i in posx_n]


    velx_p2_new = [i * 1.0E15 for i in velx_p2]
    vely_p2_new = [i * 1.0E15 for i in vely_p2]
    velmodulo_p2_new = [i * 1.0E15 for i in velmodulo_p2]
    velx_p3_new = [i * 1.0E15 for i in velx_p3]
    vely_p3_new = [i * 1.0E15 for i in vely_p3]
    velmodulo_p3_new = [i * 1.0E15 for i in velmodulo_p3]
    velx_n_new = [i * 1.0E15 for i in velx_n]

    acelx_p2_new = [i * 1.0E15 for i in acelx_p2]
    acely_p2_new = [i * 1.0E15 for i in acely_p2]
    acelmodulo_p2_new = [i * 1.0E15 for i in acelmodulo_p2]
    acelx_p3_new = [i * 1.0E15 for i in acelx_p3]
    acely_p3_new = [i * 1.0E15 for i in acely_p3]
    acelmodulo_p3_new = [i * 1.0E15 for i in acelmodulo_p3]
    acelx_n_new = [i * 1.0E15 for i in acelx_n]


    '------------------- Criando arquivos txt com as Listas -------------------'

    tabela('Lista x Próton 2', posx_p2_new)
    tabela('Lista y Próton 2', posy_p2_new)
    tabela('Lista r Próton 2', posr_p2_new)

    tabela('Lista x Próton 3', posx_p3_new)
    tabela('Lista y Próton 3', posy_p3_new)
    tabela('Lista r Próton 3', posr_p3_new)

    tabela('Lista r Núcleo', posx_n_new)

    tabela('Lista vx Próton 2', velx_p2_new)
    tabela('Lista vy Próton 2', vely_p2_new)
    tabela('Lista v Próton 2', velmodulo_p2_new)

    tabela('Lista vx Próton 3', velx_p3_new)
    tabela('Lista vy Próton 3', vely_p3_new)
    tabela('Lista v Próton 3', velmodulo_p3_new)

    tabela('Lista v Núcleo', velx_n_new)

    tabela('Lista ax Próton 2', acelx_p2_new)
    tabela('Lista ay Próton 2', acely_p2_new)
    tabela('Lista a Próton 2', acelmodulo_p2_new)

    tabela('Lista ax Próton 3', acelx_p3_new)
    tabela('Lista ay Próton 3', acely_p3_new)
    tabela('Lista a Próton 3', acelmodulo_p3_new)

    tabela('Lista a Núcleo', acelx_n_new)

    tabela('Lista tempo', tempo)


    '------------------- Gráficos -------------------'
    #Plotando os gráficos
    plot_grafico_trajetoria(posx_p2_new, posy_p2_new, 'Posição x (m)', 'Posição y (m)', 'Trajetória do Próton 2')
    #plot_grafico(tempo, pos_r, 'r (m)', 't (s)')
    plot_grafico2(tempo, velx_p2_new, vely_p2_new, 'v (m/s)', 't (s)', 'vx', 'vy', 'Próton 2')
    #plot_grafico(tempo, vel_modulo, 'v (m/s)', 't (s)')
    plot_grafico2(tempo, acelx_p2_new, acely_p2_new, 'a (m/s^2)', 't (s)', 'ax', 'ay', 'Próton 2')
    #plot_grafico(tempo, acel_modulo, 'a (m/s^2)', 't (s)')
    plot_grafico_trajetoria(posx_p3_new, posy_p3_new, 'Posição x (m)', 'Posição y (m)', 'Trajetória do Próton 3')
    #plot_grafico(tempo, pos_r, 'r (m)', 't (s)')
    plot_grafico2(tempo, velx_p3_new, vely_p3_new, 'v (m/s)', 't (s)', 'vx', 'vy', 'Próton 2')
    #plot_grafico(tempo, vel_modulo, 'v (m/s)', 't (s)')
    plot_grafico2(tempo, acelx_p3_new, acely_p3_new, 'a (m/s^2)', 't (s)', 'ax', 'ay', 'Próton 2')
    #plot_grafico(tempo, acel_modulo, 'a (m/s^2)', 't (s)')

    Continue reading...

Compartilhe esta Página