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();
But I can't figure out how to also plot the data as points over the lines, as in this example:
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?
To generate a line plot with pandas, we typically create a DataFrame* with the dataset to be plotted. Then, the plot. line() method is called on the DataFrame. Set the values to be represented in the x-axis.
Initializing the Plots Object Plotting can be performed in pandas by using the “. plot()” function. This function directly creates the plot for the dataset. This function can also be used in two ways.
You can plot multiple lines from the data provided by an array in python using matplotlib. You can do it by specifying different columns of the array as the x and y-axis parameters in the matplotlib. pyplot. plot() function.
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='.-')
To define a different marker and linestyle for each line, you can use a list:
df.plot(style=['+-','o-','.--','s:'])
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With