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)])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值