python 图形绘制----柱状图(Matplotlib)
python 图形绘制----柱状图(Matplotlib)
·
import numpy as np
from matplotlib import pyplot as plt
#设置中文
plt.rcParams['font.sans-serif']=['KaiTi']
plt.rcParams['axes.unicode_minus'] = False
x = ["a","b","c","d","e"]
y = np.random.randint(0,100,5)
plt.bar(x,y)
plt.title("学生人数")
plt.show()
import numpy as np
from matplotlib import pyplot as plt
#设置中文
plt.rcParams['font.sans-serif']=['KaiTi']
plt.rcParams['axes.unicode_minus'] = False
x = ["a","b","c","d","e"]
y1 = np.random.randint(10,20,5)
y2 = np.random.randint(10,20,5)
plt.bar(x,y1)
plt.bar(x,y2,bottom=y1)
plt.legend(labels=["男生","女生"],loc = "upper right")
plt.show()
import numpy as np
from matplotlib import pyplot as plt
#设置中文
plt.rcParams['font.sans-serif']=['KaiTi']
plt.rcParams['axes.unicode_minus'] = False
#数据条数
x1 = np.arange(5)
#随机获取第一 第二数据
y1 = np.random.randint(10,20,5)
y2 = np.random.randint(10,20,5)
#柱状图宽度(不是间隔)
bar_width = 0.3
plt.bar(x1,y1,bar_width)
plt.bar(x1+bar_width,y2,bar_width)
#设置数值
tick_label = ['a','b','c','d','e']
#设置y轴上的数值间隔
plt.xticks(x1+bar_width/2,tick_label)
plt.legend(labels=["男生","女生"],loc="upper right")
plt.show()
import numpy as np
from matplotlib import pyplot as plt
#设置中文
plt.rcParams['font.sans-serif']=['KaiTi']
plt.rcParams['axes.unicode_minus'] = False
#数据条数
x1 = np.arange(21)
#随机获取第一 第二数据
y1 = np.random.randint(50,200,21)
y2 = np.random.randint(50,200,21)
#柱状图宽度(不是间隔)
bar_width = 0.4
#设置数值
tabel = ["2002","2003","2004","2005","2006","2007","2008","2009","2010","2011","2012","2013",
"2014","2015","2016","2017","2018","2019","2020","2021","2022"]
#设置y轴上的数值间隔
plt.bar(x1,y1,bar_width)
plt.bar(x1+bar_width,y2,bar_width)
plt.xticks(x1+bar_width/2,tabel)
plt.legend(labels=["男生","女生"],loc="upper right")
plt.show()
更多推荐
已为社区贡献2条内容
所有评论(0)