Home > database >  How do I plot a line graph with dots for each data point using MatPlotLib in Python?
How do I plot a line graph with dots for each data point using MatPlotLib in Python?

Time:03-16

My desired outcome is a graph that looks like this

ax.plot(df.x, df.y, 'b.-')

where a line is generated with a dot at every location there is a data point. But I also want specific colors like "lightsteelblue".

When I try plotting

ax.plot(df.x, df.y, color='lightsteelblue', ls='.-')

a value error comes up that '.-' is not supported with "ls". Any ideas on how to get around this?

Thanks for your time, -Bojan

CodePudding user response:

The argument ls is short for linestyle, but you're trying to also pass it a marker style. But to pass a marker style you need to use the marker argument, e.g.

ax.plot(x, y, color='black', ls='-', marker='.')

CodePudding user response:

below code should work for you

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, np.random.randint(1, 7) , 10)
y = np.linspace(0, np.random.randint(7, 9), 10)

fig, ax = plt.subplots()

ax.plot(x, y, **{'color': 'lightsteelblue', 'marker': 'o'})

plt.show()

output

enter image description here

  • Related