In [1]:
import matplotlib.pyplot as plt
import numpy as np

%matplotlib notebook and %matplotlib inline

Matplotlib supports many GUI backends. Plotting within the notebook can be enabled using the %matplotlib magic both with %matplotlib notebook and %matplotlib inline. Each one results in distinct behavior.


In [2]:
%matplotlib notebook

In [3]:
x_values = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y_values = np.sin(x_values)

plt.plot(x_values, y_values)
plt.show()



In [4]:
%matplotlib inline

In [5]:
plt.figure()
x_values = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y_values = np.sin(x_values)

plt.plot(x_values, y_values)
plt.show()



In [6]:
plt.figure()
x_values = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y_values = np.sin(x_values)

plt.plot(x_values, y_values)

plt.xlim(x_values.min()*1.1, x_values.max()*1.1)
plt.ylim(y_values.min()*1.1, y_values.max()*1.1)
plt.xlabel('x values')
plt.ylabel('y values')
plt.show()


styling with matplotlib


In [7]:
from matplotlib import style
style.available


Out[7]:
['dark_background',
 'seaborn-talk',
 'seaborn-deep',
 'seaborn-muted',
 'grayscale',
 'bmh',
 'seaborn-paper',
 'ggplot',
 'seaborn-dark',
 'seaborn-dark-palette',
 'seaborn-darkgrid',
 'seaborn-poster',
 'classic',
 'seaborn-colorblind',
 'seaborn-bright',
 'seaborn-ticks',
 'seaborn-white',
 'fivethirtyeight',
 'seaborn-pastel',
 'seaborn-notebook',
 'seaborn-whitegrid']

In [8]:
style.use('fivethirtyeight')
plt.figure()
x_values = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y_values = np.sin(x_values)

plt.plot(x_values, y_values)

plt.xlim(x_values.min()*1.1, x_values.max()*1.1)
plt.ylim(y_values.min()*1.1, y_values.max()*1.1)
plt.show()


changing the inline backend


In [9]:
%config InlineBackend.figure_format='retina'

In [10]:
plt.figure()
x_values = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y_values = np.sin(x_values)

plt.plot(x_values, y_values)

plt.xlim(x_values.min()*1.1, x_values.max()*1.1)
plt.ylim(y_values.min()*1.1, y_values.max()*1.1)
plt.show()



In [11]:
%config InlineBackend.figure_formats = {'svg',}

In [12]:
plt.figure()
x_values = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y_values = np.sin(x_values)

plt.plot(x_values, y_values)

plt.xlim(x_values.min()*1.1, x_values.max()*1.1)
plt.ylim(y_values.min()*1.1, y_values.max()*1.1)
plt.show()


built-in support for Pandas

As of matplotlib 1.5, pandas dataframes can be plotted by column in plots such as plt.scatter(). To illustrate this capability, let's create a dataframe from a .csv of Pennsylvania fracking data. This dataset is a list of the production of unconventional gas wells for the month of December, 2015.


In [13]:
import pandas as pd

In [15]:
PA_Frack_Wells_Dec = pd.read_csv('gas_data/Oil_Gas_Well_Historical_Production_Report.csv')
PA_Frack_Wells_Dec.head()


Out[15]:
Well_Permit_Num Period_ID Production_Indicator Well_Status Farm_Name Spud_Date Gas_Quantity Gas_Production_Days Condensate_Quantity Condensate_Production_Days ... Well_Municipality Well_Latitude Well_Longitude Unconventional Well_Configuration Home_Use Reporting_Period Comment_Reason Comment_Text Record_Source
0 003-21994 15DECU Yes ACTIVE FAWN DEVELOPERS INC 1 12/05/2008 2134.00 31 NaN NaN ... FAWN 40.657901 -79.781085 Yes VERTI No Dec 2015 (Unconventional wells) NaN NaN OGRE
1 003-22091 15DECU Yes ACTIVE FAWN DEVELOPERS INC 3H 12/11/2009 33817.00 31 NaN NaN ... FAWN 40.648568 -79.777621 Yes HORIZ No Dec 2015 (Unconventional wells) NaN NaN OGRE
2 003-22092 15DECU Yes ACTIVE FAWN DEVELOPERS INC 4H 12/03/2009 15917.00 31 NaN NaN ... FAWN 40.648704 -79.777674 Yes HORIZ No Dec 2015 (Unconventional wells) NaN NaN OGRE
3 003-22230 15DECU Yes ACTIVE BOVARD DOROTHY UNIT 4H 10/30/2012 43000.43 31 NaN NaN ... FAWN 40.634369 -79.753756 Yes HORIZ No Dec 2015 (Unconventional wells) NaN NaN OGRE
4 003-22231 15DECU Yes ACTIVE BOVARD DOROTHY UNIT 5H 10/30/2012 38414.70 31 NaN NaN ... FAWN 40.634453 -79.753761 Yes HORIZ No Dec 2015 (Unconventional wells) NaN NaN OGRE

5 rows × 26 columns


In [18]:
PA_Frack_Wells_Dec.describe()


Out[18]:
Gas_Quantity Gas_Production_Days Condensate_Quantity Condensate_Production_Days Oil_Quantity Oil_Production_Days Well_Latitude Well_Longitude
count 6619.000000 6619.000000 838.000000 838.000000 39.000000 39 9051.000000 9051.000000
mean 62868.259174 29.399154 534.822303 30.392601 180.765385 31 41.055176 -78.182785
std 74075.234077 5.075785 799.959004 3.023965 218.346404 0 0.738610 1.751591
min 0.110000 1.000000 0.100000 1.000000 1.000000 31 39.721901 -80.516255
25% 16829.500000 31.000000 76.605000 31.000000 62.500000 31 40.244028 -80.043979
50% 36150.000000 31.000000 254.855000 31.000000 114.000000 31 41.343683 -77.575103
75% 82817.500000 31.000000 659.280000 31.000000 189.940000 31 41.721817 -76.568217
max 934826.000000 31.000000 7395.000000 31.000000 1013.000000 31 41.997586 -75.555286

We can pass the name of columns to be x and y within plt.scatter as long as we set data equal to the name of the DataFrame which is PA_Frack_Wells_Dec.


In [19]:
style.use('seaborn-notebook')
plt.figure()
plt.scatter(x='Gas_Production_Days',y='Gas_Quantity',data=PA_Frack_Wells_Dec)
plt.xlabel('number of days of gas production')
plt.ylabel('gas production (thousand cubic feet)')
plt.ylim(-20000,PA_Frack_Wells_Dec.Gas_Quantity.max()*1.1)
plt.xlim(0,32)
plt.show()


plotting with Basemap


In [20]:
from mpl_toolkits.basemap import Basemap
style.use('seaborn-notebook')

In [22]:
m = Basemap(projection='mill',
            llcrnrlat = 39,
            llcrnrlon = -83,
            urcrnrlat = 43,
            urcrnrlon = -72,
            resolution='l')

core_x,core_y = m(PA_Frack_Wells_Dec.Well_Longitude.tolist(),PA_Frack_Wells_Dec.Well_Latitude.tolist())

m.drawcoastlines()
m.drawcountries()
m.drawstates()
m.fillcontinents(color='tan',lake_color='lightblue',zorder=0)
m.scatter(x=core_x,y=core_y,c=PA_Frack_Wells_Dec.Gas_Quantity.tolist(),cmap='viridis',vmin=0)
m.colorbar(label='gas quantity (Mcf)')
plt.title('Unconventional gas wells in PA (as of Dec 2015)',)
plt.show()



In [23]:
m = Basemap(projection='mill',
            llcrnrlat = 39,
            llcrnrlon = -83,
            urcrnrlat = 43,
            urcrnrlon = -72,
            resolution='l')

core_x,core_y = m(PA_Frack_Wells_Dec.Well_Longitude.tolist(),PA_Frack_Wells_Dec.Well_Latitude.tolist())

m.drawcoastlines()
m.drawcountries()
m.drawstates()
m.fillcontinents(color='tan',lake_color='lightblue',zorder=0)
m.scatter(x=core_x,y=core_y,c=PA_Frack_Wells_Dec.Gas_Quantity.tolist(),cmap='viridis',vmin=0)
m.readshapefile('./gas_data/marcellus_extent', 'marcellus_extent',color='red',linewidth=1)
m.colorbar(label='gas quantity (Mcf)')
plt.title('Unconventional gas wells in PA (with Marcellus Shale extent)')
plt.show()



In [24]:
high_producing_wells = PA_Frack_Wells_Dec[PA_Frack_Wells_Dec.Gas_Quantity>300000]

m = Basemap(projection='mill',
            llcrnrlat = 39,
            llcrnrlon = -83,
            urcrnrlat = 43,
            urcrnrlon = -72,
            resolution='l')

core_x,core_y = m(high_producing_wells.Well_Longitude.tolist(),high_producing_wells.Well_Latitude.tolist())

m.drawcoastlines()
m.drawcountries()
m.drawstates()
m.fillcontinents(color='tan',lake_color='lightblue',zorder=0)
m.scatter(x=core_x,y=core_y,c=high_producing_wells.Gas_Quantity.tolist(),cmap='viridis',vmin=0)
m.colorbar(label='gas quantity (Mcf)')
plt.title('high producing unconventional gas wells in PA (> 300,000 Mcf)')
plt.show()



In [ ]: