Skip to content

Commit

Permalink
Merge pull request #43 from autonomio/new
Browse files Browse the repository at this point in the history
Major improvements to table and line types
  • Loading branch information
mikkokotila authored May 21, 2020
2 parents d4079e3 + e6abb8e commit f6e061d
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 40 deletions.
22 changes: 14 additions & 8 deletions astetik/plots/bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ def bar(data,
x=None,
y=None,
sort=None,
order=None,
multi_color=False,
group_duplicate=True,
group_by_y=True,
orient='h',
aspect=1,
size=5,
palette='default',
alpha=1,
style='astetik',
Expand Down Expand Up @@ -59,7 +63,7 @@ def bar(data,
multi_color :: If True, label values will be used for hue.
group_duplicate :: If set to False, duplicate values will not be merged.
group_by_y :: If set to False, duplicate values will not be merged.
----------------------
2.3. COMMON PARAMETERS
Expand Down Expand Up @@ -115,15 +119,15 @@ def bar(data,
x = 'y'
y = 'x'

size, aspect = factorplot_sizing(data[x])
#size, aspect = factorplot_sizing(data[x])

# merge duplicate items
if group_duplicate == True:
if group_by_y == True:
data = data.groupby(y).sum().reset_index()
data.columns = [y, x]

if sort != None:
data = data.sort_values(y, ascending=sort)
if sort is not None:
order = data[x].sort_values(ascending=sort)

if multi_color == True:
n_colors = len(data[x].unique())
Expand All @@ -138,13 +142,15 @@ def bar(data,
fig_height=None,
fig_width=None)
# <<< HEADER ENDS
p = sns.factorplot(data=data,
p = sns.catplot(data=data,
x=x,
y=y,
palette=palette,
orient=orient,
aspect=aspect,
size=size,
height=size,
alpha=alpha,
order=order,
kind='bar')

# SCALING AND LIMITS STARTS >>>
Expand Down
12 changes: 7 additions & 5 deletions astetik/plots/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,9 @@ def line(data,
textcoords="offset points",
size=14,
va="center")


plt.gcf().axes[0].yaxis.get_major_formatter().set_scientific(False)

# SCALING
if y_scale != None or x_scale != None:
for i in range(lines):
Expand All @@ -275,9 +277,9 @@ def line(data,
if len(x) > 1:
print("You can only have mean line with single line")
else:
x_mean = data[x].mean()
x_mean = np.full(len(data), x_mean)
plt.plot(data[y], x_mean)
x_median = data[x].median()
x_median = np.full(len(data), x_median)
plt.plot(data[y], x_median)

# DATETIME FORMAT
if time_frame != None:
Expand All @@ -290,7 +292,7 @@ def line(data,

_thousand_sep(p, ax, data, y, x[0])
_titles(title, sub_title=sub_title)
_footer(p, x_label, y_label, save=save)
_footer(p, x_label, y_label, save=save, tight=False)
_legend(x, legend, legend_labels, legend_position)

ax.xaxis.set_major_locator(ticker.MaxNLocator(nbins=5, integer=True))
Expand Down
3 changes: 2 additions & 1 deletion astetik/style/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def styles(dpi):
'axes.titlesize': 11.0,
'axes.titlepad': 10.0,
'axes.titleweight': 'normal',
'axes.labelpad': 15,

# FIGURE
'figure.dpi': dpi,
Expand All @@ -54,7 +55,7 @@ def styles(dpi):

# LEGEND
'legend.framealpha': 1,
'legend.frameon': True,
'legend.frameon': False,
'legend.handleheight': 2.5,
'legend.handlelength': 2.5,
'legend.loc': 'upper right',
Expand Down
12 changes: 8 additions & 4 deletions astetik/style/titles.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

def _titles(title,
sub_title,
location='left',
fontname='Verdana',
fontsize='24'):
location='center',
fontname='Arial',
fontsize='18'):

'''TITLE HANDLER
Expand Down Expand Up @@ -39,4 +39,8 @@ def _titles(title,
fontname=fontname,
weight='normal',
y=1.03,
color="grey");
color="#342b3b");




172 changes: 152 additions & 20 deletions astetik/tables/table.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,161 @@
import pandas as pd
from IPython.core.display import display, HTML

from ..utils.utils import table_prep

def table(data,
title="Descriptive Stats",
sub_title="",
table_width=630,
indexcol_width=150,
return_html=False):

def table(data, title="Descriptive Stats", table_title=""):
'''Displays a publication quality data table
with any number of columns and aspects. Works
best for 6-7 or less columns datasets.
"""PRETTY TABLE
data | DataFrame | a dataframe with three columns of numeric data
title | str | the main title of the table
sub_title | str | shown below the title
table_width | int | the width of the table in pixels
indexcol_width | int | the width of th index column in pixels.
Width of the data columns is computed based on `table_width` and
`indexcol_width` values.
USE
===
NOTE: you have to input a dataframe with exactly
three columns of data!
'''

html = '''
<style type=\"text/css\">
.tg {
border-collapse:collapse;
border-spacing:0;
border:none;}
.tg td {
font-family: Arial, sans-serif;
font-size:14px;
padding:10px 5px;
border-style:solid;
border-width:0px;
overflow:hidden;
word-break:normal;
}
.tg th {
font-family:Arial, sans-serif,
sans-serif;
font-size:14px;
font-weight:normal;
padding:10px 5px;
border-style:solid;
border-width:0px;
overflow:hidden;
word-break:normal;
}
.tg .tg-index {
font-family:Verdana, Geneva, sans-serif !important;
text-align: left;
padding-left: 10px;
vertical-align:top
}
.tg .tg-anay {
font-family:Verdana, Geneva, sans-serif !important;
text-align:right;
vertical-align:top
}
.tg .tg-jua3 {
font-weight:bold;
font-family:Verdana, Geneva, sans-serif !important;
text-align:right;vertical-align:top
}
hr {
height: 1px;
background-color: #333;
padding: 0px;
margin: 0px;
}
.hr2 {
height: 2px !important;
background-color: #333;
}
table {
table-layout: fixed;
width: 500px;
border-style:hidden;
border-collapse: collapse;\"
margin-top: 0px;
padding-top: 0px;
}
.title {
font-family: Arial, sans-serif;
font-style: italic;
font-size: 22px;
font-weight: bold;
padding-bottom: 0px;
}
.sub_title {
font-size: 18px;
font-family: Arial, sans-serif;
margin-top: 8px !important;
padding-bottom: 12px;
}
</style>
<table class=\"tg\">
<colgroup>
<col style=\"width: _INDEXCOL_WIDTH_px\">
<col style=\"width: _COL_WIDTH_px\">
<col style=\"width: _COL_WIDTH_px\">
<col style=\"width: _COL_WIDTH_px\">
<col style=\"width: _COL_WIDTH_px\">
</colgroup>
<p class='title'> _TABLE_TITLE_ </p>
<p class='sub_title'> _TABLE_SUBTITLE_ </p>
<hr align=\"left\", width=\"_TABLE_WIDTH_\">
<tr>
<th class=\"tg-index\"></th>
'''

# add columns
for col in data.columns:
html += '<th class=\"tg-anay\">' + str(col) + '</th>'

ast.table(df[['Age','Fare','Pclass']])
for index in data.index:

html += '</tr><tr><td class=\"tg-index\">' + str(index) + '</td>'

for col in data.columns:

html += '<td class=\"tg-jua3\">' + f'{data.loc[index][col]:,}' + '</td>'

html +='''
</tr>
</table>
<hr align=\"left\", width=\"_TABLE_WIDTH_\">
'''

col_width = int((table_width - indexcol_width) / len(data.columns))

html = html.replace('_TABLE_TITLE_', title)
html = html.replace('_TABLE_SUBTITLE_', sub_title)
html = html.replace('_TABLE_WIDTH_', str(table_width))
html = html.replace('_COL_WIDTH_', str(col_width))
html = html.replace('_INDEXCOL_WIDTH_', str(indexcol_width))

display(HTML(html))

PARAMETERS
----------
data :: a dataframe with three columns of numeric data
title :: title to be used immediately above the table
table_title :: main title, which is useful when 'title'
parameter is used for description or table number etc.
"""

data = table_prep(data)

display(HTML("<style type=\"text/css\"> .tg {border-collapse:collapse;border-spacing:0;border:none;} .tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:0px;overflow:hidden;word-break:normal;} .tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:0px;overflow:hidden;word-break:normal;} .tg .tg-ejgj{font-family:Verdana, Geneva, sans-serif !important;;vertical-align:top} .tg .tg-anay{font-family:Verdana, Geneva, sans-serif !important;;text-align:right;vertical-align:top} .tg .tg-jua3{font-weight:bold;font-family:Verdana, Geneva, sans-serif !important;;text-align:right;vertical-align:top} h5{font-family:Verdana;} h4{font-family:Verdana;} hr{height: 3px; background-color: #333;} .hr2{height: 1px; background-color: #333;} </style> <table class=\"tg\" style=\"undefined;table-layout: fixed; width: 500px; border-style: hidden; border-collapse: collapse;\"> <colgroup> <col style=\"width: 150px\"> <col style=\"width: 120px\"> <col style=\"width: 120px\"> <col style=\"width: 120px\"> <col style=\"width: 120px\"> </colgroup> <h5>" + str(table_title) + "</h5> <h4><i>" + str(title) + "</i></h4> <hr align=\"left\", width=\"630\"> <tr> <th class=\"tg-ejgj\"></th> <th class=\"tg-anay\">median</th> <th class=\"tg-anay\">mean</th> <th class=\"tg-anay\">std</th> <th class=\"tg-anay\">total</th> </tr> <tr> <td class=\"tg-ejgj\">" + data.index[0] + "</td> <td class=\"tg-jua3\">" + str(data['median'][0]) + "</td> <td class=\"tg-jua3\">" + str(data['mean'][0]) + "</td> <td class=\"tg-jua3\">" + str(data['std'][0]) + "</td> <td class=\"tg-jua3\">" + str(data['sum'][0]) + "</td> </tr> <tr> <td class=\"tg-ejgj\">" + data.index[1] + "</td> <td class=\"tg-jua3\">" + str(data['median'][1]) + "</td> <td class=\"tg-jua3\">" + str(data['mean'][1]) + "</td> <td class=\"tg-jua3\">" + str(data['std'][1]) + "</td> <td class=\"tg-jua3\">" + str(data['sum'][1]) + "</td> </tr> <tr> <td class=\"tg-ejgj\">" + data.index[2] + "</td> <td class=\"tg-jua3\">" + str(data['median'][2]) + "</td> <td class=\"tg-jua3\">" + str(data['mean'][2]) + "</td> <td class=\"tg-jua3\">" + str(data['std'][2]) + "</td> <td class=\"tg-jua3\">" + str(data['sum'][2]) + "</td> </tr> </table> <hr align=\"left\", width=\"630\">"))
if return_html:
return print(html)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
URL = 'http://mikkokotila.com'
LICENSE = 'MIT'
DOWNLOAD_URL = 'https://github.com/mikkokotila/pretty'
VERSION = '1.10.0'
VERSION = '1.11.0'

try:
from setuptools import setup
Expand Down
2 changes: 1 addition & 1 deletion test_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ def multicount_full(df):
df = create_data()

# run table tests
test_tables(df)
#test_tables(df)

# run simple plot tests
# test_simple_minimal(df)
Expand Down

0 comments on commit f6e061d

Please sign in to comment.