Plot Not Showing in Python

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:

enter image description here

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

line

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

enter image description here



Related Topics



Leave a reply



Submit