1

My data set is a list with 6 numbers: [23948.30, 23946.20, 23961.20, 23971.70, 23956.30, 23987.30]

I want them to be in a horizontal box plot, line on the x axis, with 23855 and 24472 as the limit of the x axis, so the box plot will be in the middle of the line. If this can not be done, at least showing the x axis under it, and very close to the box plot.

I also want the box plot to show the mean number.

Now I can only get the horizontal box plot, and I also want the x-axis show the whole number instead of "xx+2.394e".

Here is my code now:

def box_plot(circ_list, wear_limit):
    print circ_list
    print wear_limit

    fig1 = plt.figure()
    plt.boxplot(circ_list, 0, 'rs', 0)
    plt.show()

1 Answer 1

1

I'm not sure I understood everything in your post, but here are my corrections to your code:

l = [23948.30, 23946.20, 23961.20, 23971.70, 23956.30, 23987.30]

def box_plot(circ_list):
    fig, ax = plt.subplots()
    plt.boxplot(circ_list, 0, 'rs', 0, showmeans=True)
    plt.ylim((0.75, 1.25))
    ax.set_yticks([])
    labels = ["{}".format(int(i)) for i in ax.get_xticks()]
    ax.set_xticklabels(labels)
    plt.show()

box_plot(l)

The result:

Your box-plot

Now for the breakdown of your requests and how the code correspond to them:

  • Showing the mean: this is a simple addition of the argument showmeans=True in the plt.boxplot function.
  • Drawing the horizontal box plot closer to the x-axis. By default, the boxplot is drawn at y=1, so I just rescaled the y-axis between 0.75 and 1.25 using plt.ylim(). You can tweak those numbers if you want to draw the boxplot closer to the x-axis (by changing 0.75 to 0.9 for instance), or draw the top of the plot further from the boxplot (by changing the 1.25 to a 1.5 for instance). I also eliminated the yticks to make the plot cleaner using plt.set_ticks([]).
  • Showing the x-ticks labels in integer form. I simply convert each label of the xticks to an integer, and apply it back using the ax.set_xticklabels() function.

Do let me know if it correspond to what you were looking for, and happy matplotlib to you ;).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.