Reputation: 45
I was trying to create a plot with seaborn, but I faced an error:
*** ValueError: If using all scalar values, you must pass an index
In my program I have read an output file from XFOIL and was trying to plot its results to check it out.
XFOIL File format:
# x Cp
1.00000 0.24276
0.99818 0.23883
0.99591 0.22657
0.99342 0.21421
0.99066 0.20128
0.98759 0.18802
0.98413 0.17434
0.98020 0.16018
0.97569 0.14544
0.97044 0.12999
0.96429 0.11374
0.95703 0.09661
### **(the file is big, and I will not transcript it completely here)** ###
I decided to create a dataframe to enable the plotting process easily.
lst = fl.readlines()
lst_splt = [ s.replace('#','').split() for s in lst]
cp = pd.DataFrame.from_records(lst_splt[1:], columns=lst_splt[:1]).astype(float)
and finally I tried to plot it using seaborn:
sns.lineplot(x='x',y='Cp', data=cp)
but as I said on the beginning of the question, an error appeared:
*** ValueError: If using all scalar values, you must pass an index
What can I do to fix this error?
Upvotes: 0
Views: 730
Reputation: 1404
The problem is related to the columns
argument passed to the DataFrame
constructor.
When printing lst_splt[:1]
, which is the value you pass to the columns
argument, I get this:
print(lst_splt[:1])
# [['x', 'Cp']]
The Dataframe
constructor in this case needs a flat list, not a nested one. The problem is solved when you change lst_splt[:1]
to lst_splt[:1][0]
which when printed gives of course:
print(lst_splt[:1][0])
# ['x', 'Cp']
The modified verrsion of your code below works fine:
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
fl = open('data.txt', 'r')
lst = fl.readlines()
lst_splt = [ s.replace('#','').split() for s in lst]
cp = pd.DataFrame.from_records(lst_splt[1:], columns=lst_splt[:1][0]).astype(float)
sns.lineplot(data=cp, x='x',y='Cp')
plt.show()
Upvotes: 1
Reputation: 2412
Not sure why you are having this error, but you can simply do:
import matplotlib.pyplot as plt
plt.plot(cp["x"], cp["Cp"])
plt.show()
EDIT: After some experimentation, it seems that your method for creating the dataframe is probably the culprit. You can replace it with:
cp = pd.read_csv(filename, sep="\s+", skiprows=2, names=["x", "Cp"])
# Make sure that you have the right value for skiprows (should ignore the header and that's it)
# Then this works:
sns.lineplot(x="x", y="Cp", data=cp)
Upvotes: 2