lordingtar
lordingtar

Reputation: 1052

Setting tick labels in mpld3

I'm trying to create a simple bar graph with some data (hard coded here, but I'll be reading it in from a file at some point). So far, I'm able to get the bar graph, but I would like the attribute "Feature Name" to come under each bar. Right now, I'm only getting numbers 1 through 16. What can I do to make each feature under each bar?

My code:

import matplotlib.pyplot as plt
import numpy as np
import mpld3

fig, ax = plt.subplots()

N = 17
feature_name = ('AccountLength','Intlact','Vmailact','Vmailnumber','day minutes','day calls','day charge','evening minutes','evening calls','evening charge','night minutes','night calls','night charge','intl minutes','intl calls','intl charge','cust calls')
importance = (0.0304,0.0835,0.0222,0.0301,0.1434,0.0315,0.1354,0.0677,0.0268,0.0669,0.0386,0.0286,0.0371,0.0417,0.0521,0.0434,0.1197)
ind = np.arange(N)
width = 0.20
rects = ax.bar(ind, importance, width, color = 'r')
ax.grid(color='white', linestyle='solid')

ax.set_title("Why are my customers churning?", size=20)
ax.set_ylabel('Importance in percentage')
ax.set_xlabel('Feature Name')
ax.set_xticklabels( (feature_name) )
labels = (feature_name)
tooltip = mpld3.plugins.PointLabelTooltip(rects, labels=labels)
mpld3.plugins.connect(fig, tooltip)

mpld3.show()

Edit: turns out that if I use plt.show(), I can see ticklabels, but when I try to do the same in mpld3, it doesn't work. Also wondering why the tooltip doesn't show up.

Upvotes: 4

Views: 3516

Answers (2)

Michael
Michael

Reputation: 1180

I think the answer is too late, however maybe this could help someone else.

Setting the text as tick labels seems really have some issues in mpld3 package, however I was able to do this when plotting some data to certain range values, then setting ticks to this range and finally setting tick labels to needed ones.

from matplotlib import pyplot as plt
import numpy as np
import mpld3

fig, ax = plt.subplots()

feature_name = ('AccountLength','Intlact','Vmailact','Vmailnumber','day minutes','day calls','day charge','evening minutes','evening calls','evening charge','night minutes','night calls','night charge','intl minutes','intl calls','intl charge','cust calls')
importance = (0.0304,0.0835,0.0222,0.0301,0.1434,0.0315,0.1354,0.0677,0.0268,0.0669,0.0386,0.0286,0.0371,0.0417,0.0521,0.0434,0.1197)
ind = range(1, len(feature_name)+1)
width = 0.20

rects = ax.bar(ind, importance, width, color = 'r')

ax.grid(linestyle='solid')
ax.set_title("Why are my customers churning?", size=20)
ax.set_ylabel('Importance in percentage')
ax.set_xlabel('Feature Name')
ax.set_xticks(ind)
ax.set_xticklabels(feature_name)

mpld3.show()

Result:

mpld3 ticklabels example

There is an extra staff to do with ticks rotation (also not supported in mpld3). Maybe custom plugin could be written to achieve this. If someone need this, I will update answer.

Upvotes: 1

wombatonfire
wombatonfire

Reputation: 5410

Setting tick labels and locations is not supported in mpld3. See issue 22.

Tooltips do not appear because PointLabelTooltip() function accepts matplotlib Collection or Line2D object as an input, while plt.bar() returns BarContainer object.

UPD: Tick labels were tested with version 0.2 & 0.3git.

Upvotes: 0

Related Questions