簡體   English   中英

Prettyplotlib 條形圖中的條形順序

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM