Examples
First example
import datura
datura.plot([[1, 2, 3], [1, 3]], [[0, 1, 3], [1, 4]],
filename='examples/first_example.svg', x_label='x label',
y_label='y label', title='First Example',
labels=['good', 'better'])
Displaying error patches
The following demonstrates how to display error patches with output from a typical SQL query. (You can see the query that generated the data using an open data set available through the Bigquery console.)
import datura
import pandas as pd
sql_query = """
--sql
WITH final_score_CTE AS (
SELECT
MIN(homeFinalRuns) AS home_runs
, MIN(awayFinalRuns) AS away_runs
, MIN(startTime) AS start_time
, gameID
FROM `bigquery-public-data.baseball.games_wide`
WHERE gameStatus = 'closed'
GROUP BY gameID
)
SELECT
AVG(home_runs) AS avg_home_runs
, STDDEV_SAMP(home_runs) AS std_home_runs
, AVG(away_runs) AS avg_away_runs
, STDDEV_SAMP(away_runs) AS std_away_runs
, COUNT( DISTINCT gameID) AS games
, DATE_TRUNC(start_time, MONTH) AS game_month
FROM final_score_CTE
GROUP BY game_month
ORDER BY game_month
;
"""
results_df = pd.read_csv('examples/runs.csv', parse_dates=['game_month'])
results_df['sem_home'] = results_df['std_home_runs'] / results_df['games']**.5
results_df['sem_away'] = results_df['std_away_runs'] / results_df['games']**.5
datura.error_plot(results_df['game_month'],
results_df[['avg_home_runs', 'avg_away_runs']],
y_errors=results_df[['sem_home', 'sem_away']],
filename='examples/error_patch_example.svg',
x_label='Game month', y_label='Runs (+/- SE)',
title='Error Patch Example', labels=['Home', 'Away'],
label_nudges=[0, 0],
y_ticks=[0, 2, 4, 6])
Displaying data in a Pandas dataFrame
import datura
import numpy as np
import pandas as pd
df = pd.DataFrame(data={'x': range(11),
'y1': np.arange(11)**2,
'y2': 100-np.arange(11)**2})
datura.plot(df['x'], df[['y1', 'y2']], filename='examples/pandas_example.svg',
x_label='x label', y_label='y label', title='Pandas Example',
labels=df.columns[1:].tolist(), x_ticks=[0, 5, 10],
y_ticks=[0, 25, 50, 75, 100])
Displaying time series data in a Pandas dataFrame
import datura
from datetime import datetime
import numpy as np
import pandas as pd
time_range = pd.date_range(start='2021-01-01', end='2022-01-01', freq='MS')
df = pd.DataFrame(data={'t': time_range,
'y1': np.sin(np.linspace(0, 2*np.pi, 13)),
'y2': np.cos(np.linspace(0, 2*np.pi, 13))})
x_ticks = []
for tick_ind, tick in enumerate(df.t.tolist()):
if tick_ind % 3 == 0:
x_ticks.append(tick)
datura.plot(df['t'], df[['y1', 'y2']], filename='examples/time_example.svg',
x_label='x label', y_label='y label', title='Time Example',
labels=df.columns[1:].tolist(), x_ticks=x_ticks,
y_ticks=[-1, 0, 1])
Displaying histograms
import datura
import numpy as np
import pandas as pd
rng = np.random.RandomState(10)
ar = np.vstack((rng.normal(loc=-2, scale=1.5, size=5000),
rng.normal(loc=0, scale=2, size=5000),
rng.normal(loc=3, scale=1.5, size=5000))).T
df = pd.DataFrame(data={'yb': ar[:, 0],
'yy': ar[:, 1],
'yg': ar[:, 2]})
datura.hist(data=df[['yb', 'yy', 'yg']],
bin_edges=np.linspace(-10, 10, 100),
filename='examples/hist_example.svg',
x_label='Days until ready to eat',
y_label='Number of bananas',
colors=['brown', 'gold', 'green'],
labels=['brown', 'yellow', 'green'],
label_nudges=[0, 10, 20],
x_ticks=[-10, -5, 0, 5, 10],
y_ticks=[0])
Displaying bar charts
import datura
import pandas as pd
x_ticks_text = ['Boreas', 'Zephryos', 'Notos', 'Euros']
df = pd.DataFrame(data={'Night': [2, 5, 8, 3],
'Day': [1, 5, 6, 4]})
datura.bar(df, filename='examples/bar_example.svg', x_ticks_text=x_ticks_text)
Jupyter notebooks
In [1]:
import datura
import pandas as pd
In [2]:
results_df = pd.read_csv('runs.csv', parse_dates=['game_month'])
results_df['sem_home'] = results_df['std_home_runs'] / results_df['games']**.5
results_df['sem_away'] = results_df['std_away_runs'] / results_df['games']**.5
In [3]:
_ = datura.error_plot(results_df['game_month'],
results_df[['avg_home_runs', 'avg_away_runs']],
y_errors=results_df[['sem_home', 'sem_away']],
filename='error_patch_example.svg',
x_label='Game month', y_label='Runs (+/- SE)',
title='Jupyter Example', labels=['Home', 'Away'],
y_ticks=[0, 2, 4, 6])
In [4]:
import matplotlib.pyplot as plt
plt.ion()
_ = plt.plot(results_df['game_month'], results_df['avg_home_runs'], label='Home')
_ = plt.fill_between(results_df['game_month'],
results_df['avg_home_runs'] - results_df['sem_home'],
results_df['avg_home_runs'] + results_df['sem_home'], label='',
alpha=.3)
_ = plt.plot(results_df['game_month'], results_df['avg_away_runs'], label='Away')
_ = plt.fill_between(results_df['game_month'],
results_df['avg_away_runs'] - results_df['sem_away'],
results_df['avg_away_runs'] + results_df['sem_away'], label='',
alpha=.3)
_ = plt.ylim((0, 6))
_ = plt.title('Jupyter Example')
_ = plt.xlabel('Game month')
_ = plt.ylabel('Runs (+/- SE)')
_ = plt.legend()