Curious result while trying to plot graph using Python

1421
6
05-26-2017 12:50 AM
AlinPlesoianu2
New Contributor

Hello,

I encountered a curious issue while trying to automate the creation of a series of graphs using Python that include inserting non-ASCII characters.

The problem is that if I run the script as a tool from ArcMap, I don't get the non-ASCII characters inserted, but if I run the script separately, outside of ArcMap, the output is correct.

I have an example using the script from matplotlib documentation:

text_labels_and_annotations example code: unicode_demo.py — Matplotlib 2.0.2 documentation 

Example script (modified to save the graph):

# -*- coding: utf-8 -*-

"""

Demo of unicode support in text and labels.

"""

from __future__ import unicode_literals

import matplotlib.pyplot as plt

import tempfile

import os

plt.title('Développés et fabriqués')

plt.xlabel("réactivité nous permettent d'être sélectionnés et adoptés")

plt.ylabel('André was here!')

plt.text(0.2, 0.8, 'Institut für Festkörperphysik', rotation=45)

plt.text(0.4, 0.2, 'AVA (check kerning)

plt.savefig(os.path.join(tempfile.gettempdir(), 'Sample_Graph.png'))

Graph result from running outside ArcMap:

Graph from script run outside ArcMap

Graph result from running the script as a tool in ArcMap:

Graph from script run as a tool in ArcMap

When running the same script from ArcMap as a tool, I don't get the non-ASCII characters printed in the graph. Has anyone else had this issue? Is this normal?

Thanks for any help!

Alin

Tags (3)
0 Kudos
6 Replies
DanPatterson_Retired
MVP Emeritus

Did you rule out this help topic?  ...displaying-a-graph-with-international-characters.... 

AlinPlesoianu2
New Contributor

Hi Dan,

I just did that change in the Windows settings, but the result is the same.

Thank you for the suggestion,

Alin

0 Kudos
DanPatterson_Retired
MVP Emeritus

You have it correct from the source code from matplotlib... so the question now becomes, how are you running this as a tool from arctoolbox?  Did you try it from the python window in ArcMap?  If you can't show any encoding characters, then it is an ArcMap issue, which you suggest you have checked

AlinPlesoianu2
New Contributor

I created a script tool and I am running it from within the ArcMap window like any other geoprocessing tool. I also tested the same code on a clean install of ArcMap on a virtual machine, and the result is identic.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Something is missing in this equation.

You have a script as defined above.  Then you created a tool in a toolbox and assigned the script to that tool.  That tool has no parameters and you run it from ArcToolbox.  Correct?  Or if not correct, then you must have a script and you are running it from the python window in arcmap.  Which of the two scenarios is the correct one.  The script runs as described and you will be running python 2.7 with MatPlotLib 1.5 and ArcGIS 10.5 at least.

I do all my matplotlib graphing so I can see absolutely any reason that you would be running that script within arcmap at all since it doesn't use arcpy nor does it capture any data from arcmap.  

On a lark, see if adding .... import arcpy .... in your script works... ie is the script 'communicating' with arcmap in any way or form.

Next step, try accessing data from a feature class... simple x,y data will do and see if you can create a graph with arcmap running.  Normally in this scenario, I pull out the data from a featureclass or shapefile and then use matplotlib without arcmap being open at all, since there is no reason to have it open.

keep us posted.

AlinPlesoianu2
New Contributor

Hello Dan,

Yes, I have a script that is assigned to a tool in a toolbox. My script, although is different from the example one posted (its too long to put it here), similarly creates a graph with matplotlib, but based on data accessed from a feature class using cursors, so arcpy imports without problems. The script I posted replicates the problem but it's easier to test on other systems. While I can run the script with no problems outside ArcMap, I need it as a tool in a toolbox.

Thanks again,

Alin

0 Kudos