How to create a line plot in python, by importing data from excel and using it to create a plot that shares a common X-Axis?

huangapple go评论98阅读模式
英文:

How to create a line plot in python, by importing data from excel and using it to create a plot that shares a common X-Axis?

问题

尝试在Python Spyder中创建一个图表。我有Excel中的样本数据,可以导入到Spyder中。我想要将一个列('Frequency')作为X轴,将其余的列('C1,C2,C3,C4')绘制在Y轴上。我该怎么做?这是Excel中的数据以及Excel中的图表外观(https://i.stack.imgur.com/eRug5.png),图表和数据

到目前为止,我有这些。下面的这些命令(也在图像中可见)生成一个空图。

  1. data = data.head()
  2. df = pd.DataFrame(data, columns=["Frequency","C1", "C2","C3","C4"])
  3. df.plot(x = "Frequency", y=["C1", "C2","C3","C4"])
英文:

Trying to create a plot using Python Spyder. I have sample data in excel which I am able to import into Spyder, I want one column ('Frequency') to be the X axis, and the rest of the columns ('C1,C2,C3,C4') to be plotted on the Y axis. How do I do this? This is the data in excel and how the plot looks in excel (https://i.stack.imgur.com/eRug5.png) , the plot and data

This is what I have so far . These commands below (Also seen in the image) give an empty plot.

data = data.head()

#data.plot(kind='line', x='Frequency', y=['C1','C2','C3','C4'])

df = pd.DataFrame(data, columns=["Frequency","C1", "C2","C3","C4"])

df.plot(x = "Frequency",y=["C1", "C2","C3","C4"])

答案1

得分: 0

这是一个示例,您可以更改列名:

  1. import seaborn as sns
  2. import matplotlib.pyplot as plt
  3. df = pd.DataFrame({'X轴':[1,3,5,7,10,20],
  4. 'col_2':[.4,.5,.4,.5,.5,.4],
  5. 'col_3':[.7,.8,.9,.4,.2,.3],
  6. 'col_4':[.1,.3,.5,.7,.1,.0],
  7. 'col_5':[.5,.3,.6,.9,.2,.4]})
  8. dfm = df.melt('X轴', var_name='cols', value_name='vals')
  9. g = sns.catplot(x="X轴", y="vals", hue='cols', data=dfm, kind='point')

How to create a line plot in python, by importing data from excel and using it to create a plot that shares a common X-Axis?

英文:

Here is an example, you can change columns names:

  1. import seaborn as sns
  2. import matplotlib.pyplot as plt
  3. df = pd.DataFrame({'X_Axis':[1,3,5,7,10,20],
  4. 'col_2':[.4,.5,.4,.5,.5,.4],
  5. 'col_3':[.7,.8,.9,.4,.2,.3],
  6. 'col_4':[.1,.3,.5,.7,.1,.0],
  7. 'col_5':[.5,.3,.6,.9,.2,.4]})
  8. dfm = df.melt('X_Axis', var_name='cols', value_name='vals')
  9. g = sns.catplot(x="X_Axis", y="vals", hue='cols', data=dfm, kind='point')

How to create a line plot in python, by importing data from excel and using it to create a plot that shares a common X-Axis?

答案2

得分: 0

  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. path = r"C:\Users\Alisha.Walia\Desktop\Alisha\SAMPLE.xlsx"
  4. data = pd.read_excel(path)
  5. dict1 = (data.to_dict()) #print(dict1)
  6. Frequency = data["Frequency "].to_list() #print (Frequency)
  7. C1 = data["C1"].to_list() #print(C1)
  8. C2 = data["C2"].to_list() #print(C2)
  9. C3 = data["C3"].to_list() #print(C3)
  10. C4 = data["C4"].to_list() #print(C4)
  11. plt.plot(Frequency, C1)
  12. plt.plot(Frequency, C2)
  13. plt.plot(Frequency, C3)
  14. plt.plot(Frequency, C4)
  15. plt.style.use('ggplot')
  16. plt.title('SAMPLE')
  17. plt.xlabel('Frequency 20Hz-200MHz')
  18. plt.ylabel('Capacitance pF')
  19. plt.xlim(5, 500)
  20. plt.ylim(-20, 20)
  21. plt.legend()
  22. plt.show()
英文:
  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. path = r"C:\Users\Alisha.Walia\Desktop\Alisha\SAMPLE.xlsx"
  4. data = pd.read_excel(path)
  5. #df = pd.DataFrame.from_dict(data)
  6. #print(df)
  7. #prints out data from excl in tabular format
  8. dict1 = (data.to_dict()) #print(dict1)
  9. Frequency=data["Frequency "].to_list() #print (Frequency)
  10. C1=data["C1"].to_list() #print(C1)
  11. C2=data["C2"].to_list() #print(C2)
  12. C3=data["C3"].to_list() #print(C3)
  13. C4=data["C4"].to_list() #print(C4)
  14. plt.plot(Frequency,C1)
  15. plt.plot(Frequency,C2)
  16. plt.plot(Frequency,C3)
  17. plt.plot(Frequency,C4)
  18. plt.style.use('ggplot')
  19. plt.title('SAMPLE')
  20. plt.xlabel('Frequency 20Hz-200MHz')
  21. plt.ylabel('Capacitance pF')
  22. plt.xlim(5, 500)
  23. plt.ylim(-20,20)
  24. plt.legend()
  25. plt.show()

huangapple
  • 本文由 发表于 2023年2月16日 05:41:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75465704.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定