1、柱状图
import matplotlib.pyplot as plt
import numpy as np
# 1.绘制柱状图
# 组织数据
data = np.array(((12878.264202908986, 1, 'Java'), (14092.694428484134, 2, 'Python'), (9000.0, 3, 'PHP')))
data = data.T
xlabel = data[2]
x = data[1].astype(np.float)
y = data[0].astype(np.float)
bars = plt.bar(x, y, width=0.3)
ax = plt.subplot()
ax.set_ylabel("salary/month")
ax.set_xlabel("positon")
ax.set_xticks(x)
ax.set_xticklabels(xlabel)
plt.grid(linestyle='--')
plt.title("IT positon salary")
# 设置颜色
i = 0
for bar in bars:
bar.set_color('#' + str(111111 + i))
i += 420212
pass
for x, y in zip(x, y):
plt.text(x, y+0.05, '{0}yuan/month'.format(np.floor(y)), ha='center', va='bottom')
plt.show()
2、饼图
import matplotlib.pyplot as plt
import numpy as np
# 用来正常显示中文标签
plt.rcParams['font.sans-serif'] = ['SimHei']
# 用来正常显示负号
plt.rcParams['axes.unicode_minus'] = False
# 画饼图
# 1、组织数据
data = np.arange(1, 6)/np.sum(np.arange(1, 6))
print(data)
labels = ['%s %.2f %%' % ("A" + str(y), np.float(x * 100)) for x, y in zip(data, range(len(data)))]
plt.pie(data, colors=['#008888','#006660', '#FF0000', '#00FF00', '#0000FF'], labels=labels)
plt.title("每类职位占比")
plt.show()
3、散点图
import matplotlib.pyplot as plt
import numpy as np
# 组织数据
x = np.random.randint(1, 100, 100)
y = np.random.randint(1, 100, 100)
plt.scatter(x, y , s=2, c=x, marker=',')
plt.show()
4、单图表多图形
import matplotlib.pyplot as plt
import numpy as np
# 1.组织数据
print(np.arange(1, 10, 0.1))
# linespace在绘图的时候用得比较多
xData = np.linspace(-2*np.pi, np.pi*2, 100)
print(xData)
yData = np.sin(xData)
yData2 = np.cos(xData)
plt.figure()
plt.plot(xData, yData, 'g--', label='sin')
plt.plot(xData, yData2, 'r--', label='cos')
plt.scatter(xData, yData, s=50, c='r', alpha=0.6)
plt.legend(loc='best')
plt.show()
5、多图表多图形
import matplotlib.pyplot as plt
import numpy as np
# figure
figure = plt.figure()
subplot = figure.add_subplot(2,2,1)
subplot.plot(np.arange(0,np.pi*2, 0.1), np.sin(np.arange(0,np.pi*2, 0.1)))
subplot.plot(np.arange(0,np.pi*2, 0.1), np.cos(np.arange(0,np.pi*2, 0.1)))
subplot.scatter(np.arange(0,np.pi*2, 0.1), np.cos(np.arange(0,np.pi*2, 0.1)), c='r', alpha=0.5, s=30)
subplot.grid(linestyle='--')
subplot2 = figure.add_subplot(2,2,2)
# 组织数据
data = np.array(((12878.264202908986, 1, 'Java'), (14092.694428484134, 2, 'Python'), (9000.0, 3, 'PHP')))
data = data.T
xlabel = data[2]
x = data[1].astype(np.float)
y = data[0].astype(np.float)
bars = subplot2.bar(x, y, width=0.3)
subplot2.set_ylabel("salary/month")
subplot2.set_xlabel("positon")
subplot2.set_xticks(x)
subplot2.set_xticklabels(xlabel)
subplot2.grid(linestyle='--')
# 设置颜色
i = 0
for bar in bars:
bar.set_color('#' + str(111111 + i))
i += 420212
pass
for x, y in zip(x, y):
subplot2.text(x, y+0.05, '{0}yuan/month'.format(np.floor(y)), ha='center', va='bottom')
subplot3 = figure.add_subplot(2,2,3)
data = np.arange(1, 6)/np.sum(np.arange(1, 6))
labels = ['A %.2f %%' % np.float(x * 100) for x in data]
subplot3.pie(data, colors=['#008888','#006660', '#FF0000', '#00FF00', '#0000FF'], labels=labels)
subplot4 = figure.add_subplot(2,2,4)
x = np.random.randint(1, 100, 100)
y = np.random.randint(1, 100, 100)
subplot4.scatter(x, y , s=2, c=x, marker=',')
plt.grid(linestyle='--')
plt.show()