python - Pandas中水平条形图的修改

标签 python pandas matplotlib

我已经使用 Pandas plot 功能组合了一个 plot,但希望能帮助您使用以下元素完成它(如所需的输出绘图图像所示):

  1. x 轴 0 处的垂直线。
  2. x 轴上 10 点增量。
  3. 我希望 OpenToLast 条形图数据更加突出,因此希望将其他堆叠条形图淡入背景(如果可能的话)?

数据:

请参阅DataFrame.to_dict()输出here .

这就是我获取现有的方式:

auction[['OpenToLast','OpenToMaxHigh','OpenToMaxLow']].head(20).plot(kind='barh',
                        figsize=(7,10),
                        fontsize=10,
                        colormap ='winter',                                        
                        stacked = True,                                
                        legend = True)

当前情节:

enter image description here

所需输出:

enter image description here

最佳答案

尝试以下操作:

事实证明,最棘手的部分是着色,但绘制线条和更新刻度相对简单(请参见代码末尾)

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

enter image description here

关于python - Pandas中水平条形图的修改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41659975/

相关文章:

python - 直方图条形宽度

python - Cron日志显示sh:1:hdfs:找不到

python - 使用 Python 的 Requests 库导航网页/点击按钮

python - 在 matplotlib 中设置分组条形图之间的间距

python - 计算数据框列中 True/False 的出现次数

python - 如何在 Pandas 中执行相当于相关子查询的操作

python - 多个数据的直方图中的条形高度和宽度

Python 参数解析 : separate options for positional arguments

python - 响应[412]使用requests python包访问该网页时,如何绕过?

python - 使用 pandas 或 blaze 从非常大的 CSV 文件中删除列