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()
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
.
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,
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,
df = pd.read_csv('../krisk/tests/data/gapminderDataFiveYear.txt',sep='\t')
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)
As we can see, we have to reposition the coordinate accordingly, specifically for the title since it collide with both toolbox and legend.
p.set_title('GapMinder Average Population Across Continent',y_pos='-7%')
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).
We can set orientation for legend and toolbox. Below we see the legend oriented vertically and reposition nicely in the chart.
p.set_legend(orient='vertical',x_pos='12%',y_pos='-16%')
For legend and title we have discussed all the optional arguments there is. We can see docstring for set_title
and set_legend
below.
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'
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
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.
p.set_toolbox(restore=True, save_format='png', data_zoom=True)
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.
p.set_toolbox(data_view=False, restore=True)
p.set_size(width=800)
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.
p.set_toolbox(magic_type=['line','bar'],restore=True)
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.