Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
6.2k views
in Technique[技术] by (71.8m points)

python - Save data from histogram in text file

Currently I am plotting a histogram using matplotlib.pyplot to plot my data in a histogram. But now I want to save this data in a textfile. Here I want the first column to be the x value and the second to be the y value. How can I do this?

I plot my histogram in the following order:

import matplotlib.pyplot as plt

some_list = [0.42146137, 0.42146137, etc...]


plt.hist(some_list, bins = 900, histtype="step", cumulative=-1)
plt.yscale("log")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Assign plt.hist() to a value, then the first elemnt is the ys, the second is the xs. a = plt.hist(); ys = a[0]; xs = a[1]. Then you can write with any method, like pandas.DataFrame.to_csv(), depending on what structure you want. Keep in mind, the xs are actually boundaries, starting with the lower bound of the first, ending with the upper bound of the last, so the length of xs is one greater than the length of ys.

So for example, if you want just the upper bounds, maybe something like this: pd.DataFrame({'x_upper':a[1][1:], 'y': a[0]}).to_csv(filename)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...