Superimpose Plots with i = 2 in Python

  • Python
  • Thread starter quasarLie
  • Start date
  • Tags
    Plots
In summary, the code allows for the creation of multiple plots on the same figure by using the if statement to determine which data to plot. For plots with different axes scales, subplots within the same figure must be used.
  • #1
quasarLie
51
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
  • #2
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:
  • #3
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()
 

FAQ: Superimpose Plots with i = 2 in Python

What is the purpose of superimposing plots with i = 2 in Python?

The purpose of superimposing plots with i = 2 in Python is to compare two different plots on the same graph in order to visualize and analyze their similarities and differences. This can be useful for data analysis and pattern recognition.

How do I superimpose plots with i = 2 in Python?

To superimpose plots with i = 2 in Python, you can use the Matplotlib library. First, import the library and the necessary modules. Then, use the 'plt.plot()' function to plot each set of data separately, specifying the value of i as 2. Finally, use the 'plt.show()' function to display the superimposed plot.

Can I superimpose more than two plots with i = 2 in Python?

Yes, you can superimpose as many plots as you want with i = 2 in Python. Simply use the 'plt.plot()' function for each set of data, specifying i = 2 for each plot. However, keep in mind that too many superimposed plots can make the graph cluttered and difficult to interpret.

What are the advantages of using Python to superimpose plots with i = 2?

Python offers a variety of libraries and tools for data analysis and visualization, making it a convenient and powerful tool for superimposing plots with i = 2. Additionally, Python is a versatile and widely used programming language, making it easy to find resources and support for this task.

Are there any limitations to superimposing plots with i = 2 in Python?

One limitation of superimposing plots with i = 2 in Python is that it may not be suitable for all types of data. For example, if the data has drastically different ranges, superimposing plots with i = 2 may make one plot appear too small to be visible. In such cases, it may be better to use other visualization techniques, such as subplots or overlaid histograms.

Similar threads

Replies
6
Views
2K
Replies
1
Views
1K
Replies
1
Views
726
Replies
15
Views
2K
Replies
21
Views
4K
Replies
5
Views
3K
Replies
1
Views
1K
Back
Top