%matplotlib inline import json import numpy as np import networkx as nx import requests from pattern import web import matplotlib.pyplot as plt # set some nicer defaults for matplotlib from matplotlib import rcParams #these colors come from colorbrewer2.org. Each is an RGB triplet dark2_colors = [(0.10588235294117647, 0.6196078431372549, 0.4666666666666667), (0.8509803921568627, 0.37254901960784315, 0.00784313725490196), (0.4588235294117647, 0.4392156862745098, 0.7019607843137254), (0.9058823529411765, 0.1607843137254902, 0.5411764705882353), (0.4, 0.6509803921568628, 0.11764705882352941), (0.9019607843137255, 0.6705882352941176, 0.00784313725490196), (0.6509803921568628, 0.4627450980392157, 0.11372549019607843), (0.4, 0.4, 0.4)] rcParams['figure.figsize'] = (10, 6) rcParams['figure.dpi'] = 150 rcParams['axes.color_cycle'] = dark2_colors rcParams['lines.linewidth'] = 2 rcParams['axes.grid'] = False rcParams['axes.facecolor'] = 'white' rcParams['font.size'] = 14 rcParams['patch.edgecolor'] = 'none' def remove_border(axes=None, top=False, right=False, left=True, bottom=True): """ Minimize chartjunk by stripping out unnecessary plot borders and axis ticks The top/right/left/bottom keywords toggle whether the corresponding plot border is drawn """ ax = axes or plt.gca() ax.spines['top'].set_visible(top) ax.spines['right'].set_visible(right) ax.spines['left'].set_visible(left) ax.spines['bottom'].set_visible(bottom) #turn off all ticks ax.yaxis.set_ticks_position('none') ax.xaxis.set_ticks_position('none') #now re-enable visibles if top: ax.xaxis.tick_top() if bottom: ax.xaxis.tick_bottom() if left: ax.yaxis.tick_left() if right: ax.yaxis.tick_right() """ Function -------- get_senate_vote Scrapes a single JSON page for a particular Senate vote, given by the vote number Parameters ---------- vote : int The vote number to fetch Returns ------- vote : dict The JSON-decoded dictionary for that vote Examples -------- >>> get_senate_vote(11)['bill'] {u'congress': 113, u'number': 325, u'title': u'A bill to ensure the complete and timely payment of the obligations of the United States Government until May 19, 2013, and for other purposes.', u'type': u'hr'} """ #your code here """ Function -------- get_all_votes Scrapes all the Senate votes from http://www.govtrack.us/data/congress/113/votes/2013, and returns a list of dicts Parameters ----------- None Returns -------- votes : list of dicts List of JSON-parsed dicts for each senate vote """ #Your code here vote_data = get_all_votes() """ Function -------- vote_graph Parameters ---------- data : list of dicts The vote database returned from get_vote_data Returns ------- graph : NetworkX Graph object, with the following properties 1. Each node in the graph is labeled using the `display_name` of a Senator (e.g., 'Lee (R-UT)') 2. Each node has a `color` attribute set to 'r' for Republicans, 'b' for Democrats, and 'k' for Independent/other parties. 3. The edges between two nodes are weighted by the number of times two senators have cast the same Yea or Nay vote 4. Each edge also has a `difference` attribute, which is set to `1 / weight`. Examples -------- >>> graph = vote_graph(vote_data) >>> graph.node['Lee (R-UT)'] {'color': 'r'} # attributes for this senator >>> len(graph['Lee (R-UT)']) # connections to other senators 101 >>> graph['Lee (R-UT)']['Baldwin (D-WI)'] # edge relationship between Lee and Baldwin {'difference': 0.02, 'weight': 50} """ #Your code here votes = vote_graph(vote_data) #this makes sure draw_spring results are the same at each call np.random.seed(1) color = [votes.node[senator]['color'] for senator in votes.nodes()] #determine position of each node using a spring layout pos = nx.spring_layout(votes, iterations=200) #plot the edges nx.draw_networkx_edges(votes, pos, alpha = .05) #plot the nodes nx.draw_networkx_nodes(votes, pos, node_color=color) #draw the labels lbls = nx.draw_networkx_labels(votes, pos, alpha=5, font_size=8) #coordinate information is meaningless here, so let's remove it plt.xticks([]) plt.yticks([]) remove_border(left=False, bottom=False) #Your code here #Your code here #your code here """ Function -------- get_senate_bill Scrape the bill data from a single JSON page, given the bill number Parameters ----------- bill : int Bill number to fetch Returns ------- A dict, parsed from the JSON Examples -------- >>> bill = get_senate_bill(10) >>> bill['sponsor'] {u'district': None, u'name': u'Reid, Harry', u'state': u'NV', u'thomas_id': u'00952', u'title': u'Sen', u'type': u'person'} >>> bill['short_title'] u'Agriculture Reform, Food, and Jobs Act of 2013' """ #your code here """ Function -------- get_all_bills Scrape all Senate bills at http://www.govtrack.us/data/congress/113/bills/s Parameters ---------- None Returns ------- A list of dicts, one for each bill """ #your code here bill_list = get_all_bills() """ Function -------- bill_graph Turn the bill graph data into a NetworkX Digraph Parameters ---------- data : list of dicts The data returned from get_all_bills Returns ------- graph : A NetworkX DiGraph, with the following properties * Each node is a senator. For a label, use the 'name' field from the 'sponsor' and 'cosponsors' dict items * Each edge from A to B is assigned a weight equal to how many bills are sponsored by B and co-sponsored by A """ #Your code here bills = bill_graph(bill_list) #Your code here #Your code here nx.write_gexf(votes, 'votes.gexf') from IPython.display import Image path = 'name_of_your_screenshot' Image(path)