【发布时间】:2020-09-18 03:21:05
【问题描述】:
这是我生成这些图形的代码
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = (13,6)
def generate_histogram(distribucion):
values = list(distribucion.values())
intensities = list(distribucion.keys())
# plotting configuration
figure = plt.figure()
axes = figure.add_axes([0,0,1,1])
histogram = axes.bar(intensities,values,width=14)
plt.xticks(intensities,intensities)
# title and label
axes.set_title('Intensity distribution')
axes.set_xlabel('Intensity (0 to 255)')
axes.set_ylabel('Probability')
# fix scale
plt.ylim(0, 0.55)
plt.autoscale(False)
# function that make the labels
def autolabel(rects):
"""Attach a text label above each bar in *rects*, displaying its height."""
for rect in rects:
height = round((float)(rect.get_height()),4)
axes.annotate('{}'.format(height),
xy=(rect.get_x() + rect.get_width() / 2, height),
xytext=(0, 3), # 3 points vertical offset
textcoords="offset points",
ha='center', va='bottom',fontsize=10)
# generate autolabels
autolabel(histogram)
# show graph
plt.show()
generate_histogram(distribution_1)
generate_histogram(distribution_2)
generate_histogram(distribution_3)
这是我正在使用的数据
distribution_1 = {221: 0.360416255051639, 238: 0.19880422092501124, 204: 0.08321239335428827, 187: 0.05222900763358779, 170: 0.048701841041760216, 153: 0.04666771441400988, 136: 0.04238796587337225, 119: 0.03527929950606197, 255: 0.008626852267624607, 102: 0.029128423888639426, 85: 0.025297709923664122, 68: 0.025161652447238437, 51: 0.02414683430624158, 34: 0.015194881005837449, 17: 0.004271216883700045, 0: 0.00047373147732375395}
distribution_2 = {221: 0.4157265379434216, 238: 0.19262191288729233, 204: 0.07130848675348002, 187: 0.04102649303996408, 170: 0.041006286484059275, 153: 0.04099775482712169, 136: 0.03805253704535249, 119: 0.03213920071845532, 102: 0.0272240682532555, 255: 0.007630893578805568, 85: 0.024198473282442748, 68: 0.024454422990570275, 51: 0.023817691962281097, 34: 0.015106870229007634, 17: 0.004220925011225864, 0: 0.00046744499326448137}
distribution_3 = {255: 0.4824301751234845, 221: 0.0699272563987427, 187: 0.06918679838347552, 170: 0.050990121239335426, 153: 0.04777503367759318, 238: 0.024907049842837897, 136: 0.04586124831612034, 119: 0.041772339470139204, 102: 0.034856757970363715, 85: 0.022533453075886844, 68: 0.03175348001796138, 51: 0.025796587337224966, 34: 0.02501930848675348, 17: 0.015535698248765155, 0: 0.008881903906600808, 204: 0.002772788504714863}
我想要这样的东西,标签转 90 度,这样它们就不会重叠
【问题讨论】:
-
你能分享你使用的数据吗?
-
抱歉耽搁了。我已经编辑过了
标签: python matplotlib