Hom*_*lli 14 python matplotlib
我在以下片段中偶然发现,使用matplotlib创建水平条形图:
import matplotlib
from pylab import *
val = 3+10*rand(5) # the bar lengths
pos = arange(5)+.5 # the bar centers on the y axis
print pos
figure(1)
barh(pos,val, align='center')
yticks(pos, ('Tom', 'Dick', 'Harry', 'Slim', 'Jim'))
xlabel('Performance')
title('horizontal bar chart using matplotlib')
grid(True)
show()
Run Code Online (Sandbox Code Playgroud)
我想修改上面的脚本如下:
任何帮助(代码片段或链接),以帮助我进行上述修改将非常有帮助.
顺便说一句,如果我想制作堆叠的水平条(比如每个标签有3个堆叠的水平条),我如何修改上面的代码来绘制3个堆叠的水平条形图?
[[编辑]]
有人可以发布两个简短的代码片段,显示如何:
ev-*_*-br 22
import matplotlib
from pylab import *
val = 3-6*rand(5) # the bar lengths # changed your data slightly
pos = arange(5)+.5 # the bar centers on the y axis
print pos
figure(1)
barh(pos,val, align='center',height=0.1) # notice the 'height' argument
yticks(pos, ('Tom', 'Dick', 'Harry', 'Slim', 'Jim'))
gca().axvline(0,color='k',lw=3) # poor man's zero level
xlabel('Performance')
title('horizontal bar chart using matplotlib')
grid(True)
show()
Run Code Online (Sandbox Code Playgroud)
一般来说,我建议不要使用from pyplot import *.除非您处于交互模式,否则请使用面向对象的方法:
import matplotlib.pyplot as plt
from numpy.random import rand
from numpy import arange
val = 3-6*rand(5) # the bar lengths
pos = arange(5)+.5 # the bar centers on the y axis
print pos
fig = plt.figure()
ax = fig.add_subplot(111)
ax.barh(pos,val, align='center',height=0.1)
ax.set_yticks(pos, ('Tom', 'Dick', 'Harry', 'Slim', 'Jim'))
ax.axvline(0,color='k',lw=3) # poor man's zero level
ax.set_xlabel('Performance')
ax.set_title('horizontal bar chart using matplotlib')
ax.grid(True)
plt.show()
Run Code Online (Sandbox Code Playgroud)
画廊是各种情节的良好起点matplotlib
正如振亚所说,你必须调整你的情节.
例如,下面是一个生成自定义水平条形图的函数:
默认情况下,它会在右侧绘制类别(人员)的名称,但您当然可以更改它.
import numpy as np
import matplotlib.pyplot as plt
# creation of the data
name_list = ['day1', 'day2', 'day3', 'day4']
data = {name: 3+10*np.random.rand(5) for name in name_list}
colors_list = ['0.5', 'r', 'b', 'g'] #optional
def customize_barh(data, width_bar=1, width_space=0.5, colors=None):
n_measure = len(data) #number of measure per people
n_people = data[data.keys()[0]].size # number of people
#some calculation to determine the position of Y ticks labels
total_space = n_people*(n_measure*width_bar)+(n_people-1)*width_space
ind_space = n_measure*width_bar
step = ind_space/2.
pos = np.arange(step, total_space+width_space, ind_space+width_space)
# create the figure and the axes to plot the data
fig = plt.figure(figsize=(8,6))
ax = fig.add_axes([0.15, 0.15, 0.65, 0.7])
# remove top and right spines and turn ticks off if no spine
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('right') # ticks position on the right
# postition of tick out
ax.tick_params(axis='both', direction='out', width=3, length=6,
labelsize=24, pad=8)
ax.spines['left'].set_linewidth(3)
ax.spines['bottom'].set_linewidth(3)
# plot the data
for i,day in enumerate(data.keys()):
if colors == None:
ax.barh(pos-step+i*width_bar, data[day], width_bar, #facecolor='0.4',
edgecolor='k', linewidth=3)
else:
ax.barh(pos-step+i*width_bar, data[day], width_bar, facecolor=colors[i],
edgecolor='k', linewidth=3)
ax.set_yticks(pos)
# you may want to use the list of name as argument of the function to be more
# flexible (if you have to add a people)
ax.set_yticklabels(('Tom', 'Dick', 'Harry', 'Slim', 'Jim'))
ax.set_ylim((-width_space, total_space+width_space))
ax.set_xlabel('Performance', size=26, labelpad=10)
customize_barh(data, colors=colors_list)
plt.savefig('perf.png')
plt.show()
Run Code Online (Sandbox Code Playgroud)
产生:

| 归档时间: |
|
| 查看次数: |
32882 次 |
| 最近记录: |