2

I have a data set of x,y values in a file called values.txt like so:

1  32432.4323
2  23432.1143
.
.
.
999 1111.23432

I also have a function f(x), 2*x^2+1

I'd like to plot the difference curve between the function and the y values. Something like this:

1  32432.4323 - f(1)
2  23432.1143 - f(2)
.
.
.
999 1111.23432 - f(999)

I can do it by calculating the difference and placing into a separate file values_diff.txt and loading that into matplotlib, but I was wondering if there was a more idiomatic way of doing this in matplotlib that doesn't require the explicit use of a second file,

Jacobi John
  • 293
  • 2
  • 8
  • 1
    There is no need to save in a seperate txt file. You can just plot the obtained difference values against your x values. Meaning, the data that you want to save you can plot it with mpl. – Stijn Van Daele Jan 05 '17 at 22:44
  • Matplotlib is not for that (it's just for plotting). Surely you need to look [numpy](http://www.numpy.org/) – Lucas Jan 05 '17 at 22:45
  • @StijnVanDaele would it be possible to provide a simple example? – Jacobi John Jan 05 '17 at 22:48
  • @lbellomo You can plot functions in matplotlib, and you can plot data, i was hoping you could simply do both without having to resort to other methods. – Jacobi John Jan 05 '17 at 22:49
  • 1
    My answer is an example of how easy it is to waste effort when you don't ask the question author to specify what they have tried themselves. – Bill Bell Jan 06 '17 at 04:18

2 Answers2

2

I'm not averse to writing code.:)

Create one vector for the x-values, one for the y-values, then one for the differences between the y's and the values of the functions. In this code, I've provided dummy values for the missing y-values. Then simply plot the y-values against the x-values first, the difference-values against the x-values next.

import matplotlib.pyplot as plt

f = lambda x: 2*x**2 +1

x = list(range(1,1000))
y = [32432.4323, 23432.1143] +[23432.1132-10*k for k in range(1,997)]+ [1111.23432]

diffs=[]
for an_x, a_y in zip(x,y):
    diffs.append(a_y-f(an_x))

plt.plot(x, y, 'r-')
plt.plot(x, diffs, 'b-')

plt.show()
Bill Bell
  • 21,021
  • 5
  • 43
  • 58
  • thanks for the example, that's what I'm pretty much doing now, I'd hoped there'd be a way to do it without the need to do the diff external to matplotlib, and instead use it's expression interface. – Jacobi John Jan 05 '17 at 23:20
1

I will not write the code for you but this will help you out

First your txt file into pandas: Load data from txt with pandas

Next your do the subtraction by using the apply function: How to apply a function to two columns of Pandas dataframe

and finally you plot them: How to plot two columns of a pandas data frame using points?

Kind regards, Stijn

Community
  • 1
  • 1
Stijn Van Daele
  • 285
  • 1
  • 14
  • 1
    using `apply` is likely unnecessary, and honestly `pandas` is really overkill. Just `numpy` or even vanilla python should work easily enough. – juanpa.arrivillaga Jan 05 '17 at 22:59
  • 1
    I'm currently using a solution like that, I used to the term "2nd file" as a means to enquire about a solution that didn't require a 2nd form of storage. oh well, I guess it's not possible - thanks for the attempt. – Jacobi John Jan 05 '17 at 23:00
  • No, you literally referred to "values_diff.txt". But no, mpl can not do the subtraction for you. – Stijn Van Daele Jan 05 '17 at 23:03