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

Categories

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")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
5.8k views
Welcome To Ask or Share your Answers For Others

1 Answer

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...