Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
168 views
in Technique[技术] by (71.8m points)

python - Line plot with data points in pandas

Using pandas I can easily make a line plot:

import pandas as pd
import numpy as np
%matplotlib inline # to use it in jupyter notebooks

df = pd.DataFrame(np.random.randn(50, 4), 
        index=pd.date_range('1/1/2000', periods=50), columns=list('ABCD'))
df = df.cumsum()
df.plot();

enter image description here

But I can't figure out how to also plot the data as points over the lines, as in this example:

enter image description here

This matplotlib example seems to suggest the direction, but I can't find how to do it using pandas plotting capabilities. And I am specially interested in learning how to do it with pandas because I am always working with dataframes.

Any clues?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use the style kwarg to the df.plot command. From the docs:

style : list or dict

matplotlib line style per column

So, you could either just set one linestyle for all the lines, or a different one for each line.

e.g. this does something similar to what you asked for:

df.plot(style='.-')

enter image description here

To define a different marker and linestyle for each line, you can use a list:

df.plot(style=['+-','o-','.--','s:'])

enter image description here

You can also pass the markevery kwarg onto matplotlib's plot command, to only draw markers at a given interval

df.plot(style='.-', markevery=5)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...