Superimpose Plots with i = 2 in Python

  • Context: Python 
  • Thread starter Thread starter quasarLie
  • Start date Start date
  • Tags Tags
    Plots
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 2K views
quasarLie
Messages
50
Reaction score
0
Hello,
I want to have multiple plot in the same figure for i=2, this is my code
Mod note: Edited the code below to prevent some array indexes from being interpreted incorrectly as BBCode italics tags.
Python:
import numpy as np
import matplotlib.pyplot as plt
from math import *
from scipy.integrate import quad
import euler as md
print('Data File Name ?')
file_name = raw_input()
#print(file_name)
#file_name='spectral.dat'
print('Index')
i = int(raw_input())
print('Time Step')
N_time = int(raw_input())
print('Graph Title ?')
title = raw_input()

data=md.extraction_spectra(file_name)
data_name=['Wavelenth ', , 'Masse', 'Energy', 'Time (Myr)', 'Normalized  mass (Msun)' ]if i==2:
    plt.plot(data[0],data[ i][N_time])
    plt.xscale('log')
    plt.yscale('log')
    plt.title(graph_title)
    plt.xlabel(data_name[0])
    plt.ylabel(data_name[ i])

    plt.show()

if i==3:
    plt.plot(data[1],data[ i][N_time])
    plt.xscale('log')
    #plt.yscale('log')
    plt.title(graph_title)
    plt.xlabel(data_name[1])
    plt.ylabel(data_name[ i])
    plt.show()
Thanks
 
Last edited by a moderator:
Physics news on Phys.org
What is your question? What does this if statement do:

Python:
if i==2:
    plt.plot(data[0],data[ i][N_time])
    plt.xscale('log')
    plt.yscale('log')
    plt.title(graph_title)
    plt.xlabel(data_name[0])
    plt.ylabel(data_name[ i])

If you want multiple plots, shouldn't this be a for loop?
 
Last edited by a moderator:
For multiple plots on the same plot axes, just give all the plot commands before the show command for example
Python:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-np.pi, np.pi, 100)
plt.plot(x, np.sin(x))
plt.plot(x, np.cos(x))
plt.show()

If the plots have different axes scales, you have to use different subplots within the same figure. For example,
Python:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-np.pi, np.pi, 100)

fig, ax = plt.subplots(1, 2, sharey=True)
ax[0].plot(x, numpy.sin(x))
ax[1].plot(x, numpy.cos(x))
plt.show()