【发布时间】:2014-12-28 11:38:10
【问题描述】:
我正在使用matplotlib scatterplot 函数在垂直线上创建手柄外观,以描绘图形的某些部分。但是,为了使它们看起来正确,我需要能够将散点图标记对齐到左侧(对于左线/轮廓线)和/或右侧(对于右侧线/轮廓线)。
这是一个例子:
#create the figure
fig = plt.figure(facecolor = '#f3f3f3', figsize = (11.5, 6))
ax = plt. ax = plt.subplot2grid((1, 1), (0,0))
#make some random data
index = pandas.DatetimeIndex(start = '01/01/2000', freq = 'b', periods = 100)
rand_levels = pandas.DataFrame( numpy.random.randn(100, 4)/252., index = index, columns = ['a', 'b', 'c', 'd'])
rand_levels = 100*numpy.exp(rand_levels.cumsum(axis = 0))
ax.stackplot(rand_levels.index, rand_levels.transpose())
#create the place holder for the vertical lines
d1, d2 = index[25], index[50]
#draw the lines
ymin, ymax = ax.get_ylim()
ax.vlines([index[25], index[50]], ymin = ymin, ymax = ymax, color = '#353535', lw = 2)
#draw the markers
ax.scatter(d1, ymax, clip_on = False, color = '#353535', marker = '>', s = 200, zorder = 3)
ax.scatter(d2, ymax, clip_on = False, color = '#353535', marker = '<', s = 200, zorder = 3)
#reset the limits
ax.set_ylim(ymin, ymax)
ax.set_xlim(rand_levels.index[0], rand_levels.index[-1])
plt.show()
上面的代码几乎为我提供了我正在寻找的图表,如下所示:
但是,我希望最左边的标记 (">") 是“左对齐”(即稍微向右移动),以便线条继续到标记的后面同样,我想要最右边的标记(“
关于如何以灵活的方式完成此任务的任何指导或建议?
注意:实际上,我的DataFrame 索引是pandas.Datetime,而不是我为这个简单示例提供的整数。
【问题讨论】:
标签: python matplotlib scatter-plot