How to offset the x axis and log the y axis and to find the gradient of my graph from an external file

扶醉桌前 提交于 2021-01-29 08:16:02

问题


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:


  1. splitting lines yields strings. convert them to numbers: float(line.split()[0]) (or int() if you have integers)
  2. plt.yscale('log')
  3. x limits should be adjusted automatically. if you want to transform the data you can do x_1 = x_1 + offset or you can fiddle with plt.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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!