import pandas as pd
import matplotlib.pyplot as plt
% matplotlib inline
Import the data
df = pd . read_csv ( "../country-gdp-2014.csv" )
df . head ()
Country
Continent
GDP_per_capita
life_expectancy
Population
0
Afghanistan
Asia
663
54.863
22856302
1
Albania
Europe
4195
74.200
3071856
2
Algeria
Africa
5098
68.963
30533827
3
Angola
Africa
2446
45.234
13926373
4
Antigua and Barbuda
N. America
12738
73.544
77656
Render the grid
ax = df . plot ( kind = 'scatter' , x = 'GDP_per_capita' , y = 'life_expectancy' )
# Turn on the grid
ax . grid ()
Customize the grid
You can find all of the linestyle
options in the matplotlib documentation .
You’ll probablyj ust use ':'
for dotted or '--'
for dashed.
If you’re curious about more options, you can also check the documentation for
it .
ax = df . plot ( kind = 'scatter' , x = 'GDP_per_capita' , y = 'life_expectancy' )
# Move the grid to be underneath your data points
ax . set_axisbelow ( True )
# Customize the grid with a specific line style, width, and color.
ax . grid ( linestyle = '-' , linewidth = 1 , color = 'red' )
Render with a larger grid (major grid) and a smaller grid (minor grid)
This allows you to have some grid marks with numbers on it (major) and some
styled differently that don’t have numbers on them (minor grid). This is useful
if you neeed a LOT of detail.
ax = df . plot ( kind = 'scatter' , x = 'GDP_per_capita' , y = 'life_expectancy' )
ax . set_axisbelow ( True )
# Turn on the minor TICKS, which are required for the minor GRID
ax . minorticks_on ()
# Customize the major grid
ax . grid ( which = 'major' , linestyle = '-' , linewidth = 1 , color = 'red' )
# Customize the minor grid
ax . grid ( which = 'minor' , linestyle = ':' , linewidth = 0.5 , color = 'black' )
Turn on the grid but turn off ticks
The problem with minor/major ticks and grid lines is if you look very very very
closely you can see the dotted grid but ALSO you can see little solid ticks
coming up from the bottom or the left of the frame. They’re gross. Let’s turn
them off.
ax = df . plot ( kind = 'scatter' , x = 'GDP_per_capita' , y = 'life_expectancy' )
# Grid goes below your data
ax . set_axisbelow ( True )
# Turn on the minor TICKS, which are required for the minor GRID
ax . minorticks_on ()
# Customize the major grid
ax . grid ( which = 'major' , linestyle = '-' , linewidth = 1 , color = 'red' )
# Customize the minor grid
ax . grid ( which = 'minor' , linestyle = ':' , linewidth = 0.5 , color = 'black' )
# Turn off the display of all ticks.
ax . tick_params ( which = 'both' , # Options for both major and minor ticks
top = False , # turn off top ticks
left = False , # turn off left ticks
right = False , # turn off right ticks
bottom = False ) # turn off bottom ticks