我已经使用Pandas plot
功能组装了一个plot
,但希望帮助您使用以下元素完成它(如所需的输出图映像所示):
OpenToLast
栏数据更加突出,所以如果可能的话,希望将其他堆叠的条形图淡出后台?数据:
请参见DataFrame.to_dict()
output 这里。
这就是我如何获得现有的plot
auction[['OpenToLast','OpenToMaxHigh','OpenToMaxLow']].head(20).plot(kind='barh',
figsize=(7,10),
fontsize=10,
colormap ='winter',
stacked = True,
legend = True)
当前绘图:
期望输出:
发布于 2017-01-15 09:54:38
尝试以下几点:
原来最棘手的部分是着色,但绘制线条和更新滴答相对简单(请参阅代码的结尾)
import numpy as np
# get the RGBA values from your chosen colormap ('winter')
winter = matplotlib.cm.winter
winter = winter(range(winter.N))
# select N elements from winter depending on the number of columns in your
# dataframe (make sure they are spaced evenly from the colormap so they are as
# distinct as possible)
winter = winter[np.linspace(0,len(winter)-1,auction.shape[1],dtype=int)]
# set the alpha value for the two rightmost columns
winter[1:,3] = 0.2 # 0.2 is a suggestion but feel free to play around with this value
new_winter = matplotlib.colors.ListedColormap(winter) # convert array back to a colormap
# plot with the new colormap
the_plot = auction[['OpenToLast','OpenToMaxHigh','OpenToMaxLow']].head(20).plot(kind='barh',
figsize=(7,10),
fontsize=10,
colormap = new_winter,
stacked = True,
legend = True)
the_plot.axvline(0,0,1) # vertical line at 0 on the x axis
start,end = the_plot.get_xlim() # find current span of the x axis
the_plot.xaxis.set_ticks(np.arange(start,end,10)) # reset the ticks on the x axis with increments of 10
发布于 2017-01-15 09:44:04
我没有意识到我可以使用Matplotlib直接使用Pandas plot
命令。现在,我已经从上面复制了代码,并对其进行了修改,以添加Matplotlib中的其他元素。
如果有人知道怎么做的话,最好在条形图上加一个梯度,但我会把这个问题记下来,回答如下:
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
cols = ['OpenToLast','OpenToMaxHigh','OpenToMaxLow']
colors = {'OpenToLast':'b', 'OpenToMaxHigh' : '#b885ea', 'OpenToMaxLow': '#8587ea'}
axnum = auction[cols].head(20).plot(kind='barh',
figsize=(7,10),
fontsize=10,
color=[colors[i] for i in cols],
stacked = True,
legend = True)
axnum.xaxis.set_major_locator(ticker.MultipleLocator(10))
plt.axvline(0, color='b')
https://stackoverflow.com/questions/41659975
复制