def _get_or_create_plot_data(data, plotdata):
    """Create a new name for `data` if necessary, or check it is a valid name.
    """
    valid_names = plotdata.list_data()

    if not isinstance(data, str):
        name = plotdata.set_data("", data, generate_name=True)
    else:
        if data not in valid_names:
            msg = '{} is not an existing name for plot data'
            raise ValueError(msg.format(data))

        name = data

    return name