Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Oksélé. My first inadequate 3d bar plot with matplotlib
# encoding: utf-8
import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter
import numpy as np
import codecs
import pylab
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
RANGE = 20
CSV_FNAME = 'prix_train_sens.csv'
with codecs.open(CSV_FNAME, 'r', encoding='utf-8') as csv_handle:
title_line = csv_handle.next()
titles = [title.replace('"', '') for title in title_line.split('*')][2:]
table = np.genfromtxt(CSV_FNAME,
skip_header=3,
usecols=(2, 3, 4, 5, 6),
skip_footer=13,
delimiter=',')
table = table.transpose()
def line_is_minimum_for_col(line_index, col_index):
col_data = table[:, col_index]
col_min = col_data.min()
return table[line_index, col_index] == col_min
col_nbs = np.arange(20)
for index, line in enumerate(table):
print "index: %i - " % index, titles[index], line
colors = [
'g'
if line_is_minimum_for_col(index, col_index)
else 'r'
for col_index in col_nbs
]
ax.bar(col_nbs, line, zs=index, zdir='y', color=colors, alpha=0.8)
for column, value in enumerate(colors):
if value == 'g':
text = '%f' % table[index, column]
label = pylab.annotate(text,
xycoords='data',
xy = (1, 2),
textcoords = 'offset points', ha = 'right', va = 'bottom',
bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5),
arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'))
yticks = np.arange(len(table))
ax.set_yticks(yticks)
ax.set_yticklabels(titles)
ax.set_xlabel(u'Nombre de trajets par mois sur 11 mois', labelpad=300)
ax.set_ylabel(u"\n"*15 + u"Type d'abonnement")
ax.set_zlabel(u'\n'*4 + u'Prix annuel', labelpad=300)
ax.zaxis.set_major_formatter(FormatStrFormatter(u'%d €'))
for w_axis in (ax.w_xaxis, ax.w_yaxis, ax.w_zaxis):
w_axis.gridlines.set_lw(3.0)
w_axis._axinfo.update({'grid' : {'color': (0, 0, 0.75, 0.2)}})
plt.show()
@feth

This comment has been minimized.

Copy link
Owner Author

feth commented Nov 20, 2013

note: annotations seem not to work in my graph

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.