Home > Mobile >  How to plot histogram for a dataframe
How to plot histogram for a dataframe

Time:01-19

I need to plot a row of a dataframe on a histogram so that the x-axes have the number of columns in the x-axes and the ordinates have their values.

An example of my data

data = [1.7,1.8,2.0,3.2]

In the x-axes I would like numbers from one to four and in the ordinates the corresponding values.

like this:

enter image description here, but in the axis from 1 to 4.

Thanks in advance

CodePudding user response:

You can do the following:

import pandas as pd
from matplotlib import pyplot as plt

# put data in dataframe
df = pd.DataFrame([1.7, 1.8, 2.0, 3.2])

# make indexes start at 1
df.index  = 1

# create axes for plot
fig, ax = plt.subplots()

# plot using "bar" rather than histogram
df.plot.bar(
    width=1,  # make sure bars fill their ranges and edges touch
    ax=ax,
    rot=0,  # make sure labels are upright
    fc="none",  # make bars unfilled 
    ec="k",  # make bar edges black "k"
)

# remove legend if you want to
ax.get_legend().remove()

fig.show()
  • Related