All you need to do to get started is download the library using pip:
pip install charts
And import the libary in Python to get started:
import charts
As is explained in the Highchart API, the plot(series, options)
function of the Charts library needs a series
array and an options
dictionary object. The series
array constists of dictionaries with two mandatory fields:
name
: The name of the seriesdata
: The data of the seriesThe options
dictionary contains the options to style the chart. More information on these options can also be found in the Highcharts API.
To get a first idea of the library, let's load some example data and use some default options for a time-series plot. In this plot we will use Highstock, a specialized version of highcharts to display timeseries, by setting the option stock=True
and display the chart inline by setting show='inline'
.
aapl = charts.data.aapl()
msft = charts.data.msft()
ohlc = charts.data.ohlc()
ohlc['display'] = False
series = [
aapl,
msft,
ohlc
]
options = charts.options.default()
charts.plot(series, options, height=500, stock=True, show='inline', save='index.html')
First, lets go over some extra features of the Charts library in comparison with Highcharts.
OHLC
and press enter. This is especially usefull if you have a large amount of data you want to plot at the same time.object > title > text
.These features are the main differences between Charts and Highcharts and we try to stay out of your way for the rest. Lets proof that in the following examples!
To create an area chart, we simply define some data and set the chart type to area.
series = [{
'name': 'John',
'data': [5, 3, 4, 7, 2]
}, {
'name': 'Jane',
'data': [2, -2, -3, 2, 1]
}, {
'name': 'Joe',
'data': [3, 4, 4, -2, 5]
}]
charts.plot(series, {'chart': {'type': 'area'}}, show='inline')
No problem! Just change the type to bar and you are done. Note that you can even change the type
in the settings editor! Try changing this bar chart back to an area chart by setting object > chart > type
back to area.
charts.plot(series, {'chart': {'type': 'bar'}, 'title': {'text': 'A bar chart'}}, show='inline')
charts.plot(series, {'chart': {'type': 'spline'}, 'title': {'text': 'A Spline chart'}}, show='inline')
... and a scatter chart. All these charts will work separatly and stay interactive even after the notebook was shut down or in a static notebook on the web (Maybe you are even viewing a static notebook now!)
charts.plot(series, {'chart': {'type': 'scatter'}, 'title': {'text': 'A scatter chart'}}, show='inline')
Yep. Know that you mention it, I wanted to show you another chart type: The Pie chart. This chart type does need a different series format but for everything else behaves just the same as the other charts.
series = [{
'type': 'pie',
'name': 'Browser share',
'data': [
['Firefox', 45.0],
['IE', 26.8],
{
'name': 'Chrome',
'y': 12.8,
'sliced': True,
'selected': True
},
['Safari', 8.5],
['Opera', 6.2],
['Others', 0.7]
]
}]
charts.plot(series, {'title': {'text': 'A pie chart'}}, show='inline')
If you don't set a chart type globally, you can still set a chart type individually for every series. I can hear you asking: "Does that mean we can plot different types in the same graph?" Ofcourse! Highcharts has us covered there:
series = [{
'type': 'column',
'name': 'Jane',
'data': [3, 2, 1, 3, 4]
}, {
'type': 'line',
'name': 'John',
'data': [2, 3, 5, 7, 6]
}, {
'type': 'column',
'name': 'Joe',
'data': [4, 3, 3, 9, 0]
}, {
'type': 'spline',
'name': 'Average',
'data': [3, 2.67, 3, 6.33, 3.33],
'marker': {
'lineWidth': 2,
'lineColor': '#90ed7d',
'fillColor': 'white'
}
}]
charts.plot(series, {'title': {'text': 'A Combination chart'}}, show='inline')
Well we have some more features :)
charts.plot()
can creates an HTML file with all data embedded in the file. This means you can easily share the HTML file and your friend just has to double click the file to see and interact with your chart!charts.plotasync()
method as a solution which has the exact same functionality as the charts.plot()
. Every series is saved in a separate JSON file and loaded asynchrounously when needed, preventing memory issuesNow go and try it! If you have any questions, feature requests or want to help visit the github page!