首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python matplotlib编辑直方图

问python matplotlib编辑直方图
EN

Stack Overflow用户
提问于 2016-08-11 09:59:49
回答 1查看 1.7K关注 0票数 0

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

  1. 我怎样才能改变某些垃圾桶的颜色?详细地说,我想用:a更改回收箱的颜色。)值bin < 1.15红色,b.)值1.15 < bin < 1.25 c.)值> 1.25红?
  2. 我如何标出X轴不仅有小数1小数点的数字,而且还有2小数位数(现在根本没有标出)? 导入matplotlib.pyplot作为plt导入numpy作为np导入csv厚度= [] #从列表bins =matplotlib.pyplot获取厚度47,1.48,1.49,1.50 #开封箱(“Control.txt”,'r')作为csvfile: csv.reader=csvfile(csvfile,定界符=‘,’')用于绘图中的行:#x.append(浮(行))thickness.append(浮(行))thickness.append(浮(Row1))plt.hist(厚度,回收箱,对齐=‘左’,组型=‘bar’,rwidth=0.8,颜色=‘绿色’)plt.xlabel(‘厚度毫米’)plt.ylabel(‘频率’)plt.title(‘直方图’) plt.show()

见下图所示的直方图:

到目前为止plt.histogram

EN

回答 1

Stack Overflow用户

发布于 2016-08-11 10:05:15

像这样的东西(可能有一些bug):

代码语言:javascript
复制
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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38893125

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档