Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 668
Rakesh Adhikesavan Avatar asked Feb 09 '16 19:02

Rakesh Adhikesavan


People also ask

How do I change the index of a data frame?

To change the index values we need to use the set_index method which is available in pandas allows specifying the indexes. where, inplace parameter accepts True or False, which specifies that change in index is permanent or temporary. True indicates that change is Permanent.

How do I reshape my data frame?

melt() function is used to reshape a DataFrame from a wide to a long format. It is useful to get a DataFrame where one or more columns are identifier variables, and the other columns are unpivoted to the row axis leaving only two non-identifier columns named variable and value by default.

What is Orient in pandas?

orient: String value, ('dict', 'list', 'series', 'split', 'records', 'index') Defines which dtype to convert Columns(series into). For example, 'list' would return a dictionary of lists with Key=Column name and Value=List (Converted series).


1 Answers

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')
like image 53
cge Avatar answered Sep 21 '22 11:09

cge