Wrong indexing in plot function