user8173426
user8173426

Reputation: 1

issue with python plot

So with this code I need to plot an IV-curve exponentially decaying, but it is in wrong direction and needs to be mirrored/flipped. The x andy values are not being plotted in the correct axes and needs to be switched. It would show the relation with current exponentially decreasing while given a voltage.I tried all sorts of debugging, but it kept showing an exponential growth or the same kind of decay.

import matplotlib.pyplot as plt
import numpy as np
xdata=np.linspace(23,0)# voltage data
ydata=np.exp(xdata)# current data
plt.plot(ydata,xdata)
plt.title(r'IV-curve')
plt.xlabel('Voltage(V)')
plt.ylabel('Current(I)')
plt.show()

Here's what it looks like: https://i.sstatic.net/27Imw.jpg

Also, bear with me as this may seem like a trivial code, but I literally started coding for the first time last week, so I will get some bumps on the road :)

Upvotes: 0

Views: 123

Answers (2)

seralouk
seralouk

Reputation: 33147

The problem is that the ydata that you use are not correctly ordered.

The solution is simple. Reorder the ydata.

Do this:

import matplotlib.pyplot as plt
import numpy as np

xdata = np.linspace(23,0)# voltage data
ydata = np.exp(xdata)# current data
ydata = np.sort(ydata)

plt.plot(ydata,xdata)
plt.title(r'IV-curve')
plt.xlabel('Voltage(V)')
plt.ylabel('Current(I)')

plt.show()

Result:

enter image description here

Upvotes: 1

bob.sacamento
bob.sacamento

Reputation: 6651

It looks like maybe

plt.plot(ydata,xdata)

should be

plt.plot(xdata,ydata)

This will correct the axes. But you still aren't going to get a decaying exponential. Why? Not because of the plotting but because of your data. Your data is a growing exponential. If you want decay use something like

ydata=np.exp(-xdata)

i.e. minus sign in front of xdata.

Upvotes: 0

Related Questions