0

I am trying to plot a regression fit plot with scatter values and regression fit.

Attached is the excel file with data from dropbox and the desired output graph (made in sigmaplot) https://www.dropbox.com/s/czwq78yyy6vymaj/aniso.xlsx?dl=0

enter image description here

My code:

import matplotlib.pyplot as plt
import pandas as pd
data_x = pd.read_excel('aniso.xlsx', 'Sheet1', parse_cols='B', skiprows=2)
data_36 = pd.read_excel('aniso.xlsx', 'Sheet1', parse_cols='H', skiprows=2)
data_37 = pd.read_excel('aniso.xlsx', 'Sheet1', parse_cols='N')
data_38 = pd.read_excel('aniso.xlsx', 'Sheet1', parse_cols='O')
data_39 = pd.read_excel('aniso.xlsx', 'Sheet1', parse_cols='A')
plt.plot(data_x[2:21], data_36[2:21], 'h', color='#1f77b4', label='protein concentration')
plt.plot(data_37, data_38, color='#d62728', ls='--', label='regression fit')
plt.errorbar(data_x[2:21], data_36[2:21], yerr=data_39[2:21])
plt.show()

When running this code, I get the following error:

File "/usr/local/lib/python2.7/site-packages/matplotlib/axes/_axes.py", line 2931, in xywhere
    assert len(xs) == len(ys)
AssertionError
7
  • 3
    Are you sure that the error comes from the errorbar call, not for instance from the second plot (you don't use slicing there)? Can you post the entire error trace, not just the last line? Commented Aug 24, 2017 at 5:27
  • 2
    You have not been consistent with how you skiprows. Try printing out data_x, data_36 and data_39. You'll see that they're different lengths, and that the headers are being read in incorrectly. Also note that because column B has a 2-line header, it will need to be read in differently to the other columns with only 1-line headers. Also, if you skiprows when you read the data in, you probably don't need to also slice the data between [2:21], since the skiprows should take care of removing the items at the start. I haven't added this as an answer since I haven't tested it out. Commented Aug 24, 2017 at 10:10
  • @tom I made my data consistent and removed the skiprows. When I printed my data, everything looked okay. However, when I tried to plot the graph with the errorbar, I got the following error: File "/usr/local/lib/python2.7/site-packages/matplotlib/axes/_axes.py", line 2964, in extract_err raise ValueError("err must be [ scalar | N, Nx1 " ValueError: err must be [ scalar | N, Nx1 or 2xN array-like ] Commented Aug 25, 2017 at 15:53
  • Try making the errors into a numpy array rather than a data frame? Commented Aug 25, 2017 at 15:57
  • @tom pardom my ignorance, I am not sure what do you mean. The only way I know to read an excel file is by pandas only. Can you please edit this line where I read the data_39 values - data_39 = pd.read_excel('aniso.xlsx', 'Sheet1', parse_cols='A') Commented Aug 25, 2017 at 16:14

1 Answer 1

2

plt.errorbar(x, y) strongly takes float or array-like object. I will suggest:

plt.errorbar(np.array(x), np.array(y))

It worked for me!

Sign up to request clarification or add additional context in comments.

Comments

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.