繁体   English   中英

python matplotlib编辑直方图

[英]python matplotlib edit histogram

我使用matplotlib创建直方图。 仍然存在我自己或借助互联网无法解决的问题。

  1. 如何更改某些垃圾箱的颜色? 详细地说,我想使用以下方式更改垃圾箱的颜色:a。)值bin <1.15红色,b。)值1.15 <bin <1.25 c。)值> 1.25红色?

  2. 如何不仅用小数点后一位数字标记X轴,还用小数点后两位数字标记X轴(现在不作图)?

     import matplotlib.pyplot as plt import numpy as np import csv thickness = [] #gets thickness from list bins = [1.00,1.01,1.02,1.03,1.04,1.05,1.06,1.07,1.08,1.09,1.10,1.11,1.12,1.13,1.14,1.15,1.16,1.17,1.18,1.19,1.20,1.21,1.22,1.23,1.24,1.25,1.26,1.27,1.28,1.29,1.30,1.31,1.32,1.33,1.34,1.35,1.36,1.37,1.38,1.39,1.40,1.41,1.42,1.43,1.44,1.45,1.46,1.47,1.48,1.49,1.50 ] #set bins manuelly with open('control.txt','r') as csvfile: plots = csv.reader(csvfile, delimiter=',') for row in plots: #x.append(float(row[0])) thickness.append(float(row[1])) plt.hist(thickness, bins, align='left', histtype='bar', rwidth=0.8, color='green') plt.xlabel('thickness [mm]') plt.ylabel('frequency') plt.title('Histogram') plt.show() 

请参见下面绘制的直方图:

到目前为止的直方图

这样的事情(可能会有一些错误):

import matplotlib.pyplot as plt
import numpy as np
import csv
from matplotlib.ticker import FormatStrFormatter


thickness = []  #gets thickness from list
bins = [1.00,1.01,1.02,1.03,1.04,1.05,1.06,1.07,1.08,1.09,1.10,1.11,1.12,1.13,1.14,1.15,1.16,1.17,1.18,1.19,1.20,1.21,1.22,1.23,1.24,1.25,1.26,1.27,1.28,1.29,1.30,1.31,1.32,1.33,1.34,1.35,1.36,1.37,1.38,1.39,1.40,1.41,1.42,1.43,1.44,1.45,1.46,1.47,1.48,1.49,1.50
] #set bins manuelly

with open('control.txt','r') as csvfile:
plots = csv.reader(csvfile, delimiter=',')
for row in plots:
    #x.append(float(row[0]))
    thickness.append(float(row[1]))

h,bins = plt.hist(thickness, bins)
plt.clf()
fig, ax = plt.subplots()
ax.bar(bins[bins<1.2],h[bins<1.2],rwidth=0.8, color='red')
plt.bar(bins[np.logical_and(bins>1.2,bins<1.5)],h[np.logical_and(bins>1.2,bins<1.5)],rwidth=0.8, color='green')
ax.bar(bins[bins>1.5],h[bins>1.5],rwidth=0.8, color='red')

ax.set_xlabel('thickness [mm]')

ax.set_ylabel('frequency')
ax.set_title('Histogram')

ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f'))  
plt.show()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM