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评论64阅读模式
英文:

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),图表和数据

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

data = data.head()
df = pd.DataFrame(data, columns=["Frequency","C1", "C2","C3","C4"])
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

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

import seaborn as sns
import matplotlib.pyplot as plt

df = pd.DataFrame({'X轴':[1,3,5,7,10,20],
                   'col_2':[.4,.5,.4,.5,.5,.4],
                   'col_3':[.7,.8,.9,.4,.2,.3],
                   'col_4':[.1,.3,.5,.7,.1,.0],
                   'col_5':[.5,.3,.6,.9,.2,.4]})

dfm = df.melt('X轴', var_name='cols', value_name='vals')
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:

import seaborn as sns
import matplotlib.pyplot as plt

df = pd.DataFrame({'X_Axis':[1,3,5,7,10,20],
                   'col_2':[.4,.5,.4,.5,.5,.4],
                   'col_3':[.7,.8,.9,.4,.2,.3],
                   'col_4':[.1,.3,.5,.7,.1,.0],
                   'col_5':[.5,.3,.6,.9,.2,.4]})

dfm = df.melt('X_Axis', var_name='cols', value_name='vals')
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

import pandas as pd
import matplotlib.pyplot as plt

path = r"C:\Users\Alisha.Walia\Desktop\Alisha\SAMPLE.xlsx"
data = pd.read_excel(path)

dict1 = (data.to_dict()) #print(dict1)

Frequency = data["Frequency "].to_list() #print (Frequency)

C1 = data["C1"].to_list() #print(C1)
C2 = data["C2"].to_list() #print(C2)
C3 = data["C3"].to_list() #print(C3)
C4 = data["C4"].to_list() #print(C4)

plt.plot(Frequency, C1)
plt.plot(Frequency, C2)
plt.plot(Frequency, C3)
plt.plot(Frequency, C4)

plt.style.use('ggplot')
plt.title('SAMPLE')
plt.xlabel('Frequency 20Hz-200MHz')
plt.ylabel('Capacitance pF')
plt.xlim(5, 500)
plt.ylim(-20, 20)
plt.legend()

plt.show()
英文:
import pandas as pd
import matplotlib.pyplot as plt


path = r"C:\Users\Alisha.Walia\Desktop\Alisha\SAMPLE.xlsx"
data = pd.read_excel(path)

#df = pd.DataFrame.from_dict(data)
#print(df)
#prints out data from excl in tabular format

dict1 = (data.to_dict()) #print(dict1)


Frequency=data["Frequency "].to_list() #print (Frequency)

C1=data["C1"].to_list() #print(C1)
C2=data["C2"].to_list() #print(C2)
C3=data["C3"].to_list() #print(C3)
C4=data["C4"].to_list() #print(C4)


plt.plot(Frequency,C1)
plt.plot(Frequency,C2)
plt.plot(Frequency,C3)
plt.plot(Frequency,C4)


plt.style.use('ggplot')
plt.title('SAMPLE')
plt.xlabel('Frequency 20Hz-200MHz')
plt.ylabel('Capacitance pF')
plt.xlim(5, 500)
plt.ylim(-20,20)
plt.legend()

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:

确定