-2

I want to replicate a line plot from excel using x-y data table, but the output from my code looks different than the plot I get from the excel.

I noticed that if I change the plotting style from line to scatter in the excel then both plots looks the same.

This is a sample code for reproduce:

import matplotlib.pyplot as plt

X=[1,5,7,15,20,25,30]
Y=[12,9,10,8,7,9,6]

plt.plot(X,Y)

How to replicate a line plot from excel in to python?

David
  • 8,113
  • 2
  • 17
  • 36
  • Does this answer your question? [matplotlib: Set markers for individual points on a line](https://stackoverflow.com/questions/8409095/matplotlib-set-markers-for-individual-points-on-a-line) – Trenton McKinney Dec 16 '19 at 18:58
  • just use the `marker` option of `plot`, `plt.plot(X,Y, 'k', marker='o')` – David Dec 16 '19 at 18:59

1 Answers1

0

A line chart in Excel has a categorical horizontal (X) axis by default (or time if the data is dates). In a category axis, the data points are spread evenly across the X axis and not according to their value.

In an XY Scatter chart, however, both axes are numerical and the data points are plotted according to their value. A scatter chart series can be formatted to show dots, lines and any combination thereof, so it can look like a line chart.

If the python plot looks the same as the Excel scatter chart, that means that it uses a numeric X axis.

To make the python plot look like an Excel line chart with a categorical axis, change the X values in your python data to consecutive numbers 1 to 7. That will space out the data points evenly on the X axis.

teylyn
  • 34,374
  • 4
  • 53
  • 73
  • 1
    @TrentonMcKinney I must admit, I first mis-read the question. I've edited my answer and it now shows an approach that (AFAIC) answers the question. – teylyn Dec 17 '19 at 00:03