2017-10-05 68 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