问题
I am new to Python code and I need to graph my experiment data for my lab portfolio however I can't seem to log the y-axis and offset the x-axis or even know where to start for the gradient.
I have tried np.log in various different ways and each time I get an error, for the offset (which 26408) I tried just subtracting that number from x, I have tried creating an array and subtracting that and again I end up with various errors.
import matplotlib.pyplot as plt
%matplotlib inline
f = open("Large C 100ohms","r")
lines = f.readlines()[1:]
x = [line.split()[0] for line in lines]
y = [line.split()[1] for line in lines]
x_1 = np.array(x)
#print(x_1)
y_1 = np.array(y)
#print(y_1)
#offset x axis
#log y axis
plt.plot(x, y,'bo', linestyle='-')
plt.gca().invert_yaxis()
plt.xlabel('Time(ms)')
plt.ylabel('Voltage(V)')
plt.title('100 ohms')
#gradient
plt.show()
f.close()`
回答1:
- splitting lines yields strings. convert them to numbers:
float(line.split()[0])(orint()if you have integers) plt.yscale('log')- x limits should be adjusted automatically. if you want to transform the data you can do
x_1 = x_1 + offsetor you can fiddle withplt.xlim(low, high)
来源:https://stackoverflow.com/questions/65380791/how-to-offset-the-x-axis-and-log-the-y-axis-and-to-find-the-gradient-of-my-graph