As well as using Excel’s own charting capabilities, PyXLL allows you to use Python’s other plotting libraries within Excel.
PyXLL has support for the following Python plotting libraries, and can be extended to support other via custom code.
Using Python’s plotting packages is preferable to using Excel’s own charts in some situations.
To show a plot or chart in Excel you use whichever Python plotting library you prefer to generate
the chart and then use PyXLL’s plot
function to render it to Excel. See the individual
guides linked above for specific instructions for each.
Regardless of which plotting library you use the plot itself will be inserted into Excel as an image. This means that it will not be interactive in the way that one hosted on a website or in a Jupyter notebook might be.
You can plot directly from an Excel worksheet function decorated with xl_func
, and so you
can provide your own inputs to your plotting function. These can be used to let the user of your
function have some control over how the chart is plotted to make it interactive. Each time they
change an input the plot will be re-drawn.
Note
Depending on the version of Excel you are using and the plotting library, the chart may be exported as an SVG image when plotting to Excel.
Some plotting libraries can occasionally show problems when plotting to SVG. If you see
any visual errors (for example, borders being too thick or the background color showing
through) you can set allow_svg=False
when calling plot
. This will cause
it to export the image in a bitmap format instead.
When you use plot
from an Excel worksheet function using xl_func
the image
inserted into the Excel workbook will be placed just below the cell the function is being called from.
Additionally, subsequent calls to the same function will replace the image rather than creating a
new one each time the function is called. This is done by giving the image a unique name for the
calling cell. If you perform multiple plots from the same function you will need to pass a name
for each to the plot
function.
The plot
function when called from anywhere other than a worksheet function will always
add a new image to the Excel workbook. By default, the location of the image will be just underneath
the currently selected cell.
If you want to replace an existing image rather than add a new one, use the name
argument to
plot
and when plotting an image with the same name multiple times the existing image in Excel
will be replaced instead of creating a new one.
New in PyXLL 5.7
When resizing a plot in Excel, when you change the selection to something else (e.g. click off the plot and into another cell) the figure will be redrawn to the new size of the image displayed in Excel.
This can be disabled by passing allow_resize=False
to the plot
function, or by setting
the following in your pyxll.cfg file
[PYXLL]
plot_auto_resize = 0