Using Highcharts for Python – Basic Tutorial

Using Highcharts for Python - Basic Tutorial logo

Highcharts Core is the gold standard in JavaScript data visualization libraries, enabling you to design rich, beautiful, and highly interactive data visualizations of (almost) any kind imaginable, and to render those visualizations in your web or mobile applications. Take a look at some of the customer showcases and the own demo gallery to see what you can do with Highcharts.
Highcharts for Python is a Python wrapper for the Highcharts Core JavaScript library, which means that it is designed to give developers working in Python a simple and Pythonic way of interacting with Highcharts Core.
Highcharts for Python will not render data visualizations itself – that’s what Highcharts Core does – but it will allow you to:

  1. Configure your data visualizations in Python.
  2. Supply data you have in Python to your data visualizations.
  3. Programmatically produce the Highcharts Core JavaScript code that will actually render your data visualization.
  4. Programmatically download a static version of your visualization (as needed) within Python.

Think of Highcharts for Python as a translator to bridge your data visualization needs between Python and JavaScript.

Getting Started with Highcharts for Python

To use Highcharts for Python, like with any Python library, you first have to install it. That’s super easy: just open your Python project (in your virtual environment – you are using virtual environments, right?) and execute:

$ pip install highcharts-core

And that’s it! Highcharts for Python will now be installed in your project and available for use. To use it in your code, you just have to import it the way you would any other library.

Importing Highcharts for Python

The Highcharts for Python library is quite extensive. It’s got a rich (read: complicated) API, with lots of different objects and modules. That’s a reflection of the visualization power that Highcharts offers you, but it does make the decision of what to import and how to import it a little more complicated.
Python’s best practice is to import only what you need, which helps to maximize the performance of your Python code and prevents your application’s namespace from getting cluttered with various things (and further reducing the memory footprint of your Python code, which is always good).
You can either import specific things from their precise locations, or you can also just import the catch-all highcharts module, which flattens the entire API and exposes just about every class/object in one location. We definitely recommend importing things from their precise location like so:

# Import classes using precise module indications. For example:
from highcharts_core.chart import Chart
from highcharts_core.global_options.shared_options import SharedOptions
from highcharts_core.options import HighchartsOptions
from import BarOptions
from import BarSeries

A Super Simple Example

While your project is likely more complicated, let’s take a look at the easiest use case: visualizing some data quickly and easily. You likely have your data in a CSV file, or perhaps in a Pandas dataframe, or maybe just stored in a list in your Python code. Highcharts for Python can work with all of those scenarios.
Let’s assume you have your data stored in a CSV file named “my-csv-file.csv” (creative, right?).
You can create a chart from a CSV file by:

  1. Importing the Chart type from highcharts_core.chart:
    from highcharts_core.chart import Chart
  2. Calling the Chart.from_csv() class method, which will create a new Chart instance:
    from highcharts_core.chart
    import Chart
    # Create a new Chart instance from the CSV file "my-csv-file.csv".
    my_chart = Chart.from_csv('my-csv-file.csv',
      property_column_map = {
        'x': 0,
        'y': 3,
        'id': 'id'
      series_type = 'line',
      chart_kwargs = {
        'container: '
        target_div ',
        'variable_name': 'myChart'

And that’s it! Let’s breakdown what’s happening in the method call above:
First, you’re telling the .from_csv() method which CSV file to use. That’s pretty straightforward. But the magic really happens in the next three arguments:

  • The property_column_map argument, which tells the method which CSV columns correspond to which properties in the series’ data points, and
  • The series_type argument, which tells the method that you want to plot the CSV data in a line series (plot the data points as a line).
  • The chart_kwargs argument, which supplies other keyword arguments used to configure your Chart instance.

The property_column_map Argument

The property_column_map argument takes a dict whose keys correspond to the properties in the series type’s .data property. That can seem a little complicated, but think of it this way:
Highcharts supports over 70 different series types in its visualization suite. Different series types may have different properties for their data points. Some (like the LineSeries shown above) may be very simple: they get an “x” value for the x-axis, and a “y” value for the y-axis, and an “id” that uniquely identifies the data point.
The keys in property_column_map are the data point properties that should be populated. In the example above, the “x” key corresponds to the data point’s .x property, the “y” key corresponds to the data point’s .y property, and the “id” key corresponds to the data point’s .id property.
So the property_column_map argument tells Highcharts for Python which CSV column maps to which property. Pretty straightforward!

If you use convenience methods like .from_csv(), make sure to review the series type’s data points to map your data to the appropriate properties. You can do so in the extensive API Reference for the series type you want to chart.

Some CSV files may contain headers, while others might not contain headers. For that reason, the column_property_map values accept either integers (which indicate which column in your CSV file should map to that property) or strings (which contain the column name if your CSV file contains a header).

By default, Highcharts for Python expects that your CSV file has a header row, but you can override this expectation by supplying an argument has_header_row = False to the .from_csv() method.

The series_type Argument

Using the series_type argument, you determine which series type will be used to visualize your data. You can find a complete list of supported series types in the Supported Visualizations section of our documentation.
In the example above, we use all of the default settings for the LineSeries that gets created, but you can further configure your series by passing its configuration options in a series_kwargs argument. That argument expects a dict whose keys map to the series properties you wish to configure, and whose values are the values that you want to apply.

The chart_kwargs Argument

Just as you can use the optional series_kwargs argument to configure the series of data you are creating, you can use the chart_kwargs argument to configure your Chart instance. This argument expects a dict whose keys map to the Chart properties you wish to configure, and whose values are the values that you want to apply.
In the example above, we are setting the my_chart.container property (giving it the value target_div) and the my_chart.variable_name property (giving it the value “myChart”). These chart properties are very important for rendering your chart in a web environment, so we’ll discuss them in greater detail below.

Other Approaches

The example above focuses on using a CSV file, but Highcharts for Python has you covered regardless of how you are managing your data in your Python application. You have similar convenience functions for loading data:

You can call these convenience methods on the Chart class as shown above, or you can also call the exact same convenience methods on a series type, and then add the series to a chart instance using the my_chart.add_series() method.

Our other tutorials show you in detail how to use these different convenience methods to rapidly build great visualizations – we recommend you check out here: Highcharts for Python Tutorials.

And that’s it! You now have a fully-configured Chart instance containing the data from your CSV file. We’ll describe how to actually use this Chart instance down below.

Visualizing Your Chart

Now that we’ve built a Chart instance and populated it with data, our next step is to visualize it. How you actually do this depends to some extent on how you are building your Python application.
Maybe you are using a web framework like Flask, Django, or FastAPI and relying on their templating engines for creating your views. Or maybe you are providing a Python backend API that delivers data to an entirely separate app via RESTful API calls. In any case, to visualize your new chart you need to somehow get its configuration to your web-based front-end. And that is super simple as well.
Using the example above, you can generate the full set of HTML and JavaScript content to render your chart with one method call:

as_js_literal = my_chart.to_js_literal()
# This will produce a string equivalent to:
# document.addEventListener('DOMContentLoaded', function() {
#   const myChart = Highcharts.chart('target_div', {
#      series: {
#          type: 'line',
#          data: [0, 5, 3, 5]
#      }
#   });
# });

So what is this method call doing? It is taking the entire set of instructions included in your my_chart variable, creating a JavaScript literal string that represents them, and putting that string in the as_js_literal Python variable. This string can then be piped into your web front end using whatever templating engine you are using, or delivered to your front-end in an API response, and it will then render your chart as you configured it.
In the example above, if you place the as_js_literal string in what gets rendered in your user’s browser, your chart will be automatically rendered by Highcharts Core, placing the chart inside the div element in your content whose id is “target_div”.
And that’s it! You should now see a beautiful line chart in your web content.

Downloading Your Chart

Often when you’ve created a visualization, you may want to download a static version of it as an image that can be embedded in other documents. With Highcharts for Python, that is a fairly trivial exercise. Given the example above, you can produce a PNG image very simply with one method call:

# Download a PNG version of the chart in memory within your Python code.
my_png_image = my_chart.download_chart(format = 'png')
# Download a PNG version of the chart and save it the file "/images/my-chart-file.png"
my_png_image = my_chart.download_chart(
  format = 'png',
  filename = '/images/my-chart-file.png'

The two examples shown above both download a PNG of your chart:
The first example keeps that PNG image in your Python code only, storing its binary data in the my_png_image variable. The second example not only stores its binary data in the my_png_image variable, but it also saves the PNG image to the file “/images/my-chart-file.png”.
The format argument is really the one doing the heavy lifting above. In the example above, it tells the method to generate a PNG image, but you can also create “jpeg”, “pdf”, and “svg”.
And that’s it! You should know that the .download_chart() method defaults to using the Highcharts Export Server provided by Highsoft (creators of Highcharts Core), however you can configure the method to use your own custom Highcharts Export Server if you choose. For more details on how to do this, please review our tutorial on Exporting Static Charts using Highcharts for Python.

More Resources

The above tutorial is just a really simple example of how you can create rich visualizations with just a handful of method calls using Highcharts for Python. But the library offers so much more! We recommend you take a look at the following additional resources which you may find useful: