2017-06-19 95 views
-1

我想用mathplotlib创建水平barblot,但我遇到了几个问题。更改水平barplot大小matplotlib

  1. 我需要改变整个图的大小。目前它默认为800x600。我想自己设置参数,然后剧情就会调整大小。因为例如当前酒吧之间有太多的空白空间,我想放大酒吧的宽度。

  2. 如果文本(条形标题或数值)不适合该图,则会被忽略。我想扩展屏幕,使文本永远不会消失。

下面是样本图像的代码输出: enter image description here

这里的示例代码:

import numpy as np 
import matplotlib.pyplot as plt 

people = ('Tom sadf hasidfu hasdufhas d', 'Dick', 'Harry', 'Slim') 
y_pos = np.arange(len(people)) 
performance = 3 + 10 * np.random.rand(len(people)) 

plt.barh(y_pos, performance, align='center', height=0.3, color="skyblue") 
plt.yticks(y_pos, people) 
plt.xlabel('Performance') 
plt.title('How fast do you want to go today?') 

for i, v in enumerate(performance): 
    plt.text(v + 0.5, i, str(v), color='blue', fontweight='bold') 

#plt.show() 

filename = "myfilename.png" 
plt.savefig(filename) 

回答

1

有很多的方法改变图形的尺寸和调整的参数剧情。他们都可以使用选择的搜索引擎找到。

举个例子,图中的尺寸可以经由figsize参数改变,则ticklabels可以通过调用plt.tight_layout被包括并且所述限制可以通过plt.xlim进行设置。

import numpy as np 
import matplotlib.pyplot as plt 

people = ('Tom Hanswurst Fitzgerald', 'Dick', 'Harry', 'Slim') 
y_pos = np.arange(len(people)) 
performance = 3 + 10 * np.random.rand(len(people)) 

plt.figure(figsize=(8,4)) 
plt.barh(y_pos, performance, align='center', height=0.3, color="skyblue") 
plt.yticks(y_pos, people) 
plt.xlim(0,np.max(performance)*1.4) 
plt.xlabel('Performance') 
plt.title('How fast do you want to go today?') 

for i, v in enumerate(performance): 
    plt.text(v + 0.5, i, str(v), color='blue', fontweight='bold') 

plt.tight_layout() 
plt.show() 

enter image description here