首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >潘达斯水平条形图的修正

潘达斯水平条形图的修正
EN

Stack Overflow用户
提问于 2017-01-15 10:11:42
回答 2查看 1K关注 0票数 2

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

  1. X轴上0处的一条垂直线。
  2. 在x轴上增加10点。
  3. 我希望OpenToLast栏数据更加突出,所以如果可能的话,希望将其他堆叠的条形图淡出后台?

数据:

请参见DataFrame.to_dict() output 这里

这就是我如何获得现有的plot

代码语言:javascript
运行
复制
auction[['OpenToLast','OpenToMaxHigh','OpenToMaxLow']].head(20).plot(kind='barh',
                        figsize=(7,10),
                        fontsize=10,
                        colormap ='winter',                                        
                        stacked = True,                                
                        legend = True)

当前绘图:

期望输出:

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-01-15 17:54:38

尝试以下几点:

原来最棘手的部分是着色,但绘制线条和更新滴答相对简单(请参阅代码的结尾)

代码语言:javascript
运行
复制
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

票数 2
EN

Stack Overflow用户

发布于 2017-01-15 17:44:04

我没有意识到我可以使用Matplotlib直接使用Pandas plot命令。现在,我已经从上面复制了代码,并对其进行了修改,以添加Matplotlib中的其他元素。

如果有人知道怎么做的话,最好在条形图上加一个梯度,但我会把这个问题记下来,回答如下:

代码语言:javascript
运行
复制
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')

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41659975

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档