Created
June 12, 2015 15:48
-
-
Save joferkington/428b2b36e3c485f3ef24 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import matplotlib.pyplot as plt | |
from mpldatacursor import datacursor | |
def main(): | |
accounts = generate_accounts() | |
lookup = plot(accounts) | |
datacursor(formatter=Formatter(lookup), bbox=dict(alpha=1)) | |
plt.show() | |
def plot(accounts): | |
lookup = {} | |
fig, ax = plt.subplots() | |
for i, account in enumerate(accounts): | |
artist, = ax.bar([i], [account.balance], align='center') | |
lookup[artist] = account | |
ax.set(xticks=[], ylabel='Balance ($)') | |
ax.grid(axis='y') | |
return lookup | |
def generate_accounts(): | |
names = ['Mark', 'Nancy', 'Sam', 'John', 'Patricia', 'Anna'] | |
accounts = [random.randint(100000, 9999999) for _ in names] | |
balances = [random.randint(-1000, 10000) for _ in names] | |
return [BankAccount(*item) for item in zip(names, accounts, balances)] | |
class BankAccount: | |
def __init__(self, owner, number, balance): | |
self.owner, self.number, self.balance = owner, number, balance | |
class Formatter: | |
def __init__(self, account_lookup): | |
self.account_lookup = account_lookup | |
def __call__(self, **kwargs): | |
artist = kwargs['event'].artist | |
account = self.account_lookup[artist] | |
template = 'Owner: {}\nAccount: {}\nBalance: {}' | |
return template.format(account.owner, account.number, account.balance) | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment