def _bar_stacked_example_1(quantity_by_fruit_and_country): """Docstring""" # Plot the data (chartify.Chart( blank_labels=True, x_axis_type='categorical').set_title("Stacked bar chart").set_subtitle( "Stack columns by a categorical factor.").plot.bar_stacked( data_frame=quantity_by_fruit_and_country, categorical_columns=['fruit'], numeric_column='quantity', stack_column='country', normalize=False).show(_OUTPUT_FORMAT))
def test_multi_series_zero_baseline_unstacked_missing(self): """Area plot tests""" test_data = self.data_missing ch = chartify.Chart() ch.plot.area(test_data, 'x', 'upper', color_column='category') assert (np.array_equal(chart_data(ch, 'a')['x'], [1, 2, 3, 3, 2, 1])) assert (np.array_equal( chart_data(ch, 'a')['upper'], [20., 30., 40., 0., 0., 0.])) assert (np.array_equal(chart_data(ch, 'c')['x'], [1, 2, 3, 3, 2, 1])) assert (np.array_equal( chart_data(ch, 'c')['upper'], [0., 0., 4., 0., 0., 0.]))
def _bar_grouped_example_1(quantity_by_fruit_and_country): """Docstring""" # Plot the data ch = chartify.Chart(blank_labels=True, x_axis_type='categorical') ch.set_title("Grouped bar chart") ch.set_subtitle( "Pass a list to group by multiple factors. Color grouped by 'fruit'") ch.plot.bar(data_frame=quantity_by_fruit_and_country, categorical_columns=['fruit', 'country'], numeric_column='quantity', color_column='fruit') ch.show(_OUTPUT_FORMAT)
def _bar_grouped_example_2(quantity_by_fruit_and_country): """Docstring""" ch = chartify.Chart(blank_labels=True, x_axis_type='categorical') ch.set_title("Grouped bar chart - Color groupings") ch.set_subtitle( "Change color independent of the axis factors. Color grouped by 'country'" ) ch.plot.bar(data_frame=quantity_by_fruit_and_country, categorical_columns=['fruit', 'country'], numeric_column='quantity', color_column='country') ch.show(_OUTPUT_FORMAT)
def _area_example_1(total_quantity_by_month_and_fruit): """# Stacked area""" # Plot the data ch = chartify.Chart(blank_labels=True, x_axis_type='datetime') ch.set_title("Stacked area") ch.set_subtitle("Represent changes in distribution.") ch.plot.area(data_frame=total_quantity_by_month_and_fruit, x_column='month', y_column='quantity', color_column='fruit', stacked=True) ch.show(_OUTPUT_FORMAT)
def _interval_example2(avg_price_with_interval): """# Plot the data ordered by the numerical axis""" ch = chartify.Chart(blank_labels=True, x_axis_type='categorical') ch.set_title("Combined interval plot & bar plot") ch.plot.bar(data_frame=avg_price_with_interval, categorical_columns='fruit', numeric_column='mean') ch.plot.interval(data_frame=avg_price_with_interval, categorical_columns='fruit', lower_bound_column='lower_ci', upper_bound_column='upper_ci') ch.show(_OUTPUT_FORMAT)
def _scatter_example_2(data): """# Scatter with size""" # Plot the data ch = chartify.Chart(blank_labels=True, x_axis_type='datetime') ch.plot.scatter(data_frame=data, x_column='date', y_column='unit_price', size_column='quantity') ch.set_title("Scatterplot") ch.set_subtitle( "Optional 'size_column' argument for changing scatter size.") ch.show(_OUTPUT_FORMAT)
def _scatter_example_3(data): """# Scatter with size and color""" # Plot the data ch = chartify.Chart(blank_labels=True, x_axis_type='datetime') ch.plot.scatter(data_frame=data, x_column='date', y_column='unit_price', size_column='quantity', color_column='fruit') ch.set_title("Scatterplot") ch.set_subtitle("Optional 'color_column' argument for grouping by color.") ch.show(_OUTPUT_FORMAT)
def _line_example_1(price_by_date): """# Line with datetime x axis""" # Plot the data ch = chartify.Chart(blank_labels=True, x_axis_type='datetime') ch.set_title("Line charts") ch.set_subtitle("Plot two numeric values connected by an ordered line.") ch.plot.line( # Data must be sorted by x column data_frame=price_by_date.sort_values('date'), x_column='date', y_column='total_price') ch.show(_OUTPUT_FORMAT)
def test_multi_numeric_line(self): """Single line test""" ch = chartify.Chart() ch.plot.line( self.data, x_column='number1', y_column='number2', color_column='category1') assert (np.array_equal(chart_data(ch, 'a')['number1'], [1., 2., 3.])) assert (np.array_equal(chart_data(ch, 'a')['number2'], [5, 10, 0])) assert (np.array_equal(chart_data(ch, 'b')['number1'], [1., 2., 3.])) assert (np.array_equal(chart_data(ch, 'b')['number2'], [4, -3, -10]))
def test_lollipop_color_column(self): sliced_data = self.data[self.data['category1'] == 'a'] ch = chartify.Chart(x_axis_type='categorical') ch.plot.lollipop( sliced_data, categorical_columns='category2', numeric_column='number', color_column='category2') assert (np.array_equal( chart_color_mapper(ch).factors, ['1', '2', '3'])) assert (np.array_equal( chart_color_mapper(ch).palette, ['#1f77b4', '#ff7f0e', '#2ca02c']))
def test_single_numeric_text(self): """Single line test""" single_text = self.data[self.data['text'] == 'a'] ch = chartify.Chart() ch.plot.text( single_text, x_column='number1', y_column='number2', text_column='text') assert (np.array_equal(chart_data(ch, '')['number1'], [1., 2., 3.])) assert (np.array_equal(chart_data(ch, '')['number2'], [5, 10, 0])) assert (np.array_equal(chart_data(ch, '')['text'], ['a', 'a', 'a']))
def test_heatmap(self): """Area plot tests""" ch = chartify.Chart(x_axis_type='categorical', y_axis_type='categorical') ch.plot.heatmap(self.data, x_column='category1', y_column='category2', color_column='values') assert (np.array_equal( chart_data(ch, '')['category1'], ['a', 'a', 'b', 'b'])) assert (np.array_equal( chart_data(ch, '')['category2'], ['1', '2', '1', '2'])) assert (np.array_equal(chart_data(ch, '')['values'], [1., 2., 3., 4.]))
def test_single_datetime_line(self): """Single line test""" data = pd.DataFrame({ 'number': [1, 10, -10, 0], 'datetimes': ['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04'], }) ch = chartify.Chart(x_axis_type='datetime') ch.plot.line(data, x_column='datetimes', y_column='number') assert (np.array_equal( chart_data(ch, '')['datetimes'], pd.date_range('2017-01-01', '2017-01-04'))) assert (np.array_equal(chart_data(ch, '')['number'], [1, 10, -10, 0]))
def test_horizontal(self): """Horizontal test""" sliced_data = self.data[self.data['category1'] == 'a'] for method_name in self.plot_methods: ch = chartify.Chart(y_axis_type='categorical') plot_method = getattr(ch.plot, method_name) plot_method( sliced_data, categorical_columns='category2', numeric_column='number') assert (np.array_equal( chart_data(ch, '')['factors'], ['2', '1', '3'])) assert (np.array_equal(chart_data(ch, '')['number'], [10, 5, 0]))
def display_layout(layout): (chartify.Chart( layout=layout) # Assign the layout when instantiating the chart. .plot.scatter( data_frame=data, x_column='Price', y_column='Demand') .set_title("Slide layout: '{}'".format(layout)) .set_subtitle("Demand vs. Price.") .set_source_label("") .axes.set_xaxis_label("Demand (# Users)") .axes.set_yaxis_label("Price ($)") .show(_OUTPUT_FORMAT))
def test_multi_series_zero_baseline_stacked(self): """Area plot tests""" test_data = self.data ch = chartify.Chart() ch.plot.area( test_data, 'x', 'upper', color_column='category', stacked=True) assert (np.array_equal(chart_data(ch, 'a')['x'], [1, 2, 2, 1])) assert (np.array_equal( chart_data(ch, 'a')['upper'], [20., 30., 0., 0.])) assert (np.array_equal(chart_data(ch, 'b')['x'], [1, 2, 2, 1])) assert (np.array_equal( chart_data(ch, 'b')['upper'], [22., 33., 30., 20.]))
def _bar_grouped_example_4(quantity_by_fruit_and_country): """Docstring""" ch = chartify.Chart(blank_labels=True, x_axis_type='categorical') ch.set_title("Grouped bar chart - Factor order") ch.set_subtitle("Change categorical order with 'categorical_order_by'.") ch.plot.bar(data_frame=quantity_by_fruit_and_country, categorical_columns=['country', 'fruit'], numeric_column='quantity', color_column='country', categorical_order_by='labels', categorical_order_ascending=True) ch.axes.set_xaxis_tick_orientation('vertical') ch.show(_OUTPUT_FORMAT)
def _bar_grouped_example_3(quantity_by_fruit_and_country): """Docstring""" ch = chartify.Chart(blank_labels=True, x_axis_type='categorical') ch.set_title("Grouped bar chart - Group hierarchy order") ch.set_subtitle( "Change chage order of 'categorical_column' list to switch grouping hierarchy." ) ch.plot.bar(data_frame=quantity_by_fruit_and_country, categorical_columns=['country', 'fruit'], numeric_column='quantity', color_column='country') ch.axes.set_xaxis_tick_orientation('vertical') ch.show(_OUTPUT_FORMAT)
def _interval_example(avg_price_with_interval): """# Plot the data ordered by the numerical axis""" ch = chartify.Chart(blank_labels=True, x_axis_type='categorical') ch.set_title("Interval plots") ch.set_subtitle( "Represent variation. Optional `middle_column` to mark a middle point." ) ch.plot.interval(data_frame=avg_price_with_interval, categorical_columns='fruit', lower_bound_column='lower_ci', upper_bound_column='upper_ci', middle_column='mean') ch.show(_OUTPUT_FORMAT)
def test_categorical_axis_type_casting(): """Categorical axis plotting breaks for non-str types. Test that type casting is performed correctly""" test_df = pd.DataFrame({ 'categorical_strings': ['a', 'a', 'c', 'd'], 'categorical_integers': [1, 2, 3, 3], 'categorical_floats': [1.1, 2.1, 3.3, 4.4], 'numeric': [1, 2, 3, 4] }) # Multi factor test ch = chartify.Chart(blank_labels=True, x_axis_type='categorical') ch.plot.bar( test_df, categorical_columns=['categorical_strings', 'categorical_integers'], numeric_column='numeric') # Single factor test ch = chartify.Chart(blank_labels=True, y_axis_type='categorical') ch.plot.bar(test_df, 'categorical_floats', 'numeric') # Stacked bar test ch = chartify.Chart(blank_labels=True, x_axis_type='categorical') ch.plot.bar_stacked(test_df, 'categorical_strings', 'numeric', 'categorical_floats') # Color test ch = chartify.Chart(blank_labels=True, x_axis_type='categorical') ch.plot.bar( test_df, categorical_columns=['categorical_strings', 'categorical_integers'], numeric_column='numeric', color_column='categorical_integers') # Test that original data frame dtypes are not overwritten. assert (test_df.dtypes['categorical_integers'] == 'int64') assert (test_df.dtypes['categorical_floats'] == 'float64')
def _categorical_example_2(data): """Docstring""" # Plot the data ch = chartify.Chart(blank_labels=True) ch.style.set_color_palette(palette_type='categorical', ) ch.plot.scatter(data_frame=data, x_column='x', y_column='y', color_column='country') ch.set_title( "Pass 'palette' parameter to .set_color_palette() to change palette colors." ) ch.set_subtitle("") ch.show(_OUTPUT_FORMAT)
def test_set_legend_location(self): data = chartify.examples.example_data() ch = chartify.Chart(x_axis_type='datetime') ch.plot.scatter(data_frame=data, x_column='date', y_column='unit_price', size_column='quantity', color_column='fruit') ch.set_legend_location('top_right') assert (ch.figure.legend[0].location == 'top_right') assert (ch.figure.legend[0].orientation == 'horizontal') ch.set_legend_location('top_left', 'vertical') assert (ch.figure.legend[0].location == 'top_left') assert (ch.figure.legend[0].orientation == 'vertical') ch.set_legend_location('outside_bottom') ch = chartify.Chart(x_axis_type='datetime') ch.plot.scatter(data_frame=data, x_column='date', y_column='unit_price', size_column='quantity', color_column='fruit') ch.set_legend_location(None) assert (ch.figure.legend[0].visible is False)
def main(): ''' This will load data from the Alaska State Department of Health and Human Services and parse it into a standard format CSV ''' filter_region = ['Statewide'] df=pdf.read_csv('./data/parsed.csv') regions= ~df['Economic_Region'].isin(filter_region) ch = chartify.Chart(x_axis_type='datetime', y_axis_type='linear') ch.plot.scatter(data_frame=df_cases.loc[regions], x_column='Date_', y_column='daily_cases', color_column='Economic_Region',) ch.show()
def setup(self): data = chartify.examples.example_data() data = data.sort_values(['date', 'fruit']) color_order = data['fruit'].unique() ch = chartify.Chart(x_axis_type='datetime') ch.plot.scatter(data_frame=data, x_column='date', y_column='unit_price', size_column='quantity', color_column='fruit', color_order=color_order) self.chart = ch self.data = data self.color_order = color_order
def _kde_example2(data): """# Parallel with datetime x axis""" # Plot the data ch = chartify.Chart(blank_labels=True, y_axis_type='density') ch.set_title("KDE plot + Histogram") ch.plot.kde(data_frame=data, values_column='unit_price', color_column='fruit') ch.style.color_palette.reset_palette_order() ch.plot.histogram(data_frame=data, values_column='unit_price', color_column='fruit', method='density') ch.show(_OUTPUT_FORMAT)
def test_multi_numeric_text(self): """Single line test""" ch = chartify.Chart() ch.plot.text(self.data, x_column='number1', y_column='number2', text_column='text', color_column='text') assert (np.array_equal(chart_data(ch, 'a')['number1'], [1., 2., 3.])) assert (np.array_equal(chart_data(ch, 'a')['number2'], [5, 10, 0])) assert (np.array_equal(chart_data(ch, 'a')['text'], ['a', 'a', 'a'])) assert (np.array_equal(chart_data(ch, 'b')['number1'], [1., 2., 3.])) assert (np.array_equal(chart_data(ch, 'b')['number2'], [4, -3, -10])) assert (np.array_equal(chart_data(ch, 'b')['text'], ['b', 'b', 'b']))
def plotPublisher1000(fname='publishers_count.csv'): ''' Author: Joshua Williams Makes a plot of the number of games that belong to each developer in the top 1000 ranking of our Dataset :fname (str): The name of the file that contains the dataframe [must be a .csv file] ''' import pandas as pd import chartify as ch assert isinstance(fname, str) and '.csv' in fname, "Input is not str name of a .csv file!" # assert isinstance(fname2, str) and '.csv' in fname2, "Input is not str name of a .csv file!" data = pd.read_csv(fname) # data2 = pd.read_csd(fname2) data = pd.DataFrame({'publisher': list(data.columns.values[1:11]), 'count': list(data.values[0][1:11])}) # Create a Chartify object for this plot chart = ch.Chart(blank_labels=True, y_axis_type='categorical') # Set Variables for plot chart.set_title('Top 10 Game Publishers with Well Received Games on Steam') chart.axes.set_xaxis_label('Number of Games Publisher has in Top 1000') chart.axes.set_yaxis_label('Publishers') chart.set_source_label('Steam Website') chart.plot.bar(data_frame=data, categorical_columns='publisher', numeric_column='count', color_column='publisher', categorical_order_ascending=True) chart.style.color_palette.reset_palette_order() chart.plot.text(data_frame=data, categorical_columns='publisher', numeric_column='count', text_column='count', color_column='publisher', categorical_order_ascending=True) # Adjust the axis range to prevent clipping of the text labels. chart.axes.set_xaxis_range(0, 20) # Display Plot chart.show()
def test_data(self): data = chartify.examples.example_data() data = data.sort_values(['date', 'fruit']) color_order = data['fruit'].unique() ch = chartify.Chart(x_axis_type='datetime') ch.plot.scatter(data_frame=data, x_column='date', y_column='unit_price', size_column='quantity', color_column='fruit', color_order=color_order) assert (np.array_equal( list(filter(lambda x: x['fruit'][0] == color_order[0], ch.data))[0]['unit_price'], data[data['fruit'] == color_order[0]]['unit_price'].values))
def _heatmap_example_1(average_price_by_fruit_and_country): """Docstring""" # Plot the data (chartify.Chart(blank_labels=True, x_axis_type='categorical', y_axis_type='categorical').plot.heatmap( data_frame=average_price_by_fruit_and_country, x_column='fruit', y_column='country', color_column='total_price', text_column='total_price', text_color='white').axes.set_xaxis_label('Fruit').axes. set_yaxis_label('Country').set_title('Heatmap').set_subtitle( "Plot numeric value grouped by two categorical values").show( _OUTPUT_FORMAT))