# -*- coding: utf-8 -*-
"""
Created on Tue Mar 3 16:18:01 2020
@author: 35187
"""
import matplotlib.pyplot as plt
import numpy as np
num_list = [1.5,0.6,7.8,6] #Y值
num_label = ['Monday','Tuesday','Friday','Sunday'] #设置X轴值
plt.bar(num_label, num_list,0.5, color=['r','g','b','y']) #0.5表示条宽
plt.ylabel('Number of Students') #设置Y轴标签
plt.title('Bar') #设置X轴标签
plt.show()
index = np.arange(4)
num_Y1=[1.8,0.7,7.8,6.3]
num_Y2=[1,2.2,3.1,1]
labels=['boy','girl'] #设置标签
bar_width=0.2 #条宽
plt.ylabel('Number of Gender') #设置Y轴标签
plt.bar(index,num_Y1,bar_width,color='y')
plt.bar(index+bar_width,num_Y2,bar_width,tick_label=num_label,color='r') #水平叠加直方图,tick_label设置X轴值
plt.legend(labels,loc='upper right',ncol=1) #显示标记
plt.show()
运行结果:
来源:CSDN
作者:X&P
链接:https://blog.csdn.net/weixin_44031108/article/details/104636113