Charting with Python

You can use the 1010data graphics widget to create charts in Python.

To use Python for charting within the 1010data Macro Language, you need to add an implementation layer within the graphics widget (<widget class_="graphics">). For more details about using the graphics widget, see <widget class_="graphics"> in the 1010data Reference Manual.

The code for charting with Python should use the following format:
<dynamic>
  <widget class_="graphics">
  ... 1010 XML QUERY ...
  <layer name="implementation" 
    plotvar_="[PYTHON_VAR]" plotlib_="[PYTHON_LIB]">
    <code language_="python">
    <![CDATA[
    ... PYTHON CODE ...
    ]]>
    <code>
  </layer>
  </widget>
</dynamic>

[PYTHON_VAR] is used for setting the value to be rendered, and the [PYTHON_LIB] value is the Python library that will be used for rendering the graphics. Note that different libraries have different ways of setting parameters and data.

Example: Scatter plot chart using Python

In the following example, the Python library mathplotlib is used to both generate data and render the chart graphics.

<dynamic>
  <do>
  <!-- Data generated with Python's numpy,
       Then stored as a temp table in the accum
       and referenced with <data> -->

    <data name="py_rand_xy">
      <code language_="python">
        <![CDATA[
d = 2*np.random.randn(1000,2)
ops = ten.rebase(pd.DataFrame(d, columns=list('xy')))
        ]]>
      </code>
      <sel value="(x + y < cos(x) & x + y > tan(x))|(x + y > cos(x) & x + y < tan(x))"/>
  </data>
  </do>
  <widget class_="graphics">
    <!-- VERY simple plotting with Python.
         Get some data,
         create a scatter plot -->
    <table data="{@py_rand_xy}"/>
    <layer name="implementation" 
        plotvar_="plt" 
        plotlib_="matplotlib" 
        width_="500" height_="200">
      <code language_="python"><![CDATA[
df = ten.GetData(ops, table).as_pandas()
plt.scatter(df['x'],df['y'])
        ]]>
      </code>
    </layer>
  </widget>
</dynamic>