#!/usr/bin/env python # coding: utf-8 #

Table of Contents

#
# In[ ]: # Copyright (c) 2017-2018 Spotify AB # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import chartify import pandas as pd def print_public_methods(obj): print('Methods:') print('\n'.join([x for x in dir(obj) if not x.startswith('_')])) # # Chart object # - Run the cell below to instantiate a chart and assign to a variable # In[ ]: ch = chartify.Chart() # - Use .show() to render the chart. # In[ ]: ch.show() # - Note that the chart is blank at this point. # - The default labels provide directions for how to override their values. # # Adding chart labels # - __Your turn__: Add labels to the following chart. Look at the default values for instruction. # - Title # - Subtitle # - Source # - X label # - Y label # In[ ]: ch = chartify.Chart() # Add code here to overwrite the labels ch.show() # # Getting help # - From within a jupyter notebook you can see the available attributes of the chart object by pressing "tab" # - Select the space just after the "." character below and hit tab. # In[ ]: ch = chartify.Chart() ch. # - You can also use "?" to pull up documentation for objects and methods. # - Run the cell below to pull up the chartify.Chart documentation # In[ ]: get_ipython().run_line_magic('pinfo', 'chartify.Chart') # - This can also be accomplished by pressing "shift + tab". # - Press "shift + tab" twice to see the expanded documentation. # - Try it with the next cell. # In[ ]: chartify.Chart # # Callouts # - The chart object has a callout object (ch.callout) that contains methods for adding callouts to the chart. # - Callouts can be used to add text, lines, or shaded areas to annotate parts of your chart. # - __Your Turn:__ Fill in the code below to add a text callout that says "hi" at coordinate (10, 10) # - Look up the documentation for ch.callout.text if you need help # In[ ]: ch = chartify.Chart() ch.callout.text(# Fill in the code here) ch.show() # - Use tab below to see what callouts are available. # In[ ]: ch.callout. # - You should see this list of callouts: # In[ ]: # List of available callouts: print_public_methods(ch.callout) # # Axes # - The axes object contains methods for setting or getting axis properties. # In[ ]: # Avaiable axes methods: print_public_methods(ch.axes) # - __Your turn__: modify the chart below so the xaxis range goes from 0 to 100 # In[ ]: ch = chartify.Chart() ch.callout.text('hi', 10, 10) # Add code here to modify the xrange to (0, 100) ch.show() # # Method chaining # - Chart methods can be chained by wrapping the statments in parentheses. See the example below: # In[ ]: (chartify.Chart(blank_labels=True) .callout.text('hi', 10, 10) .axes.set_xaxis_range(0, 100) .show() ) # # Plotting # ## Input data format # Chartify expects the input data to be: # - Tidy (Each variable has its own column, each row corresponds to an observation) # - In the columns of a Pandas DataFrame. # # Below we'll explore some examples of valid and invalid input data # - Run this cell to generate an example dataset # In[ ]: data = chartify.examples.example_data() data.head() # ## Pivoted data: INVALID # - Pivoted data is not Tidy (note the `country` dimension has an observation in each column) # In[ ]: pivoted_data = pd.pivot_table(data, columns='country', values='quantity', index='fruit', aggfunc='sum') pivoted_data # ### Melting pivoted data: VALID # - You can use pandas.melt to convert pivoted data into the tidy data format. # - The output of SQL queries with `groupby` produces output in tidy format. # In[ ]: value_columns = pivoted_data.columns melted_data = pd.melt(pivoted_data.reset_index(), # Need to reset the index to put "fruit" into a column. id_vars='fruit', value_vars=value_columns) melted_data.head() # ## Pandas series: INVALID # - Data in a pandas Series must be converted to a DataFrame for use with Chartify. # In[ ]: data.groupby(['country'])['quantity'].sum() # ## Pandas index: INVALID # - The output below is a pandas DataFrame, but the country dimension is in the Index. # In[ ]: data.groupby(['country'])[['quantity']].sum() # ## Pandas DataFrame: VALID # - The code below produces a valid pandas DataFrame for use with Chartify. # - Notice how the country dimension is now in a column. # In[ ]: chart_data = data.groupby(['country'])['quantity'].sum().reset_index() chart_data # # Axis types # - Specify the x_axis_type and y_axis_type parameters when instantiating the chart object. # - Both are set to `linear` by default. # - Look at the chart object documentation to see the list of available options for x_axis_type and y_axis_type # In[ ]: get_ipython().run_line_magic('pinfo', 'chartify.Chart') # - __The Chart axis types influence the plots that are available__ # - Look at how the plot methods change based on the axis types: # In[ ]: ch = chartify.Chart(x_axis_type='datetime', y_axis_type='linear') # List of available callouts: print_public_methods(ch.plot) # In[ ]: ch = chartify.Chart(x_axis_type='categorical', y_axis_type='linear') # List of available plots: print_public_methods(ch.plot) # - __Your turn__: Create a chart with 'density' y and 'linear' x axis types. What type of plots are available? # In[ ]: ch = chartify.Chart(# Your code goes here) # # Vertical Bar plot # - __Your turn__: Create a bar plot based on the dataframe below. # In[ ]: bar_data = (data.groupby('country')[['quantity']].sum() .reset_index() ) bar_data # In[ ]: # Implement the bar plot here. # Set the appropriate x_axis_type otherwise the bar method won't be available. # Look at the bar documentation to figure out how to pass in the parameters. # If you get stuck move on to the next section for hints. ch = chartify.Chart(# Your code goes here) # # Examples # - Chartify includes many examples. They're a good starting point if you're trying to create a chart that you're unfamiliar with. # In[ ]: # List of available examples print_public_methods(chartify.examples) # - Run the appropriate method to see examples and the corresponding code that generates the example. # In[ ]: chartify.examples.plot_bar() # # Bar plot - Horizontal vs. Vertical # - Copy your bar plot here, but make it horizantal instead of vertical. Look to the example above if you get stuck. # In[ ]: # # Grouped bar plot # - __Your Turn__: Create a grouped bar plot with the data below. # In[ ]: grouped_bar_data = (data.groupby(['country', 'fruit'])[['quantity']].sum() .reset_index() ) grouped_bar_data # In[ ]: # Implement the grouped bar plot here. # Look at the example for help if you get stuck. # # show('html') vs. show('png') # - Chartify charts can be rendered as either "HTML" or "PNG" (HTML is the default) # - HTML output: # - Is faster and good for iteration. # - Can be saved as an image by screenshotting, or by clicking the icon on the top right. # - Will _NOT_ show up in Jupyter notebooks when uploaded to GHE. # - PNG output: # - Is slower and better for the finished product. # - Can be copy and pasted directly from the jupyter notebook (Right click on the image) # - Will show up in Jupyter notebooks when uploaded to GHE. # - See the difference by running the two cells below # In[ ]: (chartify.Chart(blank_labels=True) .set_title("HTML output") .set_subtitle("Faster, but will not show up in GHE") .show() ) # In[ ]: (chartify.Chart(blank_labels=True) .set_title("PNG output") .set_subtitle("Slower, but will show up in GHE. Right click to copy + paste.") .show('png') ) # # Color palette types # - Chartify includes 4 different color palette types: `categorical`, `accent`, `sequential`, `diverging`. # - Note the differences in the examples below # In[ ]: chartify.examples.style_color_palette_categorical() # In[ ]: chartify.examples.style_color_palette_accent() # In[ ]: chartify.examples.style_color_palette_diverging() # In[ ]: chartify.examples.style_color_palette_sequential() # # Color palettes # - Chartify includes a set of pre-defined color palettes: # In[ ]: chartify.color_palettes # - Use .show() to see the colors associated with each: # In[ ]: chartify.color_palettes.show() # - Assign the color palettes with `.set_color_palette` # In[ ]: ch = chartify.Chart(x_axis_type='categorical', blank_labels=True) ch.style.set_color_palette('categorical', 'Dark2') ch.plot.bar(data_frame=grouped_bar_data, categorical_columns=['fruit', 'country'], numeric_column='quantity', color_column='fruit') ch.show() # - Color palette objects include methods for manipulation. See the examples below: # In[ ]: dark2 = chartify.color_palettes['Dark2'] dark2.show() # - Sort # In[ ]: sorted_dark2 = dark2.sort_by_hue() sorted_dark2.show() # - Expand # In[ ]: dark2.expand_palette(20).show() # - Shift # In[ ]: shifted_dark2 = dark2.shift_palette('white', percent=20) shifted_dark2.show() # In[ ]: - Assign the shifted color palette to a chart: # In[ ]: ch = chartify.Chart(x_axis_type='categorical', blank_labels=True) ch.style.set_color_palette('categorical', shifted_dark2) ch.plot.bar(data_frame=grouped_bar_data, categorical_columns=['fruit', 'country'], numeric_column='quantity', color_column='fruit') ch.show() # # Layouts # - Chartify layouts are tailored toward use in slides. # - Notice how the output changes for each of the slide layout options below: # In[ ]: layout_options = ['slide_100%', 'slide_75%', 'slide_50%', 'slide_25%'] for option in layout_options: ch = chartify.Chart(layout=option, blank_labels=True, x_axis_type='categorical') ch.set_title('Layout: {}'.format(option)) ch.plot.bar(data_frame=grouped_bar_data, categorical_columns=['fruit', 'country'], numeric_column='quantity', color_column='fruit') ch.show() # # Advanced usage with Bokeh # - Chartify is built on top of another visualization package called [Bokeh](http://bokeh.pydata.org/en/latest/) # - The example below shows how you can access the Bokeh [figure](https://bokeh.pydata.org/en/latest/docs/reference/plotting.html#bokeh.plotting.figure.Figure) from a Chartify chart object. # In[ ]: ch = chartify.Chart(blank_labels=True, x_axis_type='categorical') ch.plot.bar(data_frame=grouped_bar_data, categorical_columns=['fruit', 'country'], numeric_column='quantity', color_column='fruit') ch.figure # - The following example shows how you can modify attributes not exposed in Chartify by accessing the Bokeh figure. See [Bokeh](http://bokeh.pydata.org/en/latest/) documentation for more details. # In[ ]: ch.figure.xaxis.axis_label_text_font_size = '30pt' ch.figure.xaxis.axis_label_text_color = 'red' ch.figure.height = 400 ch.axes.set_xaxis_label('A large xaxis label') ch.show() # In[ ]: