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!
Hello Cris I have been trying to create charts using Chart codes with phyton, I reviwed in shell and executed but nothing happend, It just not appear on my layer. I´ve tryed with differents codes but it is just the same, nothing appear on my layer. Could you help me, and show me how to do it correctly? This is the code that I am using import arcpy aprx = arcpy.mp.ArcGISProject (”C:/Users/DP008343/Desktop/PruebaPyton/MakingTheMostGeochemestryCopy.aprx”) Map = aprx.listMaps () [0] sensusLayer = map.istLayers (‘Marin_Pyton’) [0] c = arcpy.Chart (‘MyChart’)chart = arcpy.Chart (‘MyChart’) chart.type = ‘scatter’ chart.title = ‘Winchester & Floyd (1977)’ chart. description… Read more »
Hi Daniel, thank you for the question. There had been a bug where charts created through Python would not always display immediately in the table of contents. This was fixed in Pro 2.6, so this could be the problem if you are using Pro 2.5 or older. Otherwise, it might be more productive to communicate through email, so please feel free to contact me (callen@esri.com) to discuss in more detail. Thanks!
Hello Chris, great post as well as the earlier blog posting. Those posts make the process pretty clear. One data item that is not clear is how to set the color. It is shown as a property that can be set. I have been unable to find any documentation or examples on how to set this property.
I am working with the MatrixHeat chart type. The UI has the ability to set the color ramp. How can I set the color ramp when creating the chart with arcpy?