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
d = {'banana': 3, 'apple':4, 'pear': 1, 'orange': 2}
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))
>>>
>>> d = {'banana': 3, 'apple': 4, 'pear': 1, 'orange': 2}
>>>
>>> OrderedDict(sorted(d.items(), key=lambda t: t[0]))
OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)])
>>>
>>> OrderedDict(sorted(d.items(), key=lambda t: t[1]))
OrderedDict([('pear', 1), ('orange', 2), ('banana', 3), ('apple', 4)])
>>>
>>> OrderedDict(sorted(d.items(), key=lambda t: len(t[0])))
OrderedDict([('pear', 1), ('apple', 4), ('orange', 2), ('banana', 3)])