Wednesday, 21 January 2015

Pandas In Ipython: Basic Plotting in Ipython notebook

As a beginner to Pandas, I post my learning curve to use this package.
First, I would like to summarize some basic and essential command lines for plotting a 2D graph.

When openning an Ipython notebook, one can use the following command:

ipython notebook --pylab

Which automatically import pylab.
Then import pandas:

import pandas as pd

read data (.dat) file using:

data = pd.read_csv('directory+filename', header = 12)

Here header the number of rows of describing text before data in your .dat file.

to view the data or data information, using some of the following commands:

data                    #show data
data.head()        #show first a few rows
data.tail()            #show last a few rows
data.keys()          #show the key words for each column
data.info()           #summarize columns
data.dtypes()       #show the data type of each column
data.describe()    #quickly gives mean, std, min, max values of the numberical columns

if you want to see all the data in a table, instead of only a few rows:

pd.options.display.max_rows = 1500 # if you have 1500 rows
data

you will see all data.

If you want to delete a redundant column:

data2.drop( 'column name/key', axis = 1, inplace=True)

Now, we want to plot the numerical data:

fig=figure()
# fig.clf()      #used for clear previous plot in the same figure
ax = fig.add_subplot(1,1,1)
plot_No1 = ax.plot(data.Temperature[0:186], data.Resistance[0:186], linewidth = 2., label=r"0T")
plot_No2 = ax.plot(data.Temperature[187:231],data.Resistance[187:231],  linewidth = 2.,label=r"1T")
ax.legend(loc = 2)    # show legned, with loc(ation) at  2 (up-left)
xlabel(r'Temperature (K)', fontsize = 20)
ylabel(r'Resistance ($\Omega$)', fontsize = 20)
ax.tick_params(axis='both', which='major', labelsize=15)
savefig('fig', transparent = True)


These commands can help you to quickly visualize your data.
I will explore many more advanced features in the future.










No comments:

Post a Comment