Histogram given y axis

Viewed 62

I know that plt.hist() method needs x-axis as argument to be plotted. But I have the following situation:

id   cluster_number
 1               10
 2               21
 3               55
 4               36
 5               70
 .                .
 .                .
 .                .

if I use my_serie = df.groupby('cluster_number')['id'].size(), the output would be a Serie with the number of id's in each cluster.

Then, with plt.plot(my_serie), I get a plot with:

  • x-axis: number of clusters...
  • y-axis: with a given number of id's

Of course, this type of data would fit better in a histogram. But plt.hist() will understand the serie given as input as an x-axis values, not y-axis values, as understood by plt.plot():

  • x-axis: number of id's in a cluster...
  • y-axis: and number of clusters with that number of id's read in x-axis

How can I get a histogram with x-axis and y-axis arrangement equal to plt.plot()?

1 Answers

Wouldn't this be a bar plot:

my_serie = df.groupby('cluster_number')['id'].size()
my_serie.plot.bar()
Related