pythonvisualizationlightningchart-python

LightningChart Python — legend entries overlap


I'm trying to use LightningChart Python v2.0.1 to display a line chart with multiple trends and outlier points, but for some reason the legend entries overlap each other.

I'm following guidance from here https://lightningchart.com/python-charts/docs/examples/chart-xy/

Here is my code:

import lightningchart as lc
import random


# Initialize the chart
chart = lc.ChartXY(
    theme=lc.Themes.Light,
    title='Chart XY',
)

# Add point series to the chart
point_series = chart.add_point_series()
point_series.set_point_color((0, 255, 255, 128))

legend = chart.add_legend()

lines_amount = 3
for i in range(lines_amount):
    # Add line series to the chart
    line_series = chart.add_line_series()
    legend.add(line_series)
    y = 0
    # Add data to series
    for x in range(1000):
        y += (random.random() * 2) - 1
        line_series.add(x=x, y=y)
        if random.random() > 0.75:
            point_series.add(x=x, y=y + random.uniform(-10, 10))

chart.open()

legend entries overlapping

What API do I need to use to make the legend entries not overlap?


Solution

  • Since LightningChart Python v2.0+, a default legend is added and auto-populated. In your code snippet, you also create a second, user-managed legend with:

    legend = chart.add_legend()
    ...
    legend.add(line_series)
    

    This results in two legends (the automatic one and your manual one), which appear to overlap. You can either remove the manual legend lines:

    # legend = chart.add_legend()   # remove
    # legend.add(line_series)       # remove
    

    Or, if you really need a custom/extra legend, disable or hide the automatic entries on the default legend when creating the chart:

    chart = lc.ChartXY(
        title='Chart XY',
        legend={'add_entries_automatically': False}   # or: {'visible': False}
    )
    # now manage your own legend:
    legend = chart.add_legend()
    legend.add(line_series)
    

    https://lightningchart.com/python-charts/docs/features/legend/