posted on 2023-05-21 17:11 read(869) comment(0) like(25) collect(3)
from matplotlib import pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.rcParams['figure.dpi'] = 100
plt.rcParams['figure.figsize'] = (5,3)
matplotlib.pyplot.bar(x, height, width: float = 0.8, bottom = None, *, align: str = ‘center’, data = None, **kwargs)
import matplotlib.pyplot as plt
x = range(5)
data = [5, 20, 15, 25, 10]
plt.title("基本柱状图")
plt.grid(ls="--", alpha=0.5)
plt.bar(x, data)
import matplotlib.pyplot as plt
x = range(5)
data = [5, 20, 15, 25, 10]
plt.title("基本柱状图")
plt.grid(ls="--", alpha=0.5)
plt.bar(x, data, bottom=[10, 20, 5, 0, 10])
import matplotlib.pyplot as plt
x = range(5)
data = [5, 20, 15, 25, 10]
plt.title("设置柱状图颜色")
plt.grid(ls="--", alpha=0.5)
plt.bar(x, data ,facecolor="green")
#plt.bar(x, data ,color="green")
import matplotlib.pyplot as plt
x = range(5)
data = [5, 20, 15, 25, 10]
plt.title("color参数设置柱状图不同颜色")
plt.grid(ls="--", alpha=0.5)
plt.bar(x, data ,color=['r', 'g', 'b'])
import matplotlib.pyplot as plt
data = [5, 20, 15, 25, 10]
plt.title("设置边缘线条样式")
plt.bar(range(len(data)), data, ec='r', ls='--', lw=2)
countries = ['挪威', '德国', '中国', '美国', '瑞典']
gold_medal = [16, 12, 9, 8, 8]
silver_medal = [8, 10, 4, 10, 5]
bronze_medal = [13, 5, 2, 7, 5]
plt.bar(countries, gold_medal,color="gold")
plt.bar(countries,silver_medal,color="silver")
plt.bar(countries,bronze_medal,color="red")
x = np.arange(len(countries))
print(x)
width = 0.2
#[0 1 2 3 4]
gold_x = x
silver_x = x + width
bronze_x = x + 2 * width
plt.bar(gold_x,gold_medal,width=width,color="gold")
plt.bar(silver_x,silver_medal,width=width,color="silver")
plt.bar(bronze_x,bronze_medal,width=width, color="saddlebrown")
plt.xticks(x+width, labels=countries)
for i in range(len(countries)):
plt.text(gold_x[i],gold_medal[i], gold_medal[i],va="bottom",ha="center",fontsize=8)
plt.text(silver_x[i],silver_medal[i], gold_medal[i],va="bottom",ha="center",fontsize=8)
plt.text(bronze_x[i],bronze_medal[i], gold_medal[i],va="bottom",ha="center",fontsize=8)
plt.legend()
#库导入 from matplotlib import pyplot as plt import numpy as np #参数设置 plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False plt.rcParams['figure.dpi'] = 100 plt.rcParams['figure.figsize'] = (5,3) #国家和奖牌数据导入 countries = ['挪威', '德国', '中国', '美国', '瑞典'] gold_medal = [16, 12, 9, 8, 8] silver_medal = [8, 10, 4, 10, 5] bronze_medal = [13, 5, 2, 7, 5] #将横坐标国家转换为数值 x = np.arange(len(countries)) width = 0.2 #计算每一块的起始坐标 gold_x = x silver_x = x + width bronze_x = x + 2 * width #绘图 plt.bar(gold_x,gold_medal,width=width,color="gold",label="金牌") plt.bar(silver_x,silver_medal,width=width,color="silver",label="银牌") plt.bar(bronze_x,bronze_medal,width=width, color="saddlebrown",label="铜牌") #将横坐标数值转换为国家 plt.xticks(x + width,labels=countries) #显示柱状图的高度文本 for i in range(len(countries)): plt.text(gold_x[i],gold_medal[i], gold_medal[i],va="bottom",ha="center",fontsize=8) plt.text(silver_x[i],silver_medal[i], gold_medal[i],va="bottom",ha="center",fontsize=8) plt.text(bronze_x[i],bronze_medal[i], gold_medal[i],va="bottom",ha="center",fontsize=8) #显示图例 plt.legend(loc="upper right")
#库导入 from matplotlib import pyplot as plt import numpy as np #参数设置 plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False plt.rcParams['figure.dpi'] = 100 plt.rcParams['figure.figsize'] = (5,3) #国家和奖牌数据输入、柱状图宽度设置 countries = ['挪威', '德国', '中国', '美国', '瑞典'] gold_medal = np.array([16, 12, 9, 8, 8]) silver_medal = np.array([8, 10, 4, 10, 5]) bronze_medal = np.array([13, 5, 2, 7, 5]) width = 0.3 #绘图 plt.bar(countries, gold_medal, color='gold', label='金牌', bottom=silver_medal + bronze_medal,width=width) plt.bar(countries, silver_medal, color='silver', label='银牌', bottom=bronze_medal,width=width) plt.bar(countries, bronze_medal, color='#A0522D', label='铜牌',width=width) #设置y轴标签,图例和文本值 plt.ylabel('奖牌数') plt.legend(loc='upper right') for i in range(len(countries)): max_y = bronze_medal[i]+silver_medal[i]+gold_medal[i] plt.text(countries[i], max_y, max_y, va="bottom", ha="center")
plt.barh(y, width, height=0.8, left=None, *, align='center', **kwargs)
countries = ['挪威', '德国', '中国', '美国', '瑞典']
gold_medal = np.array([16, 12, 9, 8, 8])
plt.barh(countries, width=gold_medal)
movie = ['新蝙蝠侠', '狙击手', '奇迹笨小孩']
real_day1 = [4053, 2548, 1543]
real_day2 = [7840, 4013, 2421]
real_day3 = [8080, 3673, 1342]
#库导入 from matplotlib import pyplot as plt import numpy as np #参数设置 plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False plt.rcParams['figure.dpi'] = 100 plt.rcParams['figure.figsize'] = (5,3) #数据的输入 movie = ['新蝙蝠侠', '狙击手', '奇迹笨小孩'] real_day1 = np.array( [4053, 2548, 1543]) real_day2 = np.array([7840, 4013, 2421]) real_day3 = np.array([8080, 3673, 1342]) #y轴转换为数值型 num_y = np.arange(len(movie)) #设置同图形的高度 height = 0.2 #计算每个图形高度的起始位置 movie1_start_y = num_y movie2_start_y = num_y + height movie3_start_y = num_y + 2 * height #绘制图形 plt.barh(movie1_start_y, real_day1, height=height) plt.barh(movie2_start_y, real_day2, height=height) plt.barh(movie3_start_y, real_day3, height=height) # 计算宽度值和y轴值,替换y轴数据 plt.yticks(num_y + height, movie)
#库导入 from matplotlib import pyplot as plt import numpy as np #参数设置 plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False plt.rcParams['figure.dpi'] = 100 plt.rcParams['figure.figsize'] = (5,3) #数据的输入 movie = ['新蝙蝠侠', '狙击手', '奇迹笨小孩'] real_day1 = np.array( [4053, 2548, 1543]) real_day2 = np.array([7840, 4013, 2421]) real_day3 = np.array([8080, 3673, 1342]) #确定距离左侧 left_day2 = real_day1 left_day3 = real_day1 + real_day2 # 设置线条高度 height = 0.2 # 绘制图形: plt.barh(movie, real_day1, height=height) plt.barh(movie, real_day2, left=left_day2, height=height) plt.barh(movie, real_day3, left=left_day3, height=height) # 设置数值文本,计算宽度值和y轴为值 sum_data = real_day1 + real_day2 +real_day3 for i in range(len(movie)): plt.text(sum_data[i], movie[i], sum_data[i],va="center" , ha="left")
柱状图 | 直方图 |
---|---|
柱状图一般用于描述离散型分类数据的对比 | 直方图一般用于描述连续型数据的分布关系 |
每根柱子宽度固定,柱子之间会有间距 | 每根柱子宽度可以不一样,且一般没有间距 |
横轴变量可以任意排序 | 横轴变量有一定顺序规则 |
plt.hist(x, bins=None, range=None, density=None, weights=None, cumulative=False, bottom=None, histtype='bar', align='mid', orientation='vertical', rwidth=None, log=False, color=None, label=None, stacked=False, normed=None, *, data=None, **kwargs)
x_value = np.random.randint(140,180,300)
plt.hist(x_value, bins=10, edgecolor='white')
plt.title("数据统计")
plt.xlabel("身高")
plt.ylabel("比率")
num
#array([25., 28., 34., 39., 29., 25., 37., 34., 26., 23.])
bins_limit
#array([140. , 143.9, 147.8, 151.7, 155.6, 159.5, 163.4, 167.3, 171.2,
# 175.1, 179. ])
for i in patches:
print(i)
print(i.get_x())
print(i.get_y())
print(i.get_height())
print(i.get_width())
patches[0].get_width()
#3.9000000000000057
#创建一个画布
fig, ax = plt.subplots()
# 绘制直方图
num,bins_limit,patches = ax.hist(x_value, bins=10, edgecolor='white')
# 注意num返回的个数是10,bins_limit返回的个数为11,需要截取
print(bins_limit[:-1])
# 曲线图
ax.plot(bins_limit[:10], num, '--',marker="o")
plt.xticks(bins_limit,rotation=45)
fig, ax = plt.subplots()
x = np.random.normal(100,20,100)
bins = [50, 60, 70, 90, 100,110, 140, 150]
ax.hist(x, bins, color="g",rwidth=0.5)
ax.set_title('不等距分组')
plt.show()
n_bins=10
fig,ax=plt.subplots(figsize=(8,5))
x_multi = [np.random.randn(n) for n in [10000, 5000, 2000]]
ax.hist(x_multi, n_bins, histtype='bar',label=list("ABC"))
ax.set_title('多类型直方图')
ax.legend()
x_value = np.random.randint(140,180,200)
x2_value = np.random.randint(140,180,200)
plt.hist([x_value,x2_value],bins=10, stacked=True)
#([array([16., 23., 27., 22., 13., 22., 18., 21., 18., 20.]),
# array([39., 46., 44., 35., 33., 47., 41., 42., 33., 40.])],
# array([140. , 143.9, 147.8, 151.7, 155.6, 159.5, 163.4, 167.3, 171.2,
# 175.1, 179. ]),
pyplot.pie(x, explode=None, labels=None, colors=None, autopct=None)
plt.rcParams['figure.figsize'] = (5,5)
labels = ['娱乐','育儿','饮食','房贷','交通','其它']
x = [200,500,1200,7000,200,900]
plt.pie(x,labels=labels)
#[Text(1.09783,0.0690696,'娱乐'),
# Text(1.05632,0.30689,'育儿'),
# Text(0.753002,0.801865,'饮食'),
# Text(-1.06544,-0.273559,'房贷'),
# Text(0.889919,-0.646564,'交通'),
# Text(1.05632,-0.30689,'其它')])
labels = ['娱乐','育儿','饮食','房贷','交通','其它'] x = [200,500,1200,7000,200,900] plt.title("饼图示例-8月份家庭支出") plt.pie(x,labels=labels,autopct='%.2f%%') #[Text(1.09783,0.0690696,'娱乐'), # Text(1.05632,0.30689,'育儿'), # Text(0.753002,0.801865,'饮食'), # Text(-1.06544,-0.273559,'房贷'), # Text(0.889919,-0.646564,'交通'), # Text(1.05632,-0.30689,'其它')], # [Text(0.598816,0.0376743,'2.00%'), # Text(0.576176,0.167395,'5.00%'), # Text(0.410728,0.437381,'12.00%'), # Text(-0.58115,-0.149214,'70.00%'), # Text(0.48541,-0.352671,'2.00%'), # Text(0.576176,-0.167395,'9.00%')])
labels = ['娱乐','育儿','饮食','房贷','交通','其它']
x = [200,500,1200,7000,200,900]
explode = (0.03,0.05,0.06,0.04,0.08,0.21)
plt.pie(x,labels=labels,autopct='%3.2f%%',explode=explode)
labels = ['娱乐','育儿','饮食','房贷','交通','其它']
x = [200,500,1200,7000,200,900]
explode = (0.03,0.05,0.06,0.04,0.08,0.1)
plt.pie(x,labels=labels,autopct='%3.2f%%',explode=explode, labeldistance=1.35, pctdistance=1.2)
labels = ['娱乐','育儿','饮食','房贷','交通','其它']
x = [200,500,1200,7000,200,900]
explode = (0.03,0.05,0.06,0.04,0.08,0.1)
plt.pie(x,labels=labels,autopct='%3.2f%%',explode=explode, labeldistance=1.35, pctdistance=1.2)
plt.legend()
Author:Fiee
link:http://www.pythonblackhole.com/blog/article/25328/9fbb1d1c669edf40e0ac/
source:python black hole net
Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.
name:
Comment content: (supports up to 255 characters)
Copyright © 2018-2021 python black hole network All Rights Reserved All rights reserved, and all rights reserved.京ICP备18063182号-7
For complaints and reports, and advertising cooperation, please contact vgs_info@163.com or QQ3083709327
Disclaimer: All articles on the website are uploaded by users and are only for readers' learning and communication use, and commercial use is prohibited. If the article involves pornography, reactionary, infringement and other illegal information, please report it to us and we will delete it immediately after verification!