Python 熊猫水平条图的修改
我已经使用PandasPython 熊猫水平条图的修改,python,pandas,matplotlib,Python,Pandas,Matplotlib,我已经使用Pandasplot功能组合了一个plot,但希望通过以下元素(如所需的输出绘图图像所示)帮助完成此功能: x轴上0处的垂直线 x轴上的10点增量 我希望OpenToLast条形图数据更加突出,因此如果可能的话,我们希望将其他堆叠的条形图淡入背景中 数据: 请参见DataFrame.to_dict()output 这就是我获取现有绘图的方式: auction[['OpenToLast','OpenToMaxHigh','OpenToMaxLow']].head(20).plot(kin
plot
功能组合了一个plot
,但希望通过以下元素(如所需的输出绘图图像所示)帮助完成此功能:
OpenToLast
条形图数据更加突出,因此如果可能的话,我们希望将其他堆叠的条形图淡入背景中DataFrame.to_dict()
output
这就是我获取现有绘图的方式:
auction[['OpenToLast','OpenToMaxHigh','OpenToMaxLow']].head(20).plot(kind='barh',
figsize=(7,10),
fontsize=10,
colormap ='winter',
stacked = True,
legend = True)
当前绘图:
所需输出:
我没有意识到我可以将Pandasplot
命令直接与Matplotlib API一起使用。现在,我已经从上面复制了代码,并对其进行了修改,以便在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')
尝试以下操作:
事实证明,最棘手的部分是着色,但绘制线条和更新记号相对简单(见代码末尾)
谢谢您看这个。我找到了一种获取输出(见上文)的方法,但会接受您的答案,并对其进行标记,因为Colormap
元素非常有趣,谢谢,您如何读取熊猫dict数据?我读过关于如何创建数据框的文章。to_dict()
,但我不确定你是如何解析它的?干杯同样喜欢start,end=axnum.get_xlim()
你所做的事情,干杯@bunjiglad来帮助你。pandas DataFrame构造函数可以将dictionary作为其第一个参数,因此如果您的dictionary被称为类似于my_data
的东西,您可以只执行pd.DataFrame(my_data)
您需要先显式导入Timestamp类(我猜您是从您提供的链接复制dictionary,对吗?)先从熊猫导入时间戳执行
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