Make histogram from CSV file with python

时间:2019-04-17 01:33:54

标签: python histogram

I have written this code to perform a histogram from a .csv file however I do not get the histogram but as you see in the image

enter image description here

how can I fix it?

import matplotlib.pyplot as plt
import pandas as pd

data = pd.read_csv('test.csv', header=None)

plt.hist(data)

plt.show()

The head lines in the .csv file are:

-95.725
-78.477
-77.976
-77.01
-73.161
-72.505
-71.794
-71.036
-70.653
-70.476
-69.32
-68.787
-68.234
-67.968
-67.742
-67.611
-67.577
-66.69
-66.381
-66.172
-66.072
-65.773
-64.969
-64.897
-64.603

1 个答案:

答案 0 :(得分:0)

I'm not sure if this will work, but try adding the keyword parameters bins='auto', density=True and histtype='step' to the plt.hist function.

For example:

import matplotlib.pyplot as plt
import pandas as pd

data = pd.read_csv('test.csv', header=None)

plt.hist(data, bins='auto', density=True, histtype='step')

plt.show()

What they each do is:

  • bins='auto': Lets numpy automatically decide on the best bin edges;
  • density=True: Sets the area within the histogram to equal 1.0;
  • histtype='bar': Gives the bar style look for the histogram.

This and more can all be found in the matplotlib API.