{ "metadata": { "name": "", "signature": "sha256:3bf58934fdb4ee6f3f19c9f00e520b9a4bca9bd630038bbf8e9df4aedd4b8b5b" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "import vincent\n", "import pandas as pd\n", "import random" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "#Iterable\n", "list_data = [10, 20, 30, 20, 15, 30, 45]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "#Dicts of iterables\n", "cat_1 = ['y1', 'y2', 'y3', 'y4']\n", "index_1 = range(0, 21, 1)\n", "multi_iter1 = {'index': index_1}\n", "for cat in cat_1:\n", " multi_iter1[cat] = [random.randint(10, 100) for x in index_1]\n", "\n", "cat_2 = ['y' + str(x) for x in range(0, 10, 1)]\n", "index_2 = range(1, 21, 1)\n", "multi_iter2 = {'index': index_2}\n", "for cat in cat_2:\n", " multi_iter2[cat] = [random.randint(10, 100) for x in index_2]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "#Pandas\n", "\n", "farm_1 = {'apples': 10, 'berries': 32, 'squash': 21, 'melons': 13, 'corn': 18}\n", "farm_2 = {'apples': 15, 'berries': 43, 'squash': 17, 'melons': 10, 'corn': 22}\n", "farm_3 = {'apples': 6, 'berries': 24, 'squash': 22, 'melons': 16, 'corn': 30}\n", "farm_4 = {'apples': 12, 'berries': 30, 'squash': 15, 'melons': 9, 'corn': 15}\n", "\n", "farm_data = [farm_1, farm_2, farm_3, farm_4]\n", "farm_index = ['Farm 1', 'Farm 2', 'Farm 3', 'Farm 4']\n", "df_farm = pd.DataFrame(farm_data, index=farm_index)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "#As DataFrames\n", "index_3 = multi_iter2.pop('index')\n", "df_1 = pd.DataFrame(multi_iter2, index=index_3)\n", "df_1 = df_1.reindex(columns=sorted(df_1.columns))\n", "\n", "cat_4 = ['Metric_' + str(x) for x in range(0, 10, 1)]\n", "index_4 = ['Data 1', 'Data 2', 'Data 3', 'Data 4']\n", "data_3 = {}\n", "for cat in cat_4:\n", " data_3[cat] = [random.randint(10, 100) for x in index_4]\n", "df_2 = pd.DataFrame(data_3, index=index_4)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "import pandas.io.data as web\n", "import datetime\n", "all_data = {}\n", "date_start = datetime.datetime(2010, 1, 1)\n", "date_end = datetime.datetime(2014, 1, 1)\n", "for ticker in ['AAPL', 'IBM', 'YHOO', 'MSFT']:\n", " all_data[ticker] = web.DataReader(ticker, 'yahoo', date_start, date_end)\n", "price = pd.DataFrame({tic: data['Adj Close']\n", " for tic, data in all_data.items()})" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Load the necessary Javascript libraries." ] }, { "cell_type": "code", "collapsed": false, "input": [ "vincent.initialize_notebook()" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", " " ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the notebook `Chart`s can be displayed by just having the chart variable as the last statement in a cell which will call `chart._repr_html()` internally. You can also use `chart.display()` or `from IPython.display import display; display(chart)`." ] }, { "cell_type": "code", "collapsed": false, "input": [ "bar = vincent.Bar(multi_iter1['y1'])\n", "bar.axis_titles(x='Index', y='Value')\n", "bar.display()" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n", "\n", " " ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "line = vincent.Line(multi_iter1, iter_idx='index')\n", "line.axis_titles(x='Index', y='Value')\n", "line.legend(title='Categories')\n", "line" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n", "\n", " " ], "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "line = vincent.Line(price[['MSFT', 'YHOO']])\n", "line.axis_titles(x='Date', y='Price')\n", "line.legend(title='MSFT vs YHOO')\n", "line" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n", "\n", " " ], "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "" ] } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "scatter = vincent.Scatter(df_1)\n", "scatter.axis_titles(x='Index', y='Data Value')\n", "scatter.legend(title='Categories')\n", "scatter.colors(brew='Set3')\n", "scatter" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n", "\n", " " ], "metadata": {}, "output_type": "pyout", "prompt_number": 11, "text": [ "" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "# Example using DataFrame column for x-axis values\n", "scatter2 = vincent.Scatter(df_1, key_on='y0')\n", "scatter2.axis_titles(x='Column y0', y='Data Value')\n", "scatter2.legend(title='Columns')\n", "scatter2" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n", "\n", " " ], "metadata": {}, "output_type": "pyout", "prompt_number": 12, "text": [ "" ] } ], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "stacked = vincent.StackedArea(df_1)\n", "stacked.axis_titles(x='Index', y='Value')\n", "stacked.legend(title='Categories')\n", "stacked.colors(brew='Spectral')\n", "stacked" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n", "\n", " " ], "metadata": {}, "output_type": "pyout", "prompt_number": 13, "text": [ "" ] } ], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "stacked = vincent.StackedArea(price)\n", "stacked.axis_titles(x='Date', y='Price')\n", "stacked.legend(title='Tech Stocks')\n", "stacked" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n", "\n", " " ], "metadata": {}, "output_type": "pyout", "prompt_number": 14, "text": [ "" ] } ], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": [ "stack = vincent.StackedBar(df_2)\n", "stack.legend(title='Categories')\n", "stack.scales['x'].padding = 0.1\n", "stack" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n", "\n", " " ], "metadata": {}, "output_type": "pyout", "prompt_number": 15, "text": [ "" ] } ], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": [ "stack = vincent.StackedBar(df_farm.T)\n", "stack.axis_titles(x='Total Produce', y='Farms')\n", "stack.legend(title='Produce Types')\n", "stack.colors(brew='Pastel1')\n", "stack" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n", "\n", " " ], "metadata": {}, "output_type": "pyout", "prompt_number": 16, "text": [ "" ] } ], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": [ "group = vincent.GroupedBar(df_2)\n", "group.legend(title='Categories')\n", "group.colors(brew='Spectral')\n", "group.width=750\n", "group" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n", "\n", " " ], "metadata": {}, "output_type": "pyout", "prompt_number": 17, "text": [ "" ] } ], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [ "group = vincent.GroupedBar(df_farm)\n", "group.axis_titles(x='Total Produce', y='Farms')\n", "group.legend(title='Produce Types')\n", "group.colors(brew='Set2')\n", "group" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n", "\n", " " ], "metadata": {}, "output_type": "pyout", "prompt_number": 18, "text": [ "" ] } ], "prompt_number": 18 }, { "cell_type": "code", "collapsed": false, "input": [ "pie = vincent.Pie(farm_1)\n", "pie.legend('Farm 1 Fruit')\n", "pie" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n", "\n", " " ], "metadata": {}, "output_type": "pyout", "prompt_number": 19, "text": [ "" ] } ], "prompt_number": 19 }, { "cell_type": "code", "collapsed": false, "input": [ "donut = vincent.Pie(farm_1, inner_radius=200)\n", "donut.colors(brew=\"Set2\")\n", "donut.legend('Farm 1 Fruit')\n", "donut" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n", "\n", " " ], "metadata": {}, "output_type": "pyout", "prompt_number": 20, "text": [ "" ] } ], "prompt_number": 20 }, { "cell_type": "code", "collapsed": false, "input": [ "import collections\n", "text = 'The sun did not shine It was too wet to play So we sat in the house All that cold cold wet day sat there with Sally we sat there we two And I said How I wish we had something to do Too wet to go out and too cold to play ball So we sat in the house We did nothing at all So all we could do was to Sit Sit Sit Sit And we did not like it Not one little bit And then something went BUMP How that bump made us jump We looked Then we saw him step in on the mat We looked And we saw him The Cat in the Hat And he said to us Why do you sit there like that I know it is wet And the sun is not sunny But we can have lots of good fun that is funny I know some good games we could play Said the cat I know some new tricks Said the Cat in the Hat A lot of good tricks I will show them to you Your mother Will not mind at all if I do Then Sally and I Did not know what to say Our mother was out of the house For the day But the fish said No No Make that cat go away Tell that Cat in the Hat you do NOT want to play He should not be here He should not be about He should not be here When your mother is out Now Now Have no fear Have no fear said the cat My tricks are not bad Said the Cat in the Hat Why we can have lots of good fun if you wish With a game that I call UP UP UP with a fish Put me down said the fish This is no fun at all Put me down said the fish I do NOT wish to fall Have no fear said the cat I will not let you fall I will hold you up high as I stand on a ball With a book on one hand And a cup on my hat But that is not ALL I can do said the cat Look at me Look at me now said the cat With a cup and a cake on the top of my hat I can hold up TWO books I can hold up the fish And a little toy ship And some milk on a dish And look I can hop up and down on the ball But that is not all Oh no That is not all Look at me Look at me Look at me NOW It is fun to have fun But you have to know how I can hold up the cup And the milk and the cake I can hold up these books And the fish on a rake I can hold the toy ship And a little toy man And look With my tail I can hold a red fan I can fan with the fan As I hop on the ball But that is not all Oh no That is not all That is what the cat said Then he fell on his head He came down with a bump from up there on the ball And Sally and I We saw ALL the things fall And our fish came down too He fell into a pot He said Do I like this Oh no I do not This is not a good game Said our fish as he lit No I do not like it Not one little bit Now look what you did Said the fish to the cat Now look at this house Look at this Look at that You sank our toy ship Sank it deep in the cake You shook up our house And you bent our new rake You SHOULD NOT be here when our mother is not You get out of this house Said the fish in the pot But I like it here Oh I like it a lot Said the Cat in the Hat To the fish in the pot I will NOT go away I do NOT wish to go And so said the Cat in the Hat So so so I will show you Another good game that I know And then he ran out And then fast as a fox The Cat in the Hat Came back in with a box A big red wood box It was shut with a hook Now look at this trick Said the cat Take a look Then he got up on top With a tip of his hat I call this game FUN IN A BOX Said the cat In this box are two things I will show to you now You will like these two things Said the cat with a bow I will pick up the hook You will see something new Two things And I call them Thing One and Thing Two These things will not bite you They want to have fun Then out of the box Came Thing Two and Thing One And they ran to us fast They said How do you do Would you like to shake hands With Thing One and Thing Two And Sally and I Did now know what to do So we had to shake hands With Thing One and Thing Two We shook their two hands But our fish said No No Those Things should not be In this house Make them go They should not be here When your mother is not Put them out Put them out Said the fish in the pot Have no fear little fish Said the Cat in the Hat These things are good Things And he gave them a pat They are tame Oh so tame They have come here to play They will give you some fun On this wet wet day Now here is a game that they like Said the cat They like to fly kites Said the Cat in the Hat No Not in the house Said the fish in the pot They should not fly kites In a house They should not Oh the things they will bump Oh the things they will hit Oh I do not like it Not one little bit Then Sally and I Saw them run down the hall We saw those two Things Bump their kites on the wall Bump Thump Thump Bump Down the wall in the hall Thing Two and Thing One They ran up They ran down On the string of one kit We saw Mothers new gown Her gown with the dots That are pink white and red Then we saw one kite bump On the head of her bed Then those Things ran about With big bumps jumps and kicks And with hops and big thumps And all kinds of bad tricks And I said I do NOT like the way that they play If Mother could see this Oh what would she say Then our fish said Look Look And our fish shook with fear Your mother is on her way home Do you hear Oh what will she do to us What will she say Oh she will not like it To find us this way So DO something Fast said the fish Do you hear I saw her Your mother Your mother is near So as fast as you can Think of something to do You will have to get rid of Thing One and Thing Two So as fast as I could I went after my net And I said With my net I can get them I bet I bet with my net I can get those Things yet Then I let down my net It came down the a PLOP And I had them At last Those two Things had to stop Then I said to the cat Now you do as I say You pack up those Things And you take them away Oh dear said the cat You did not like our game Oh dear What shame What a shame What a shame Then he shut up the Things In the box with the hook And the cat went away With a sad kind of look That is good said the fishHe has gone away Yes But your mother will come She will find this big mess And this mess is so big And so deep and so tall we can not pick it up There is no way at all And THEN Who was back in the house Why the cat Have no fear of this mess Said the Cat in the Hat I always pick up all my playthings And so I will show you another good trick that I know Then we saw him pick up all the things that were down He picked up the cake And the rake And the gown And the milk and the strings And the books and the dish And the fan and the cup And the ship and the fish And he put them away Then he said That is that And then he was gone with the tip of his hat Then our mother came in And said said to us two Did you have any fun Tell me What did you do And Sally and I did not know What to say Should we tell her The things that went on there that day She we tell her about it Now what SHOULD we do Well what would YOU do If you mother asked YOU'\n", "counter = collections.Counter()\n", "for w in text.split():\n", " counter[w] += 1\n", "normalize = lambda x: int(x / (max(counter.values()) - min(counter.values())) * 90 + 10)\n", "word_list = {k: normalize(v) for k, v in counter.items()}\n", "word = vincent.Word(word_list)\n", "word" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n", "\n", " " ], "metadata": {}, "output_type": "pyout", "prompt_number": 21, "text": [ "" ] } ], "prompt_number": 21 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }