No plot window in matplotlib
You can type
import pylab
pylab.show()
or better, use ipython -pylab
.
Since the use of pylab
is not recommended anymore, the solution would nowadays be
import matplotlib.pyplot as plt
plt.plot([1,2,3])
plt.show()
Plot not showing in python
You have imported matplotlib
itself as plt
. This is not commonly done. Instead you want to import matplotlib.pyplot
. This then allows you to do all the conventional plotting in matplotlib
such as plt.plot()
etc., in addition to showing any figures that have been generated using pandas
plotting functionality which is what seems to be being used in the question
The solution to your problem is to add plt.show()
at the end of your code using the correct module imports at the top of the code:
import matplotlib
import matplotlib.pyplot as plt
# the rest of your code
plt.show()
Plot of function not showing up when called
As other users have commented, x and y are single values as yo uhave defined them.
I would recommend defining p_n
as a function, and using list comprehension or arrays for x and y. For example;
def p_n(N):
a=1/(2**N)
b=factorial(N)
c=factorial(N-25)
d=factorial(25)
return a*((b)/(c*d))
N = 10
x = list(range(N))
y = [p_n(x_val) for x_val in x]
Then pass x
and y
to plt.plot(...)
as you did before. You should get the following result:
Live Graphics Using Matplotlib Not Showing the Plot
You could make use of matplotlib's
interactive mode plt.ion()
to initiate interactive plotting. Then call plt.show()
to display the window, and update it using plt.gcf().canvas.draw
, as shown here:
import numpy as np
import matplotlib.pyplot as plt
# Parameters
time = 10 # maximum time for the simulation
h = 0.1 # step size
steps = int(time/h) # number of steps
order = 4 # two second order equations
ICs = [0, 0, 0, 0.25, 0] # intial conditions; t0, x1, x1dot, x2, x2dot
# Initializing vars array
vars = np.empty(shape=(order,steps)) # each row is another var, i.e. x1,x2,...
# Set initial conditions for each var
for i in range(order):
vars[i][0] = ICs[i+1]
t = np.empty(steps)
t[0] = ICs[0]
K = np.zeros(shape=(4,len(vars))) # Each row is k1, k2, k3, k4 for each var
fig,ax=plt.subplots()
plt.ion() # set interactive mode on
plt.show() # open display window
# ODE function
def ODE():
dx1dt = np.random.randint(-1000,1000)
dv1dt = np.random.randint(-1000,1000)
dx2dt = np.random.randint(-1000,1000)
dv2dt = np.random.randint(-1000,1000)
return(np.array([dx1dt, dv1dt, dx2dt, dv2dt]))
# Loop calculates each var value using RK 4th order method
for i in range(steps-1):
K[0] = ODE()
K[1] = ODE()
K[2] = ODE()
K[3] = ODE()
vars[:,i+1] = vars[:,i] + 1/6 * (K[0] + 2 * K[1] + 2 * K[2] + K[3])
t[i+1] = t[i] + h
# Plotting every fourth calculation
if (i%4 == 0):
#plt.cla()
plt.plot(t[:i], vars[0,:i],color='black')
plt.title(f'Title (stepsize: {h})')
plt.xlabel('time [s]')
plt.ylabel('position [m]')
plt.tight_layout()
plt.gcf().canvas.draw() #update display window
plt.pause(0.01)
plt.tight_layout()
Plots not showing in Jupyter notebook
If you are working with a Jupyter Notebook then you can add the following line to the top cell where you call all your imports. The following command will render your graph
%matplotlib inline
Related Topics
Best Way to Get the Max Value in a Spark Dataframe Column
Regular Expression: Match Everything After a Particular Word
How to Check If Numbers Are in a List in Python
Python - How to Pad the Output of a MySQL Table
Python: How to Turn CSV Data in to Array
Python: Plotting Percentage in Seaborn Bar Plot
Pandas Filtering for Multiple Substrings in Series
Add Padding to Images to Get Them into the Same Shape
How to Find Number of Ways That the Integers 1,2,3 Can Add Up to N
Check If a Specific Class and Value Exist in HTML Using Beautifulsoup Python
Using Python, How to Access a Shared Folder on Windows Network
How to Remove Parentheses from a String
How to Run External Executable Using Python
Passing a List of Values from Python to the in Clause of an SQL Query
How to Convert Number 1 to a Boolean in Python
How to Check If a String Column in Pyspark Dataframe Is All Numeric
Programme to Print Mulitples of 5 in a Range Specified by User