Making Plots Look Good


In [1]:
import pandas
import matplotlib
from matplotlib import pyplot
%matplotlib inline

###  Reading in data
data = pandas.read_csv('states_data.csv')
print(data.head(7))


          name  population_million  electoral_votes party_2016
0      Alabama            4.887870                9          R
1       Alaska            0.737438                3          R
2      Arizona            7.171650               11          R
3     Arkansas            3.013830                6          R
4   California           39.557000               55          D
5     Colorado            5.695560                9          D
6  Connecticut            3.572670                7          D

In [2]:
pop = data['population_million']
votes = data['electoral_votes']
dems = (data['party_2016'] == 'D')
reps = (data['party_2016'] == 'R')

figure, ax = pyplot.subplots()

points1 = pyplot.plot(pop[reps], (votes/pop)[reps], ls='', marker='o', ms=8, mfc='red')
points2 = pyplot.plot(pop[dems], (votes/pop)[dems], ls='', marker='o', ms=8, mfc='blue')


Figure Size


In [3]:
figure, ax = pyplot.subplots(figsize=(3, 2))

points1 = pyplot.plot(pop[reps], (votes/pop)[reps], ls='', marker='o', ms=8, mfc='red')
points2 = pyplot.plot(pop[dems], (votes/pop)[dems], ls='', marker='o', ms=8, mfc='blue')


Title and Axis Labels


In [4]:
figure, ax = pyplot.subplots()

points1 = pyplot.plot(pop[reps], (votes/pop)[reps], ls='', marker='o', ms=8, mfc='red')
points2 = pyplot.plot(pop[dems], (votes/pop)[dems], ls='', marker='o', ms=8, mfc='blue')

###  set title and axis labels
ax.set_title('Electoral votes by state in the USA', size=16)
ax.set_xlabel('Population (millions)', size=16)
ax.set_ylabel('Electoral Votes per million', size=16)


Out[4]:
<matplotlib.text.Text at 0x113080e10>

Tick Label Size

You can adjust the size of the numbers printed on the axes.


In [5]:
figure, ax = pyplot.subplots()

points1 = pyplot.plot(pop[reps], (votes/pop)[reps], ls='', marker='o', ms=8, mfc='red')
points2 = pyplot.plot(pop[dems], (votes/pop)[dems], ls='', marker='o', ms=8, mfc='blue')

###  set title and axis labels
ax.set_title('Electoral votes by state in the USA', size=15)
ax.set_xlabel('Population (millions)', size=15)
ax.set_ylabel('Electoral Votes per million', size=15)

###  set axis labelsize
ax.xaxis.set_tick_params(labelsize=14)
ax.yaxis.set_tick_params(labelsize=14)


Axis Range and Logarithmic Scale

Ways to modify the axes to help with viewing data.


In [6]:
figure, ax = pyplot.subplots()

points1 = pyplot.plot(pop[reps], (votes/pop)[reps], ls='', marker='o', ms=8, mfc='red')
points2 = pyplot.plot(pop[dems], (votes/pop)[dems], ls='', marker='o', ms=8, mfc='blue')

###  set title and axis labels
ax.set_title('Electoral votes by state in the USA', size=15)
ax.set_xlabel('Population (millions)', size=15)
ax.set_ylabel('Electoral Votes per million', size=15)

###  set axis labelsize
ax.xaxis.set_tick_params(labelsize=14)
ax.yaxis.set_tick_params(labelsize=14)

###  set x-axis to logarithmic scale
ax.set_xscale('log')

###  set tick values and labels
ax.xaxis.set_ticks([0.1, 1, 10, 100])
ax.xaxis.set_ticklabels(['0.1', '1', '10', '100'])
ax.xaxis.set_ticklabels(['point one', 'one', 'ten', 'one hundred'])

###  set axis range
ax.set_ylim(0, 6)


Out[6]:
(0, 6)

Minor Ticks and Grids


In [7]:
figure, ax = pyplot.subplots()

points1 = pyplot.plot(pop[reps], (votes/pop)[reps], ls='', marker='o', ms=8, mfc='red')
points2 = pyplot.plot(pop[dems], (votes/pop)[dems], ls='', marker='o', ms=8, mfc='blue')

###  set title and axis labels
ax.set_title('Electoral votes by state in the USA', size=15)
ax.set_xlabel('Population (millions)', size=15)
ax.set_ylabel('Electoral Votes per million', size=15)

###  set axis labelsize
ax.xaxis.set_tick_params(labelsize=14)
ax.yaxis.set_tick_params(labelsize=14)

###  set x-axis to logarithmic scale
ax.set_xscale('log')

###  set tick values and labels
ax.xaxis.set_ticks([0.1, 1, 10, 100])
ax.xaxis.set_ticklabels(['0.1', '1', '10', '100'])

###  set axis range
ax.set_ylim(0, 6)

###  add minor ticks
ax.minorticks_on()

###  add a grid
ax.grid()



In [ ]:


In [ ]:


In [ ]:

matplotlibrc (set plotting defaults)


In [8]:
import pandas
import matplotlib
from matplotlib import pyplot
%matplotlib inline
matplotlib.rc_file('./rcfile_dir/matplotlibrc_atomczak')

###  Reading in data
data = pandas.read_csv('states_data.csv')
pop = data['population_million']
votes = data['electoral_votes']
dems = data['party_2016'] == 'D'
reps = data['party_2016'] == 'R'

figure, ax = pyplot.subplots()

points1 = pyplot.plot(pop[reps], (votes/pop)[reps], ls='', marker='o', ms=8, mfc='red')
points2 = pyplot.plot(pop[dems], (votes/pop)[dems], ls='', marker='o', ms=8, mfc='blue')

###  set title and axis labels
ax.set_title('Electoral votes by state in the USA')
ax.set_xlabel('Population (millions)')
ax.set_ylabel('Electoral Votes per million')

ax.set_xscale('log')



In [ ]: