21

I am using Pandas histogram.

I would like to set the y-axis range of the plot.

Here is the context:

import matplotlib.pyplot as plt
%matplotlib inline

interesting_columns = ['Level', 'Group']

for column in interesting_columns:
    data['ranking'].hist(by=data[column], normed=True)

There is a range argument that can filter x-values, but I am unaware of the y equivalent:

hist(by=[column], normed=True, range=[0, 1]) #working argument
hist(by=[column], normed=True, y_range=[0, 1]) #hypothetical argument

I've read a lot of different methods for changing plot ranges using plt attributes. They do not seem to work in a loop and for subplots.

I am struggling to grasp the right way to approach this problem.

mrmagicfluffyman
  • 365
  • 1
  • 2
  • 7
  • have you tried assigning a handle to `hist` like: `h1=hist(...)` and then looking at what methods does `h1.*` have? It probably has something like `h1.set_ylim` or something similar – Sleepyhead Jul 17 '16 at 19:55

2 Answers2

44

If you use

data['ranking'].plot.hist(ylim=(0,1)) 

(mind the .plot in the syntax!) it should work.

Davide Fiocco
  • 5,350
  • 5
  • 35
  • 72
WilliamEllisWebb
  • 922
  • 2
  • 10
  • 17
4

You can simply add option sharey=True to make all subplots share the same yaxis limit, similiary using sharex=True for xaxis

data['ranking'].hist(by=data[column], sharey=True)

Xiao Huang
  • 51
  • 3
  • 1
    Code-only answers are considered low quality: make sure to provide an explanation what your code does and how it solves the problem. It will help the asker and future readers both if you can add more information in your post. See [Explaining entirely code-based answers](https://meta.stackexchange.com/questions/114762/) – Calos Mar 12 '20 at 01:03
  • This makes all histograms have the same y-axis, but how do you limit their height? – giusti Apr 24 '21 at 18:50
  • @giusti When all histograms share the same y-axis, the limit of y-axis is determined by the range of y values of all data points. One way (may not be the best) to limit the height is to remove the data points that's outside the y range beforehand. – Xiao Huang Dec 08 '21 at 01:58