Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bar() (and possibly other plots) should take an array of string labels for x axis #2516

Closed
stevengj opened this issue Oct 12, 2013 · 3 comments

Comments

@stevengj
Copy link
Contributor

It's pretty common to have bar charts where the x axis is a set of string labels rather than numbers. This can currently be done in pyplot with e.g.

bar([1,2,3],[4,3,6], align='center')
gca().xaxis.set_ticks([1, 2, 3])
gca().xaxis.set_ticklabels(['one', 'two', 'three'])

but it would be much nicer to simply support

bar(['one', 'two', 'three'], [4,3,6])

Similarly for barh and possibly some other plot types.

cc: @alanedelman

@mdboom
Copy link
Member

mdboom commented Oct 14, 2013

I think this is an interesting idea. It would be great to have a basic implementation as a starting point to see how best this could be handled and what APIs (if any) it might break.

@stevengj
Copy link
Contributor Author

Since Axes.bar does not currently support arrays of strings for the first argument, this would be strictly new functionality and hence should not break any existing APIs.

My thinking was to simply add a few lines to bar() in _axes.py that do something like:

if all(isinstance(s, str) for s in left):
    labels = left
    left = range(1, len(left)+1)
    align = 'center'
else:
    labels = None

and then further below in the bar() function do something like

if labels != None:
    if orientation == 'horizontal':
        self.xaxis.set_ticks(left)
        self.xaxis.set_ticklabels(labels)
    else:
        self.yaxis.set_ticks(left)
        self.yaxis.set_ticklabels(labels)

(Though I'm not much of a Python hacker. I mainly use matplotlib from the Julia via the PyPlot.jl package.)

@ghost
Copy link

ghost commented Nov 7, 2015

xticks can take care of the labeling problem associated with bar charts. see example below:

import matplotlib.pyplot as plt
x=[1,2,3,4]
labels=['jan','feb','mar','apr']
y=[40,70,30,54]
plt.bar(x,y, align='center')
plt.xticks(x, labels)

Output is shown below:
bar_chart

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants