【发布时间】:2019-09-03 11:07:48
【问题描述】:
我制作了一个顶部带有散点图的条形图。数据大约是 100 本书,出版日期以及作者出生和死亡的年份。 barh 显示作者在世的时间,散点图显示出版书籍的年份。
我面临的问题是能够在一个栏上绘制多本书。因为我现在有不同的书重复栏。我正在根据数组中的位置创建 y 轴,稍后我将添加标签。
我的相关代码:
# dataframe columns to arrays. (dataset is my pandas dataframe)
begin = np.array(dataset.BORN)
end = np.array(dataset.DIED)
book = np.array(dataset['YEAR (BOOK)'])
# Data to a barh graph (sideways bar)
plt.barh(range(len(begin)), end-begin, left=begin, zorder=2,
color='#007acc', alpha=0.8, linewidth=5)
# Plots the books in a scatterplot. Changes marker color and shape.
plt.scatter(book, range(len(begin)), color='purple', s=30, marker='D', zorder=3)
# Sets the titles of the y-axis.
plt.yticks(range(len(begin)), dataset.AUTHOR)
# Sets start and end of the x-axis.
plt.xlim([1835, 2019])
# Shows the plt
plt.show()
【问题讨论】:
-
你希望情节是什么样的?例如作者可以只出现一次(在一行上)并拥有多颗钻石(每本出版的书一颗)。还是别的什么?
-
这确实是我想要的。每个作者一个栏,如果有多本书,则有多个菱形。
标签: python matplotlib