Superimpose Plots with i = 2 in Python

  • Context: Python 
  • Thread starter Thread starter quasarLie
  • Start date Start date
  • Tags Tags
    Plots
Click For Summary
SUMMARY

The discussion focuses on superimposing multiple plots in Python using Matplotlib, specifically for the variable i=2. The provided code utilizes numpy and scipy.integrate.quad for data extraction and plotting. Key insights include the use of conditional statements to determine which data to plot based on the value of i, and the suggestion to use a for loop for multiple plots or subplots for different axes scales. The discussion emphasizes the importance of structuring plot commands before the plt.show() function.

PREREQUISITES
  • Proficiency in Python programming
  • Familiarity with Matplotlib version 3.4 or higher
  • Understanding of NumPy for numerical operations
  • Basic knowledge of data extraction techniques using custom modules like euler
NEXT STEPS
  • Learn how to implement for loops for multiple plots in Matplotlib
  • Explore the use of subplots in Matplotlib for different axes scales
  • Investigate advanced data extraction methods using custom Python modules
  • Study the application of logarithmic scales in data visualization
USEFUL FOR

Data scientists, Python developers, and researchers involved in data visualization and analysis using Matplotlib.

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()
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
Replies
4
Views
5K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 21 ·
Replies
21
Views
6K
Replies
7
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K