In [1]:
import pandas as pd
import krisk.plot as kk
# Use this when you want to nbconvert the notebook (used by nbviewer)
from krisk import init_notebook; init_notebook()


Out[1]:

Before we added talk about each of these features. There's something in common, the way the element position themselves, this are position (x_pos,y_pos), align, and orientation.

Position

Everytime we add toolbox, title, or legend, we almost always want to reposition those elements.

When default to auto for both x-axes and y-axes,

  • The title will position at top-left corner.
  • The legend will position at top-center.
  • The toolbox will position at top-left corner.

As we can see from this criteria, there's definitely collision for title and toolbox, and legend if the title is longer.

Take a look at this example,


In [3]:
df = pd.read_csv('../krisk/tests/data/gapminderDataFiveYear.txt',sep='\t')

In [4]:
p = kk.bar(df,'year',y='pop',how='mean',c='continent')
p.set_size(width=800)
p.set_title('GapMinder Average Population Across Continent')
p.set_toolbox(save_format='png',restore=True)


Out[4]:

As we can see, we have to reposition the coordinate accordingly, specifically for the title since it collide with both toolbox and legend.


In [5]:
p.set_title('GapMinder Average Population Across Continent',y_pos='-7%')


Out[5]:

Notice that the coordinate for x_pos and y_pos has negative element. The way krisk started the position is from bottom-left corner, and based on percentage of the pixel. If on the negative element, then it will be the other way around, with x position started at the right, and y position started at the top.

Beside coordinate number, krisk support auto, center, left-right (x position), and top-bottom (y position).

Orientation

We can set orientation for legend and toolbox. Below we see the legend oriented vertically and reposition nicely in the chart.


In [6]:
p.set_legend(orient='vertical',x_pos='12%',y_pos='-16%')


Out[6]:

Legend and Title

For legend and title we have discussed all the optional arguments there is. We can see docstring for set_title and set_legend below.


In [7]:
help(p.set_title)


Help on method set_title in module krisk.chart:

set_title(title, x_pos='auto', y_pos='auto') method of krisk.chart.Chart instance
    Set title for the plot.
    
    The coordinate is started at bottom left corner. If x_pos and y_pos started
    at negative values, then it's converted to the upper right corner (left->right, bottom->top)
    
    Parameters
    ----------
    title: str
        Title of the chart.
    x_pos: str, {'auto', left', 'center', 'right', 'i%'}, default to 'auto'
    y_pos: str, {'auto', top', 'center', 'bottom', 'i%'}, default to 'auto'


In [8]:
help(p.set_legend)


Help on method set_legend in module krisk.chart:

set_legend(align='auto', orient='horizontal', x_pos='auto', y_pos='auto') method of krisk.chart.Chart instance
    Set legend style.
    
    The coordinate is started at bottom left corner. If x_pos and y_pos started
    at negative values, then it's converted to the upper right corner (left->right, bottom->top)
    
    Parameters
    ----------
    align: str, {'auto','left','right'}, default to 'auto'
    orient: str, {'horizontal','vertical'} default to 'horizontal'
    x_pos: str, {'auto', left', 'center', 'right', 'i%'}, default to 'auto'
    y_pos: str, {'auto', top', 'center', 'bottom', 'i%'}, default to 'auto'
    
    Returns
    -------
    Chart Object

Toolbox

Beside align, position, and orientation, Toolbox has different set of options that we can interact with the chart. First the easiest, restore,save_format, and panning.

Restore, Save, and Zoom


In [9]:
p.set_toolbox(restore=True, save_format='png', data_zoom=True)


Out[9]:

The restore option will return your plot to original form. This is intended after you edit the plot, you may want to reset it back. The save_format is nice feature that you can use to download as image after editing the plots.

Data View


In [10]:
p.set_toolbox(data_view=False, restore=True)
p.set_size(width=800)


Out[10]:

The data_view is another convenience function to let readers see table data that construct the plot. If True, it's set to read_only. Set it to False will let readers modify the data, hence changing the plot.

Magic Type


In [11]:
p.set_toolbox(magic_type=['line','bar'],restore=True)


Out[11]:

magic_type let your plot changed as you explore different options to visualize your data. Here the exact same plot is used, but using line to change the visualization.