DEV Community

GharamElhendy
GharamElhendy

Posted on

Data Visualization with Pandas and Matplotlib: General Syntax

Note: The numbers attributed to figsize in the code below can be changed to whatever the analyst sees as most suitable.

Import pandas as pd
If you're using notebooks, using inline allows you to view your lines in the notebook
% matplotlib inline

df = pd.read_csv('name_of_csv_file.csv')
df.info()


To view histograms of your dataframe

df.hist()


You can control the size of the output histograms by adding it in the parentheses and add a semicolon to suppress any noise

df.hist(figsize = (8, 8));


You can call that function on a specific variable in the data set

df['name_of_column'].hist()


A more generalized formula is as follows:

df['name_of_column'].plot(kind='hist');


If you're looking to create a bar or a pie chart for one of the variables, you'll need the counts of each distinct value or bar, which you can get through:

df['name_of_variable/column'].value_counts()


Then add the visualization of the bar chart:

df['name_of_variable/column'].value_counts().plot(kind='bar');


As for the pie chart:

df['name_of_variable/column'].value_counts().plot(kind='pie', figsize = (8, 8));


As you can see, we also determined the size of the pie chart

To get insights into all numerical variables as well as histograms for each:

pd.plotting.scatter_matrix(df_file, figsize = (15, 15));


To get one scatter plot function with parameters to specify the columns that will be used for X and Y axes:

df.plot(x='paramter1', y='parameter2', kind='scatter');


To create a box plot:

df['name_of_variable'].plot(kind='box');

Top comments (0)