matplotlib tutorial

example 1

# -*- coding: utf-8 -*-  
import numpy as np    
import matplotlib.pyplot as plt    

X=[0,1,2,3,4,5]  
Y=[222,42,455,664,454,334]    
fig = plt.figure()  
plt.bar(X,Y,width = 0.4,color="green")  
plt.xlabel("X-axis")  
plt.ylabel("Y-axis")  
plt.title("bar chart")  
plt.show()    
plt.savefig("barChart.jpg")  

example 2

import numpy as np
import matplotlib.pyplot as plt

N = 5
y1 = [20, 10, 30, 25, 15]
y2 = [15, 14, 34 ,10,5]
index = np.arange(5)

bar_width = 0.3
plt.bar(index , y1, width=0.3 , color='y')
plt.bar(index +  bar_width, y2, width=0.3 , color='b')
plt.show()

example 3

import numpy as np
import matplotlib.pyplot as plt
 N = 5
y = [20, 10, 30, 25, 15]
index = np.arange(5)
p1 = plt.bar(left=0,bottom=index , width=y, color='yellow',height=0.5, orientation='horizontal')
plt.show()

example 4 from dict

d = {'s':1,'ss':2}
plt.bar(range(len(d)), d.values(), align="center")
plt.xticks(range(len(d)), list(d.keys()))
plt.show()
x, y = d.keys(),d.values()
plot

questions: how to draw big elements in dict

from collections import OrderedDict

 # regular unsorted dictionary
d = {'banana': 3, 'apple':4, 'pear': 1, 'orange': 2}

 # dictionary sorted by key -- OrderedDict(sorted(d.items()) also works
a = OrderedDict(sorted(d.items(), key=lambda t: t[0]))
a['banana']
x, y = a.keys(),a.values()
OrderedDict(sorted(d.items(), key=lambda t: t[1],reverse = True))
>>> # regular unsorted dictionary
>>> d = {'banana': 3, 'apple': 4, 'pear': 1, 'orange': 2}

>>> # dictionary sorted by key
>>> OrderedDict(sorted(d.items(), key=lambda t: t[0]))
OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)])

>>> # dictionary sorted by value
>>> OrderedDict(sorted(d.items(), key=lambda t: t[1]))
OrderedDict([('pear', 1), ('orange', 2), ('banana', 3), ('apple', 4)])

>>> # dictionary sorted by length of the key string
>>> OrderedDict(sorted(d.items(), key=lambda t: len(t[0])))
OrderedDict([('pear', 1), ('apple', 4), ('orange', 2), ('banana', 3)])
阅读更多
个人分类: python
想对作者说点什么? 我来说一句

matplotlib中文教程

2017年11月08日 606KB 下载

没有更多推荐了,返回首页

不良信息举报

matplotlib tutorial

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭