Python Superimpose Plots with i = 2 in Python

  • Thread starter Thread starter quasarLie
  • Start date Start date
  • Tags Tags
    Plots
AI Thread Summary
The discussion revolves around creating multiple plots in a single figure using Python's Matplotlib library. A user shares code that attempts to plot data based on user input for different indices and time steps. Key points include the use of conditional statements to determine which data to plot based on the index value. A question arises about the effectiveness of using an if statement for plotting, suggesting that a for loop may be more appropriate for generating multiple plots. Additionally, the conversation highlights the need for subplots when dealing with different axes scales, providing examples of both methods. The importance of organizing plot commands before the display command is emphasized for clarity and functionality.
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:
Technology 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()
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top