Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How matplotlib automatically set x-axis and y-axis limits for bar graph ?
#1
Hi,

I am confused in math/logic behind automatically by-default setting of x-axis and y-axis limits in bar graph ? Here i am sharing some examples

1.
import matplotlib.pyplot as plt
x = [1,2,3,4,5]
y = [1000, 1002, 1001, 1003, 1005]
plt.bar(x,y) 
plt.show()
In this bar graph, X axis limits are 0,1,2,3,4,5 and Y axis limits are 0,200,400,600,800,1000. How is happened??

2.
import matplotlib.pyplot as plt
x=[20,30,90,70, 50, 60, 80, 70]
y=[3,2,5,10, 3, 9, 7, 6]
plt.bar(x,y)
plt.show()

In this bar graph, X axis limits are 0,20,30,40,50,60,70,80,90 and Y axis limits are 0,2,4,6,8,10. How is happened??
Quote
#2
If you look at plt.bar function docs, you find keyword bottom; its default value 0, that means all bars will start at 0 and have
height as defined in the second argument passed to bar (y).

By default, matplotlib chooses axes limits to cover provided range of values. Note, bar function plots filled rectangles of specified heights, so, by default matplotlib chooses axes to cover range from 0 (bottom value) to (bottom + y). Ticks and tick labels are placed in regular manner.

Check out axes.set_xticks, axes.set_xticklabels, axes.set_xlim (you can get current axes instance using plt.gca()) to change this behavior.
Quote
#3
(Jul-03-2019, 11:37 AM)scidam Wrote: If you look at plt.bar function docs, you find keyword bottom; its default value 0, that means all bars will start at 0 and have
height as defined in the second argument passed to bar (y).

By default, matplotlib chooses axes limits to cover provided range of values. Note, bar function plots filled rectangles of specified heights, so, by default matplotlib chooses axes to cover range from 0 (bottom value) to (bottom + y). Ticks and tick labels are placed in regular manner.

Check out axes.set_xticks, axes.set_xticklabels, axes.set_xlim (you can get current axes instance using plt.gca()) to change this behavior.

please explain first example where Y axis limits are 0,200,400,600,800,1000. How these limit/range comes in graph ?
Quote
#4
I didn't explore source code of matplotlib in detail, but in general, computation of values where ytick labels will be shown is performed by a matplotlib.ticker instance. By default, it is ticker.AutoLocator:

from matplotlib.ticker import AutoLocator
aul = AutoLocator()
aul.tick_values(1, 3)
Output:
array([1. , 1.25, 1.5 , 1.75, 2. , 2.25, 2.5 , 2.75, 3. ])
from pylab import *
plt.plot([1,2,3])
plt.show() # yields a graph with ticks placed in above positions
This default behavior is made for convenience, and it could be easily overridden using axes.set_yticks etc. Note, there are minor and major ticks (we are talking about major one here), so underlying machinery is quite tricky.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  IndexError: index 0 is out of bounds for axis 0 with size 0 tmhsa 0 298 Apr-24-2020, 10:00 AM
Last Post: tmhsa
  how to get x values based on y-axis values from curvefit function python_newbie09 1 702 Sep-19-2019, 02:09 AM
Last Post: scidam
  Automatically plot each variable on the same graph for each "ID" AdWill97 0 606 Apr-15-2019, 09:29 AM
Last Post: AdWill97
  How to prevent tick labels overlapping with axis SriRajesh 2 13,687 May-12-2018, 06:13 PM
Last Post: SriRajesh
  Current time on x axis in matplolib Stumpy_L 2 1,757 Feb-07-2018, 02:07 PM
Last Post: Stumpy_L
  Automatically updated graph in jupyter notebook using qt5 capponero 0 2,138 May-16-2017, 03:08 PM
Last Post: capponero

Forum Jump:


Users browsing this thread: 1 Guest(s)