2017-10-05 70 views
0

为什么注释prettyplotlib barchat和x轴标签偏离中心?prettyplotlib标签和注释偏离中心

使用prettyplotlib==0.1.7

如果我们创建与由第二参数所限定的x轴的正常条形图中,标签是公中心的条上:

%matplotlib inline 
import numpy as np 
import prettyplotlib as ppl 
import matplotlib.pyplot as plt 

fig, ax = plt.subplots(1) 

counter = {1:1, 2:4, 3:9, 4:16, 5:25, 6:36, 7:49} 

x, y = zip(*counter.items()) 

ppl.bar(ax, x , y, grid='y') 

[OUT]:

enter image description here

但是,如果我们使用xticklabels x轴标签熄灭中心:

ppl.bar(ax, x , y, xticklabels=list('1234567'), grid='y') 

[OUT]:

enter image description here

类似地,当我们使用annotate=True参数,它进入偏心:

ppl.bar(ax, x , y, annotate=True, grid='y') 

[OUT]:

enter image description here

它不像https://github.com/olgabot/prettyplotlib/wiki/Examples-with-code#hist上显示的例子

回答

1

我会建议不要再使用prettyplotlib。它已经3岁了,基本上所做的就是改变剧情的风格。直接使用matplotlib更好,如果您对样式不满意,请使用a different onecreate your own。如果遇到问题,关于改变风格的问题也很有可能在这里得到解答。

这是一种如何改变样式以重新创建上述问题的情节。

import matplotlib.pyplot as plt 

style = {"axes.grid" : True, 
     "axes.grid.axis" : "y", 
     "axes.spines.top" : False, 
     "axes.spines.right" : False, 
     "grid.color" : "white", 
     "ytick.left" : False, 
     "xtick.bottom" : False, 
     } 
plt.rcParams.update(style) 

counter = {1:1, 2:4, 3:9, 4:16, 5:25, 6:36, 7:49} 
x, y = zip(*counter.items()) 

fig, ax = plt.subplots(1) 
ax.bar(x , y, color="#66c2a5") 

plt.show() 

enter image description here

现在你可以自由设定不同的xticklabels,

ax.set_xticks(x) 
ax.set_xticklabels(list("ABCDEFG")) 

或注释的酒吧,

for i,j in zip(x,y): 
    ax.annotate(str(j), xy=(i,j), xytext=(0, 4),textcoords='offset points',ha="center") 

enter image description here

matplotlib文档维护得很好,这里有很多问题可以帮助你做特殊情况下的情节,如果有需要的话。

+0

谢谢@ImportanceOfBeingErnest!我已经搬到了'seaborn' +'matplotlib'。 – alvas