Tags » MatPlotLib

Ten Simple Rules for Better Data Visualization

Nicolas Rougier and friends have prepared an excellent outline of how to prepare superior data graphics.  Their focus is on preparing effective data charts, and their article dwells on how to prepare individual charts for academic publication.   274 more words

Visualization

Best backend for Python graphics

This time it will be short review of what I have found. As you can read here I got to work Spyder IDE in macOS as standalone app with Python 3.5 in it (backed it up in DMG file). 122 more words

Python

My Favorite MatPlotLib and Seaborn References

Here are some references I’ve found particularly useful when developing or debugging Python code with MatPlotLib and Seaborn.

User’s Guides

These focus on techniques for using specific methods.   159 more words

Python

Visualizing 1D complex-valued wavefunctions

Visualizing wavefunctions is essential in quantum mechanics (or wave physics, in general).

For starters, let’s start with the eigenmode of the wave produced by the transverse displacement of a string of length (like that of a guitar) with fixed endpoints. 504 more words

Colormap

MatPlot Lib -Add Axis Names

”’
Created on May 18, 2017

@author: asharda
”’
import matplotlib.pyplot as plt
plt.plot(,,’ro’)
plt.xlabel(“X-Axis”)
plt.ylabel(“Y-Axis”)
plt.axis()
plt.show()

MatPlotLib Part 2

”’
Created on May 18, 2017

@author: asharda
”’
import matplotlib.pyplot as plt
plt.plot(,,’ro’)
plt.axis()
plt.show()

Data visualization exercise using the Kaggle Titanic dataset - a good approach

Complete code:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('train.csv')
df = df[['Age', 'Sex', 'Pclass', 'Survived']]
print df.describe()
print df.info()

df['Age'] = df['Age'].fillna(df['Age'].mean())
survived = df['Survived'] == 1
survivors_sex = []
survivors_class = []
survivors_age = []
survivors_sex.append(len(df[(df['Sex'] == 'male') & (df['Survived'] == 1)]))
survivors_sex.append(len(df[(df['Sex'] == 'female') & (df['Survived'] == 1)]))
survivors_class.append(len(df[(df['Pclass'] == 1) & (df['Survived'] == 1)]))
survivors_class.append(len(df[(df['Pclass'] == 2) & (df['Survived'] == 1)]))
survivors_class.append(len(df[(df['Pclass'] == 3) & (df['Survived'] == 1)]))

lost_sex = []
lost_class = []
lost_age = []
lost_sex.append(len(df[(df['Sex'] == 'male') & (df['Survived'] == 0)]))
lost_sex.append(len(df[(df['Sex'] == 'female') & (df['Survived'] == 0)]))
lost_class.append(len(df[(df['Pclass'] == 1) & (df['Survived'] == 0)]))
lost_class.append(len(df[(df['Pclass'] == 2) & (df['Survived'] == 0)]))
lost_class.append(len(df[(df['Pclass'] == 3) & (df['Survived'] == 0)]))

bin_total = 9
bins = np.linspace(0, 80, bin_total)
df['Age categories']= pd.cut(df['Age'], bins)
age_unique = df['Age categories'].unique()
for el in age_unique:
  survivors_age.append(len(df[(df['Age categories'] == el) & (df['Survived'] == 1)]))
  lost_age.append(len(df[(df['Age categories'] == el) & (df['Survived'] == 0)]))

bar_width = 0.9
fig = plt.figure(figsize=(10, 5), dpi=100)
plt.gcf().subplots_adjust(bottom=0.15)

plt.subplot(131)
xs = np.arange(2)
xlabel = ['Male', 'Female']
plt.xticks(xs, xlabel, rotation='45')
plt.bar(xs, survivors_sex, alpha = 1, color = 'g', width = bar_width, label = 'Survived')
plt.bar(xs, lost_sex, bottom = survivors_sex, alpha = 1, color = 'r', width = bar_width, label = 'Lost')
plt.legend(bbox_to_anchor=(0.7, 1.17))

plt.subplot(132)
xs = np.arange(3)
xlabel = ['Class 1', 'Class 2', 'Class 3']
plt.xticks(xs, xlabel, rotation='45')
plt.bar(xs, survivors_class, alpha = 1, color = 'g', width = bar_width)
plt.bar(xs, lost_class, bottom = survivors_class, alpha = 1, color = 'r', width = bar_width)

plt.subplot(133)
xs = np.arange(len(survivors_age))
xlabel_age = []
for el in age_unique:
  xlabel_age.append(str(el))
plt.xticks(xs, xlabel_age, rotation='45')
plt.bar(xs, survivors_age, alpha = 1, color = 'g', width = bar_width)
plt.bar(xs, lost_age, bottom = survivors_age, alpha = 1, color = 'r', width = bar_width)

plt.show()
plt.savefig('Titanic2.jpg')

print 'Men survived: ',  survivors_sex[0]
print 'Men died: ',  lost_sex[0]
print 'Women survived: ',  survivors_sex[1]
print 'Women died: ',  lost_sex[1]
print 'Class 1 survived: ',  survivors_class[0]
print 'Class 1 died: ',  lost_class[0]
print 'Class 2 survived: ',  survivors_class[1]
print 'Class 2 died: ',  lost_class[1]
print 'Class 3 survived: ',  survivors_class[2]
print 'Class 3 died: ',  lost_class[2]
for i in range(bin_total-1):
    print age_unique[i], 'survived: ', survivors_age[i]
    print age_unique[i], 'died: ', lost_age[i]
… 974 more words
Data