HOME/Articles/

matplotlib example grafa (snippet)

Article Outline

Python matplotlib example 'grafa'

Modules used in program:

  • import matplotlib.pyplot as plt
  • import matplotlib.mlab as mlab
  • import numpy as np
  • import sys

python grafa

Python matplotlib example: grafa

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

'''
  Para geração de histograma das acelerações
'''

import sys
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

# Tamanho da página do histograma
width = 20  # cm
height = 15 # cm

# Altura de queda
deltaS = 34 # metros

if len(sys.argv) < 3:
  print("Usage: generate.py data.txt output")
  exit(1)

output = sys.argv[2]

data = np.fromfile(sys.argv[1], sep='\n')

data = (2 * deltaS) / (data ** 2)

num_bins = 12
avg = np.average(data)
std = np.std(data)

fig, ax = plt.subplots()
n, bins, patches = ax.hist(data, num_bins, color='burlywood', histtype='stepfilled')

print("Média: %s Desvio Padrão: %s" %(avg, std))

ax.axvline(avg, color='red', label="Média = %.2f m/s²" %avg)
ax.axvline(avg-std, color='lightblue', label="Média - Desvio Padrão = %.2f m/s²" %(avg-std))
ax.axvline(avg+std, color='darkblue', label="Média + Desvio Padrão = %.2f m/s²" % (avg+std))

ax.set_xlabel('Aceleração (m/s²)')
ax.set_ylabel('Contagem (n)')
ax.set_title('Histograma das Acelerações Médias')
ax.set_ylim( None, n.max() * 1.2)

legend = ax.legend(loc='upper left', shadow=True, prop={'size':10})
fig.tight_layout()

fig.set_size_inches(width / 2.54, height / 2.54)
plt.savefig('%s.png' %output, dpi=100)