在Matplotlib中为条形图指定x坐标的用途是什么? [英] What is use of specifying x coordinates for bar graphs in Matplotlib?
问题描述
条形图的此示例摘自Joel Grus撰写的 Data Scratch的数据科学
This example for bar charts is taken from Data Science From Scratch by Joel Grus
movies = ["Annie Hall", "Ben-Hur", "Casablanca", "Gandhi", "West Side Story"]
num_oscars = [5, 11, 3, 8, 10]
# plot bars with left x-coordinates [0, 1, 2, 3, 4], heights [num_oscars]
plt.bar(range(len(movies)), num_oscars)
plt.title("My Favorite Movies") # add a title
plt.ylabel("# of Academy Awards") # label the y-axis
# label x-axis with movie names at bar centers
plt.xticks(range(len(movies)), movies)
plt.show()
我的问题是plt.bar()
叫什么.为什么甚至需要指定条形图的左x坐标?为什么不只提供条形的高度呢?
My issue is what plt.bar()
call. Why is it even necessary to specify the left x coordinates for the bar graph? Why not just supply the height of the bars?
我的意思是为什么不只指定单个条形的高度,为什么甚至需要左坐标? 仅指定高度还不够吗?那些高度为0的条形图,您只需在提供的高度可迭代高度中指定0即可.
I mean why not just specify the heights of the individual bars, why is the left coordinate even needed ? Isn't it just sufficient to specify the height? For those bars which have a height of 0, you can just specify 0 in the height iterable that is supplied.
为什么要指定左x坐标?这有什么目的? x坐标甚至可以做什么?
Why specify the left x coordinates? What purpose does that serve? What does the x coordinates even do?
文档没有提供任何理由,为何要这样做.
The documentation offers in no reason as to why this is done.
推荐答案
x坐标不必为整数;它可以是名称列表.在更高版本(自2.1版本开始)中的预期用途是:
The x coordinate doesn't need to be an integer; it can be a list of names. The expected use in the later releases (since 2.1) is this:
from matplotlib import pyplot as plt
movies = ["Annie Hall", "Ben-Hur", "Casablanca", "Gandhi", "West Side Story"]
num_oscars = [5, 11, 3, 8, 10]
plt.bar(movies, num_oscars)
plt.show()
这篇关于在Matplotlib中为条形图指定x坐标的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!