python中用bokeh画热图为啥没数据的地方有颜色啊,不能变成没有颜色吗?
问题描述
用bokeh画热图,出来的图是有颜色的背景怎么变成白色的
问题出现的平台版本及自己尝试过哪些方法
相关代码
粘贴代码文本(请勿用截图)
from bokeh.plotting import figure, show, output_file
from bokeh.layouts import gridplot
from bokeh.models import ColorBar, BasicTicker, LinearColorMapper
import pandas as pd
import numpy as np
from bokeh.palettes import Viridis256
准备数据
file_path = r'D:\大数据工作ing\车辆数据\c095\WH1165.csv'
data = pd.read_csv(file_path)
数据清洗
cleaned_data = data[(data['enginespeed'] != 0) & (data['actualnettorq'] != 0)]
x = cleaned_data['enginespeed']
y = cleaned_data['actualnettorq']
创建颜色映射(可配置)
color_palette = Viridis256 # 更改颜色的调色板
color_low = 1 # 颜色映射的最小值
color_high = 1000 # 颜色映射的最大值
color_mapper = LinearColorMapper(palette=color_palette, low=color_low, high=color_high)
创建第一个热力图
p1 = figure(width=600, height=300, title='WH1165 - 数据分布热力图', x_range=(500, 5000), y_range=(-50, 200), x_axis_label='发动机转速', y_axis_label='扭矩离合器')
计算2D直方图
hist, xedges, yedges = np.histogram2d(x, y, bins=(300, 100))
创建遮罩,将没有数据的区域遮盖成白色
mask = np.ma.masked_where(hist == 0, hist)
p1.image(image=[mask.T], x=xedges[0], y=yedges[0], dw=xedges[-1] - xedges[0], dh=yedges[-1] - yedges[0], color_mapper=color_mapper)
color_bar1 = ColorBar(color_mapper=color_mapper, width=8, location=(0, 0), ticker=BasicTicker(desired_num_ticks=10))
p1.add_layout(color_bar1, 'right')
p1.background_fill_color = 'white'
创建第二个热力图
x2 = x[x > 100] # 过滤后的x值
y2 = y[x > 100] # 对应的y值
p2 = figure(width=600, height=300, title='过滤后的数据分布热力图', x_range=(500, 5000), y_range=(-50, 200), x_axis_label='发动机转速', y_axis_label='扭矩离合器')
计算2D直方图
hist2, xedges2, yedges2 = np.histogram2d(x2, y2, bins=(300, 100))
创建遮罩,将没有数据的区域遮盖成白色
mask2 = np.ma.masked_where(hist2 == 0, hist2)
p2.image(image=[mask2.T], x=xedges2[0], y=yedges2[0], dw=xedges2[-1] - xedges2[0], dh=yedges2[-1] - yedges2[0], color_mapper=color_mapper)
color_bar2 = ColorBar(color_mapper=color_mapper, width=8, location=(0, 0), ticker=BasicTicker(desired_num_ticks=10))
p2.add_layout(color_bar2, 'right')
创建第三个直方图
p3 = figure(width=600, height=300, title='车速分布')
hist3, edges3 = np.histogram(cleaned_data['vehiclespeed'], bins=50)
p3.quad(top=hist3, bottom=0, left=edges3[:-1], right=edges3[1:], fill_color='blue', line_color='white', alpha=0.7)
p3.xaxis.axis_label = '车速'
p3.yaxis.axis_label = '数量'
创建第四个直方图
p4 = figure(width=600, height=300, title='环境温度分布')
hist4, edges4 = np.histogram(cleaned_data['ambienttemp1'], bins=50)
p4.quad(top=hist4, bottom=0, left=edges4[:-1], right=edges4[1:], fill_color='blue', line_color='white', alpha=0.7, legend_label='环境温度分布')
p4.xaxis.axis_label = '环境温度'
p4.yaxis.axis_label = '数量'
p4.legend.title = '图例'
p4.x_range.start = 0
p4.x_range.end = 40
创建一个网格布局以容纳所有图表
layout = gridplot([[p1, p2], [p3, p4]])
保存图表为HTML文件并显示
output_file('bokeh_plots.html')
show(layout)
你期待的结果是什么?实际看到的错误信息又是什么?
以上是 python中用bokeh画热图为啥没数据的地方有颜色啊,不能变成没有颜色吗? 的全部内容, 来源链接: utcz.com/p/939009.html