# 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('_')]))
ch = chartify.Chart()
ch.show()
ch = chartify.Chart()
# Add code here to overwrite the labels
ch.show()
ch = chartify.Chart()
ch.
chartify.Chart?
chartify.Chart
ch = chartify.Chart()
ch.callout.text(# Fill in the code here)
ch.show()
ch.callout.
# List of available callouts:
print_public_methods(ch.callout)
# Avaiable axes methods:
print_public_methods(ch.axes)
ch = chartify.Chart()
ch.callout.text('hi', 10, 10)
# Add code here to modify the xrange to (0, 100)
ch.show()
(chartify.Chart(blank_labels=True)
.callout.text('hi', 10, 10)
.axes.set_xaxis_range(0, 100)
.show()
)
Chartify expects the input data to be:
Below we'll explore some examples of valid and invalid input data
data = chartify.examples.example_data()
data.head()
country
dimension has an observation in each column)pivoted_data = pd.pivot_table(data, columns='country', values='quantity', index='fruit', aggfunc='sum')
pivoted_data
groupby
produces output in tidy format.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()
data.groupby(['country'])['quantity'].sum()
data.groupby(['country'])[['quantity']].sum()
chart_data = data.groupby(['country'])['quantity'].sum().reset_index()
chart_data
linear
by default.chartify.Chart?
ch = chartify.Chart(x_axis_type='datetime',
y_axis_type='linear')
# List of available callouts:
print_public_methods(ch.plot)
ch = chartify.Chart(x_axis_type='categorical',
y_axis_type='linear')
# List of available plots:
print_public_methods(ch.plot)
ch = chartify.Chart(# Your code goes here)
bar_data = (data.groupby('country')[['quantity']].sum()
.reset_index()
)
bar_data
# 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)
# List of available examples
print_public_methods(chartify.examples)
chartify.examples.plot_bar()
grouped_bar_data = (data.groupby(['country', 'fruit'])[['quantity']].sum()
.reset_index()
)
grouped_bar_data
# Implement the grouped bar plot here.
# Look at the example for help if you get stuck.
(chartify.Chart(blank_labels=True)
.set_title("HTML output")
.set_subtitle("Faster, but will not show up in GHE")
.show()
)
(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')
)
categorical
, accent
, sequential
, diverging
.chartify.examples.style_color_palette_categorical()
chartify.examples.style_color_palette_accent()
chartify.examples.style_color_palette_diverging()
chartify.examples.style_color_palette_sequential()
chartify.color_palettes
chartify.color_palettes.show()
.set_color_palette
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()
dark2 = chartify.color_palettes['Dark2']
dark2.show()
sorted_dark2 = dark2.sort_by_hue()
sorted_dark2.show()
dark2.expand_palette(20).show()
shifted_dark2 = dark2.shift_palette('white', percent=20)
shifted_dark2.show()
- Assign the shifted color palette to a chart:
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()
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()
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
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()