0

I am trying to plot several graphs on the same plot using a pandas dataframe, but I'm getting an error probably because I'm not doing it properly and I can't figure it out. Below is a screenshot of my code (sorry the code formatter tool isn't working): Code

And I keep getting ''ValueError: x and y must be the same size''. Just for reference here is the data I am trying to plot, with the R column on the x axis and the specified columns on the x axis: Dataframe

1
  • 1
    IHMO, scatter only accept one x and one y at a time. Also, you don't need to do SAPT_data=pd.DataFrame(SAPT_data): pd.read_csv returns a DataFrame already. Commented Nov 18, 2020 at 18:45

3 Answers 3

1

Have you tried :

for column in ['exch', 'IND2', 'elst', 'dHf', 'DISP2'] :
    SAPT_DATA.plot(x="R", y=column, kind="scatter")

It should solve your problem since scatter only takes one column for the 'y' argument

EDIT :

If you want to have everything in one plot, with lines between points and special markers, you can use the following arguments for the plot function :

fig, ax = plt.subplots()
for column in ['exch', 'IND2', 'elst', 'dHf', 'DISP2'] :
    SAPT_DATA.plot(x="R", y=column, style='o-', ax=ax)
plt.show()
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, I tried this out and it gave me the plots but it gave me them separately whereas I want them all on the same graph. Also, do you know how I can make all the points in the scatter joined up with a line like how it's done in matplotlib?
If you want a line between them you shouldn't use the scatter argument. Instead, draw a regular plot and add markers on each points by using the following code : fig, ax = plt.subplots() then SAPT_DATA.plot(x='R', y=column, style='o-', ax=ax)
0

Update: Graph using teepee's method

Comments

0

You can only do one value in the y because it's a scatter plot. Intead, you should melt your data frame (df.melt(...)) using "R" as the id_var and the other 5 columns as your value_vars and then use seaborn or altair to plot these:

import seaborn as sns
df = df.melt(id_var='R',
             value_vars=['exch', 'IND2', 'elst', 'dHF', 'DISP2'],
             var_name='energy_type',
             value_name='energy')
sns.relplot(data=df, x='R', y='energy', hue='energy_type')

Side note: you are making a dataframe out of a dataframe in your code, but that doesn't cause an error; just something redundant to eliminate.

2 Comments

Amazing, thanks! I tried this out and it gave me it all on one graph which is what I wanted however I have a few issues (I have posted my graph below as an answer since I can't include images in comments): 1) I assumed 'variable' was free to edit to make whatever variable it is that these points represent, in my case it's energies, but when I changed it to 'energies' it didn't let me. 2) Is there any way I can join the points up with curves going through each of them like in matplotlib line plots?
Yes that can be done using the var_name and value_name arguments in the melt method (I have edited it to reflect that now).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.