Rakesh Adhikesavan
Rakesh Adhikesavan

Reputation: 12816

How to set marker style of Dataframe plot in Python Pandas?

I used df.plot() to get this plot:

enter image description here

I want to change the marker style to circles to make my plot look like this:

enter image description here

Also, is there a way to display the y axis value above each marker point?

Upvotes: 14

Views: 25512

Answers (1)

cge
cge

Reputation: 9890

The marker is pretty easy. Just use df.plot(marker='o').

Adding the y axis value above the points is a bit more difficult, as you'll need to use matplotlib directly, and add the points manually. The following is an example of how to do this:

import numpy as np
import pandas as pd
from matplotlib import pylab

z=pd.DataFrame( np.array([[1,2,3],[1,3,2]]).T )

z.plot(marker='o') # Plot the data, with a marker set.
pylab.xlim(0,3) # Change the axes limits so that we can see the annotations.
pylab.ylim(0,4)
ax = pylab.gca()
for i in z.index: # iterate through each index in the dataframe
    for v in z.ix[i].values: # and through each value being plotted at that index
        # annotate, at a slight offset from the point.
        ax.annotate(str(v),xy=(i,v), xytext=(5,5), textcoords='offset points')

Upvotes: 22

Related Questions