Skip to content

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

Closed

Description

@stevengj

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

Activity

  1. mdboom commented on Oct 14, 2013

    @mdboom
    Member

    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.

  2. stevengj commented on Oct 14, 2013

    @stevengj
    ContributorAuthor

    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.)

  3. added this to the milestone on Feb 25, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    API: consistencyConsistency of the matplotlib API, including naming, behavior, defaults, …New feature

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions