0

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
4
  • 1
    What is the type and shape of data after read_csv? You get many histograms with 1 entry each, so you probably need to reshape data into a pandas.Series or a 1D numpy array.
    – Colas
    Commented Apr 17, 2019 at 1:38
  • first check data.head() to see what you have.
    – furas
    Commented Apr 17, 2019 at 1:46
  • as for me it can be correct histogram for your data. What did you expect ?
    – furas
    Commented Apr 17, 2019 at 1:48
  • I was expecting a histogram with bars, how can I process my data for this?, for instance: [danielhnyk.cz/fitting-distribution-histogram-using-python/… Commented Apr 17, 2019 at 2:02

1 Answer 1

1

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.

6
  • Hhmm, could you display your pandas DataFrame object? To see what the headers appear like?
    – Aldahunter
    Commented Apr 17, 2019 at 2:13
  • You may need to specify a number for bins, since your widths are very small.
    – Aldahunter
    Commented Apr 17, 2019 at 2:14
  • Just saw that you do actually want it to appear like a bar plot, you should try changing histtype to histtype='bar'. If we can get the overall shape working, then we can change the face- and edge-colors easily. I'm saying this since the problem appears to the distribution of your data.
    – Aldahunter
    Commented Apr 17, 2019 at 2:17
  • DataFrame is: <class 'pandas.core.frame.DataFrame'>. How to process my data ? histtype doesn't work for me. Commented Apr 17, 2019 at 2:25
  • Okay, so the data frame is not the problem. If histtype is not working you should just be able to remove it as an argument since it defaults to 'bar' anyway. Try setting bins=10 to see if that gives something similar to what you had in mind.
    – Aldahunter
    Commented Apr 17, 2019 at 2:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.