ArcGIS Pro 2.8 includes a new arcpy.charts
module with new classes that significantly improve the experience of creating and configuring charts through ArcPy. This new API provides a number of advantages over the old API, including:
- Adding new chart-specific classes to make charting more object oriented
- Making the syntax for configuring charts cleaner and more convenient
- Organizing the new classes into an
arcpy.charts
module
Still unsure about how this changes the ArcPy charting experience? Hopefully a quick example will help. Here’s a code snippet from a previous blog post that creates and configures a bar chart with the original ArcPy charts API:
c = arcpy.Chart('bar_covid_by_state')
c.type = 'bar'
c.title = "Total COVID Cases by State"
c.xAxis.field = 'state'
c.xAxis.title= "State"
c.yAxis.field = 'cases_new'
c.yAxis.title = "Cases"
c.bar.aggregation = 'sum'
c.dataSource = 'memory/covid_daily'
Notice that you first create a generic Chart
object, then specify the chart type, and finally set each parameter line-by-line. While this code works perfectly fine, setting each parameter line-by-line can be a bit cumbersome. Let’s take a look at how you would create the same chart using the new API:
c = arcpy.charts.Bar(x='state', y='cases_new', aggregation='sum',
title="Total COVID Cases by State",
xTitle="State", yTitle="Cases",
dataSource='memory/covid_daily')
Here, I’m creating the chart by instantiating a Bar
object from the arcpy.charts
module, and I’m configuring it by setting the arguments in the class constructor. This approach follows a more object-oriented design and allows you to set many parameters in one easy step, rather than tediously setting each parameter line-by-line. Now you can be more productive by writing cleaner and more efficient code!
For more code samples and details about the capabilities of the new ArcPy chart classes, we encourage you to check out the documentation page for the arcpy.charts
module. We hope that you’ll try the new API in your next Python project and see the benefit of this streamlined and object-oriented approach!
Article Discussion: