[英]Order of bars in prettyplotlib barchart
如何控制prettyplotlib
條形圖中prettyplotlib
順序?
從prettyplotlib==0.1.7
使用標准ppl.bar
我可以得到一個條形圖:
%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, annotate=True, grid='y')
但是,如果我想反轉 x 軸條,當我更改x
列表時,條的順序會反轉,但標簽不會反轉:
ppl.bar(ax, list(reversed(x)) , y, annotate=True, grid='y')
我可以使用xticklabels
參數:
ppl.bar(ax, list(reversed(x)) , y, annotate=True, xticklabels=list('7654321'), grid='y')
但這也不起作用,它返回相同的正確條形順序,錯誤的 x 軸標簽:
奇怪的是,當我反轉xticklabels
的列表時,
ppl.bar(ax, list(reversed(x)) , y, annotate=True, xticklabels=list('1234567'), grid='y')
我得到了我需要的東西,但現在x
列表被反轉了,但xticklabels
不是......
但是,如果我刪除了x
列表中的反向:
ppl.bar(ax, x , y, annotate=True, xticklabels=list('1234567'), grid='y')
我們得到第一個與ppl.bar(ax, x , y, annotate=True, grid='y')
...
由於prettyplotlib主要負責樣式,因此您的問題實際上並非特定於其使用,您需要使用原生matplotlib方法來反轉您的x軸:
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, annotate=True, grid='y')
ax.invert_xaxis() # <-- fix
結果正是您所需要的:繪圖和標簽都顛倒了:
這也是解決您的問題的語義正確方法:您的數據仍然相同,您只希望它以不同的方式表示。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.