2017-10-05 137 views
1

如何控制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') 

enter image description here

我可以使用xticklabels参数:

ppl.bar(ax, list(reversed(x)) , y, annotate=True, xticklabels=list('7654321'), grid='y') 

但是,这并没有太多工作,它返回相同的权棒顺序错误x轴标签:

enter image description here

奇怪的是,当我逆转的xticklabels

列表
ppl.bar(ax, list(reversed(x)) , y, annotate=True, xticklabels=list('1234567'), grid='y') 

我有我需要的东西,但它现在的x列表逆转奇怪,但xticklabels不...

enter image description here

但如果我删除了逆转x名单:

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

我们得到的第一个相同的图作为ppl.bar(ax, x , y, annotate=True, grid='y') ...

enter image description here

+1

为什么不正确扭转轴? https://stackoverflow.com/questions/2051744/reverse-y-axis-in-pyplot此外prettyplotlib似乎已被放弃多年,开发建议使用seaborn来代替。 –

+0

啊...是的,seaborn维护得更好。 – alvas

+1

请注意,尽管seaborn为matplotlib增加了很多样式功能,但其主要目的是轻松地与熊猫进行交互。所有的seaborn样式都可以从matplotlib中随时获得,除非您知道自己在做什么,否则我会建议使用matplotlib创建平常的样式。 – ImportanceOfBeingErnest

回答

1

由于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 

结果正是你所需要的:既情节和标签也反转:

pretty result

这也是语义正确途径你的问题:你的数据仍然是一样的,你只希望它有不同的表现。

+0

谢谢@AndrasDeak!我继续使用'seaborn'虽然=) – alvas

+1

@alvas我同意这是正确的行动方案:)但由于你没有删除你的问题一段时间我想我应该添加一个答案,回答你的prettyplotlib问题;) –