为grid设置背景色(X5中)

1.对于计算字段加上背景色来区别填写的与非填写的,需要在添加背景色的类设置onRend 属性去调相应的JS函数来实现。

 

        <xui:column id="gridColumn4" ref="fOriginalCurrency" label="原币金额(万元)" width="100"
          format="0,000.000" align="right" type="html" onRender="onRender"/> 
        <xui:column id="gridColumn5" ref="fDollarAmountOff" label="折美元金额" width="100"
          format="0,000.000" align="right" type="html" onRender="onRender"/>

 

        <xui:column id="gridColumn9" ref="fOriginalCurrency" label="原币金额(万元)" width="100"
          format="0,000.000" align="right" type="html" onRender="onRender1"/> 
        <xui:column id="gridColumn10" ref="fDollarAmountOff" label="折美元金额" width="100"
          format="0,000.000" align="right" type="html" onRender="onRender1"/>

 

 

function onRender(event) {
 event.cell.setBgColor("#eeece1");
 var data = justep.xbl("dataDetail");
 return getColTyle(event, data);

};
function onRender1(event) {
 event.cell.setBgColor("#eeece1");
 var data = justep.xbl("bizData1");
 return getColTyle(event, data);

};

function getColTyle(event, data) {
 var ids = event.source.columnIds;
 var colInfo = data.getColumnInfo(ids[event.cell.cell.sourceIndex - 1]);
 var s = colInfo.type;
 if (s == "String" || s == "Date" || s == "DateTime") {
  return event.value;

 } else {
  return pmcSystem.formatAsText("number", "0,000.000", event.value);

 }
}

 

 

 

任务描述 本关任务:熟悉 matplotlib 基础知识完成挑战。 相关知识 为了完成本关任务,你需要掌握:如何安装并导入 matplotlib 库。 配置参数: axex: 设置坐标轴边界和表面的颜色、坐标刻度值大小和网格的显示; figure: 控制dpi、边界颜色、图形大小、和子区( subplot)设置; font: 字体集(font family)、字体大小和样式设置grid: 设置网格颜色和线性; legend: 设置图例和其中的文本的显示; line: 设置线条(颜色、线型、宽度等)和标记; patch: 是填充2D空间的图形对象,如多边形和圆。控制线宽、颜色和抗锯齿设置等; savefig: 可以对保存的图形进行单独设置。例如,设置渲染的文件的背景为白色; verbose: 设置matplotlib在执行期间信息输出,如silent、helpful、debug和debug-annoying; xticks和yticks: 为x,y轴的主刻度和次刻度设置颜色、大小、方向,以及标签大小。 线条相关属性标记设置 线条风格linestyle或ls 描述 ‘-‘ 实线 ‘:’ 虚线 ‘–’ 破折线 ‘None’,’ ‘,’’ 什么都不画 ‘-.’ 点划线 线条标记 标记maker 描述 ‘o’ 圆圈 ‘.’ 点 ‘D’ 菱形 ‘s’ 正方形 ‘h’ 六边形1 ‘*’ 星号 ‘H’ 六边形2 ‘d’ 小菱形 ‘_’ 水平线 ‘v’ 一角朝下的三角形 ‘8’ 八边形 ‘<’ 一角朝左的三角形 ‘p’ 五边形 ‘>’ 一角朝右的三角形 ‘,’ 像素 ‘^’ 一角朝上的三角形 ‘+’ 加号 ‘\ ‘ 竖线 ‘None’,’’,’ ‘ 无 ‘x’ X 颜色 别名 颜色 b 蓝色 g 绿色 r 红色 y 黄色 c 青色 k 黑色 m 洋红色 w 白色 如果这两种颜色不够用,还可以通过两种其他方式来定义颜色值: 使用HTML十六进制字符串 color='#123456' 使用合法的HTML颜色名字('red','chartreuse'等); 也可以传入一个归一化到[0,1]的RGB元祖。color=(0.3,0.3,0.4); 背景色 通过向如matplotlib.pyplot.axes()或者matplotlib.pyplot.subplot()这样的方法提供一个axisbg参数,可以指定坐标这的背景色。 subplot(111,axisbg=(0.1843,0.3098,0.3098)) 以下示例需要引入的库包括 import numpy as np import pandas as pd import matplotlib.pyplot as plt from matplotlib.ticker import MultipleLocator 绘图操作步骤(以点图、线图为例) #使用numpy产生数据 x=np.arange(-5,5,0.1) y=x*3 #创建窗口、子图 #方法1:先创建窗口,再创建子图。(一定绘制) fig = plt.figure(num=1, figsize=(15, 8),dpi=80) #开启一个窗口,同时设置大小,分辨率 ax1 = fig.add_subplot(2,1,1) #通过fig添加子图,参数:行数,列数,第几个。 ax2 = fig.add_subplot(2,1,2) #通过fig添加子图,参数:行数,列数,第几个。 print(fig,ax1,ax2) #方法2:一次性创建窗口和多个子图。(空白不绘制) fig,axarr = plt.subplots(4,1) #开一个新窗口,并添加4个子图,返回子图数组 ax1 = axarr[0] #通过子图数组获取一个子图 print(fig,ax1) #方法3:一次性创建窗口和一个子图。(空白不绘制) ax1 = plt.subplot(1,1,1,facecolor='white') #开一个新窗口,创建1个子图。facecolor设置背景颜色 print(ax1) #获取对窗口的引用,适用于上面三种方法 # fig = plt.gcf() #获得当前figure # fig=ax1.figure #获得指定子图所属窗口 # fig.subplots_adjust(left=0) #设置窗口左内边距为0,即左边留白为0。 #设置子图的基本元素 ax1.set_title('python-drawing') #设置图体,plt.title ax1.set_xlabel('x-name') #设置x轴名称,plt.xlabel ax1.set_ylabel('y-name') #设置y轴名称,plt.ylabel plt.axis([-6,6,-10,10]) #设置横纵坐标轴范围,这个在子图中被分解为下面两个函数 ax1.set_xlim(-5,5) #设置横轴范围,会覆盖上面的横坐标,plt.xlim ax1.set_ylim(-10,10) #设置纵轴范围,会覆盖上面的纵坐标,plt.ylim xmajorLocator = MultipleLocator(2) #定义横向主刻度标签的刻度差为2的倍数。就是隔几个刻度才显示一个标签文本 ymajorLocator = MultipleLocator(3) #定义纵向主刻度标签的刻度差为3的倍数。就是隔几个刻度才显示一个标签文本 ax1.xaxis.set_major_locator(xmajorLocator) #x轴 应用定义的横向主刻度格式。如果不应用将采用默认刻度格式 ax1.yaxis.set_major_locator(ymajorLocator) #y轴 应用定义的纵向主刻度格式。如果不应用将采用默认刻度格式 ax1.xaxis.grid(True, which='major') #x坐标轴的网格使用定义的主刻度格式 ax1.yaxis.grid(True, which='major') #x坐标轴的网格使用定义的主刻度格式 ax1.set_xticks([]) #去除坐标轴刻度 ax1.set_xticks((-5,-3,-1,1,3,5)) #设置坐标轴刻度 ax1.set_xticklabels(labels=['x1','x2','x3','x4','x5'],rotation=-30,fontsize='small') #设置刻度的显示文本,rotation旋转角度,fontsize字体大小 plot1=ax1.plot(x,y,marker='o',color='g',label='legend1') #点图:marker图标 plot2=ax1.plot(x,y,linestyle='--',alpha=0.5,color='r',label='legend2') #线图:linestyle线性,alpha透明度,color颜色,label图例文本 ax1.legend(loc='upper left') #显示图例,plt.legend() ax1.text(2.8, 7, r'y=3*x') #指定位置显示文字,plt.text() ax1.annotate('important point', xy=(2, 6), xytext=(3, 1.5), #添加标注,参数:注释文本、指向点、文字位置、箭头属性 arrowprops=dict(facecolor='black', shrink=0.05), ) #显示网格。which参数的值为major(只绘制大刻度)、minor(只绘制小刻度)、both,默认值为major。axis为'x','y','both' ax1.grid(b=True,which='major',axis='both',alpha= 0.5,color='skyblue',linestyle='--',linewidth=2) axes1 = plt.axes([.2, .3, .1, .1], facecolor='y') #在当前窗口添加一个子图,rect=[左, 下, 宽, 高],是使用的绝对布局,不和以存在窗口挤占空间 axes1.plot(x,y) #在子图上画图 plt.savefig('aa.jpg',dpi=400,bbox_inches='tight') #savefig保存图片,dpi分辨率,bbox_inches子图周边白色空间的大小 plt.show() #打开窗口,对于方法1创建在窗口一定绘制,对于方法2方法3创建的窗口,若坐标系全部空白,则不绘制 plot时可以设置的属性包括如下: 属性 值类型 alpha 浮点值 animated [True / False] antialiased or aa [True / False] clip_box matplotlib.transform.Bbox 实例 clip_on [True / False] clip_path Path 实例, Transform,以及Patch实例 color or c 任何 matplotlib 颜色 contains 命中测试函数 dash_capstyle ['butt' / 'round' / 'projecting'] dash_joinstyle ['miter' / 'round' / 'bevel'] dashes 以点为单位的连接/断开墨水序列 data (np.array xdata, np.array ydata) figure matplotlib.figure.Figure 实例 label 任何字符串 linestyle or ls [ '-' / '--' / '-.' / ':' / 'steps' / ...] linewidth or lw 以点为单位的浮点值 lod [True / False] marker [ '+' / ',' / '.' / '1' / '2' / '3' / '4' ] markeredgecolor or mec 任何 matplotlib 颜色 markeredgewidth or mew 以点为单位的浮点值 markerfacecolor or mfc 任何 matplotlib 颜色 markersize or ms 浮点值 markevery [ None / 整数值 / (startind, stride) ] picker 用于交互式线条选择 pickradius 线条的拾取选择半径 solid_capstyle ['butt' / 'round' / 'projecting'] solid_joinstyle ['miter' / 'round' / 'bevel'] transform matplotlib.transforms.Transform 实例 visible [True / False] xdata np.array ydata np.array zorder 任何数值 一个窗口多个图 #一个窗口,多个图,多条数据 sub1=plt.subplot(211,facecolor=(0.1843,0.3098,0.3098)) #将窗口分成2行1列,在第1个作图,并设置背景色 sub2=plt.subplot(212) #将窗口分成2行1列,在第2个作图 sub1.plot(x,y) #绘制子图 sub2.plot(x,y) #绘制子图 axes1 = plt.axes([.2, .3, .1, .1], facecolor='y') #添加一个子坐标系,rect=[左, 下, 宽, 高] plt.plot(x,y) #绘制子坐标系, axes2 = plt.axes([0.7, .2, .1, .1], facecolor='y') #添加一个子坐标系,rect=[左, 下, 宽, 高] plt.plot(x,y) plt.show() 极坐标 属性设置同点图、线图中。 fig = plt.figure(2) #新开一个窗口 ax1 = fig.add_subplot(1,2,1,polar=True) #启动一个极坐标子图 theta=np.arange(0,2*np.pi,0.02) #角度数列值 ax1.plot(theta,2*np.ones_like(theta),lw=2) #画图,参数:角度,半径,lw线宽 ax1.plot(theta,theta/6,linestyle='--',lw=2) #画图,参数:角度,半径,linestyle样式,lw线宽 ax2 = fig.add_subplot(1,2,2,polar=True) #启动一个极坐标子图 ax2.plot(theta,np.cos(5*theta),linestyle='--',lw=2) ax2.plot(theta,2*np.cos(4*theta),lw=2) ax2.set_rgrids(np.arange(0.2,2,0.2),angle=45) #距离网格轴,轴线刻度和显示位置 ax2.set_thetagrids([0,45,90]) #角度网格轴,范围0-360度 plt.show() 柱形图 属性设置同点图、线图中。 plt.figure(3) x_index = np.arange(5) #柱的索引 x_data = ('A', 'B', 'C', 'D', 'E') y1_data = (20, 35, 30, 35, 27) y2_data = (25, 32, 34, 20, 25) bar_width = 0.35 #定义一个数字代表每个独立柱的宽度 rects1 = plt.bar(x_index, y1_data, width=bar_width,alpha=0.4, color='b',label='legend1') #参数:左偏移、高度、柱宽、透明度、颜色、图例 rects2 = plt.bar(x_index + bar_width, y2_data, width=bar_width,alpha=0.5,color='r',label='legend2') #参数:左偏移、高度、柱宽、透明度、颜色、图例 #关于左偏移,不用关心每根柱的中心不中心,因为只要把刻度线设置在柱的中间就可以了 plt.xticks(x_index + bar_width/2, x_data) #x轴刻度线 plt.legend() #显示图例 plt.tight_layout() #自动控制图像外部边缘,此方法不能够很好的控制图像间的间隔 plt.show() 直方图 fig,(ax0,ax1) = plt.subplots(nrows=2,figsize=(9,6)) #在窗口上添加2个子图 sigma = 1 #标准差 mean = 0 #均值 x=mean+sigma*np.random.randn(10000) #正态分布随机数 ax0.hist(x,bins=40,normed=False,histtype='bar',facecolor='yellowgreen',alpha=0.75) #normed是否归一化,histtype直方图类型,facecolor颜色,alpha透明度 ax1.hist(x,bins=20,normed=1,histtype='bar',facecolor='pink',alpha=0.75,cumulative=True,rwidth=0.8) #bins柱子的个数,cumulative是否计算累加分布,rwidth柱子宽度 plt.show() #所有窗口运行 散点图 fig = plt.figure(4) #添加一个窗口 ax =fig.add_subplot(1,1,1) #在窗口上添加一个子图 x=np.random.random(100) #产生随机数组 y=np.random.random(100) #产生随机数组 ax.scatter(x,y,s=x*1000,c='y',marker=(5,1),alpha=0.5,lw=2,facecolors='none') #x横坐标,y纵坐标,s图像大小,c颜色,marker图片,lw图像边框宽度 plt.show() #所有窗口运行 三维图 fig = plt.figure(5) ax=fig.add_subplot(1,1,1,projection='3d') #绘制三维图 x,y=np.mgrid[-2:2:20j,-2:2:20j] #获取x轴数据,y轴数据 z=x*np.exp(-x**2-y**2) #获取z轴数据 ax.plot_surface(x,y,z,rstride=2,cstride=1,cmap=plt.cm.coolwarm,alpha=0.8) #绘制三维图表面 ax.set_xlabel('x-name') #x轴名称 ax.set_ylabel('y-name') #y轴名称 ax.set_zlabel('z-name') #z轴名称 plt.show() 画矩形、多边形、圆形和椭圆 fig = plt.figure(6) #创建一个窗口 ax=fig.add_subplot(1,1,1) #添加一个子图 rect1 = plt.Rectangle((0.1,0.2),0.2,0.3,color='r') #创建一个矩形,参数:(x,y),width,height circ1 = plt.Circle((0.7,0.2),0.15,color='r',alpha=0.3) #创建一个椭圆,参数:中心点,半径,默认这个圆形会跟随窗口大小进行长宽压缩 pgon1 = plt.Polygon([[0.45,0.45],[0.65,0.6],[0.2,0.6]]) #创建一个多边形,参数:每个顶点坐标 ax.add_patch(rect1) #将形状添加到子图上 ax.add_patch(circ1) #将形状添加到子图上 ax.add_patch(pgon1) #将形状添加到子图上 fig.canvas.draw() #子图绘制 plt.show() 编程要求 根据提示,在右侧编辑器补充代码,画出一个红色爱心图。其中函数function(x,y,color)的三个参数分别为: x: x轴数据 y: y轴数据 color: 线的颜色 请在指定位置补充代码,使其能正确运行并画出一颗爱心。 提示 请使用plt.plot()方法绘图并设置其线的颜色为红色。 测试说明 平台会对你编写的代码进行测试,输出结果一致则通关。 开始你的任务吧,祝你成功!
最新发布
12-11
% 磨矿背景下磨机特性曲线图 % 包含磨机功率、轴压和磨机卫士曲线,共用x轴和y轴 % 清除工作区和命令窗口 clear; clc; close all; % 物料填充率范围 (0% 到 100%) filling_rate = 0:1:100; % 1. 磨机功率曲线 power = -0.012*(filling_rate - 40).^2 + 30; power(power < 0) = 0; % 确保功率值为正 % 计算关键点位 x5_idx = find(filling_rate == 5); % x=5处的索引 power_x5 = power(x5_idx); % x=5处的功率值 peak_idx = find(filling_rate == 40); % 峰值点索引(x=40) peak_power = power(peak_idx); % 峰值功率值 end_x = 75; end_idx = find(filling_rate == end_x);% 终点索引(x=75) % 2. 轴压曲线(直线) % 起点:x=5,y=power_x5;经过峰值点x=40,y=peak_power k_axial = (peak_power - power_x5) / (filling_rate(peak_idx) - filling_rate(x5_idx)); % 斜率 b_axial = power_x5 - k_axial * filling_rate(x5_idx); % 截距 axial_pressure = k_axial * filling_rate + b_axial; % 3. 磨机卫士曲线(分段曲线) % 初始化卫士曲线数组 guard = NaN(size(filling_rate)); % 前段:x=5到x=40,下凹曲线,位于功率和轴压曲线之间 front_idx = filling_rate >= 5 & filling_rate <= 40; % 计算介于功率和轴压之间的曲线 % 使用加权平均方法:权重从0.7线性变化到0.5,确保曲线在两者之间 weights = 0.7 - 0.2*(filling_rate(front_idx) - 5)/35; % 权重从0.7渐变到0.5 guard(front_idx) = weights .* axial_pressure(front_idx) + (1-weights) .* power(front_idx); % 后段:x=40到x=75,上凹曲线(类似开口向上二次曲线的右半部分) back_idx = filling_rate > 40 & filling_rate <= 75; % 二次函数形式:y = a*(x-40)^2 + 30,a>0确保开口向上(上凹) a = 0.05; % 正系数确保上凹特性,值越大弯曲越明显 guard(back_idx) = a * (filling_rate(back_idx) - 40).^2 + peak_power; % 确保在峰值点处平滑过渡(微调后段起点) guard(back_idx(filling_rate(back_idx)==41)) = guard(front_idx(end)); % 筛选绘制范围:只绘制x=5到x=75之间的数据 plot_idx = filling_rate >= 5 & filling_rate <= 75; filling_plot = filling_rate(plot_idx); power_plot = power(plot_idx); axial_plot = axial_pressure(plot_idx); guard_plot = guard(plot_idx); % 创建图形 figure(‘Name’,‘磨机特性曲线’,‘Position’,[100 100 900 600]); % 绘制三条曲线并保存句柄 h_power = plot(filling_plot, power_plot, ‘b-’, ‘LineWidth’, 2.5); hold on; h_axial = plot(filling_plot, axial_plot, ‘r–’, ‘LineWidth’, 2); h_guard = plot(filling_plot, guard_plot, ‘g-.’, ‘LineWidth’, 2); % % 标记关键点位 % plot(filling_rate(x5_idx), power_x5, ‘ko’, ‘MarkerSize’, 8, ‘MarkerFaceColor’, ‘k’); % text(filling_rate(x5_idx)+1, power_x5+1, ['起点: (5, ', num2str(power_x5, ‘%.1f’), ‘)’], … % ‘Color’,‘k’, ‘FontSize’,10); % % plot(filling_rate(peak_idx), peak_power, ‘mo’, ‘MarkerSize’, 8, ‘MarkerFaceColor’, ‘m’); % text(filling_rate(peak_idx)+1, peak_power+1, ['峰值点: (40, ', num2str(peak_power), ‘)’], … % ‘Color’,‘m’, ‘FontSize’,10); % % 标记终点 % text(end_x-10, power(end_idx)-2, ['终点: (75, ', num2str(power(end_idx), ‘%.1f’), ‘)’], … % ‘Color’,‘k’, ‘FontSize’,10); % 设置坐标轴和标题 xlabel(‘物料填充率 (%)’,‘FontSize’,12); ylabel(‘功率 (kW)’,‘FontSize’,12); title(‘磨机功率、轴压和磨机卫士随物料填充率变化曲线’,‘FontSize’,14); % 设置坐标轴范围 xlim([0 80]); % x轴范围0到80 ylim([10 max(power)*1.5]); % 添加图例 legend([h_power, h_axial, h_guard], … {‘磨机功率曲线’, ‘轴压曲线’, ‘磨机卫士曲线’}, … ‘Location’,‘best’,‘FontSize’,10); % 添加网格和边框 grid on; % box on; box off % % 确保中文正常显示 % set(gca, ‘FontName’, ‘SimHei’); % 确保中文正常显示并隐藏坐标轴数值 ax = gca; ax.FontName = ‘SimHei’; % 设置字体支持中文 ax.XTickLabel = {}; % 隐藏x轴数值标签 ax.YTickLabel = {}; % 隐藏y轴数值标签 ax.XAxis.Visible = ‘on’; % 保持x轴线可见 ax.YAxis.Visible = ‘on’; % 保持y轴线可见 ax.FontSize = 10; % 设置合适的字体大小 hold off; % 隐藏坐标轴线 ax = gca; ax.XAxis.Visible = ‘off’; ax.YAxis.Visible = ‘off’; % 获取当前坐标轴范围 xlims = ax.XLim; ylims = ax.YLim; % 获取坐标轴位置(归一化单位) ax_pos = ax.Position; % 转换函数:数据坐标到归一化图形坐标 ds2nfu = @(x,y) [ax_pos(1) + (x - xlims(1)) / (xlims(2)-xlims(1)) * ax_pos(3), … ax_pos(2) + (y - ylims(1)) / (ylims(2)-ylims(1)) * ax_pos(4)]; % 原点 (xlims(1), ylims(1))(0,10) origin = ds2nfu(xlims(1), ylims(1)); % x轴终点 (xlims(2), ylims(1)) x_end = ds2nfu(xlims(2), ylims(1)); % y轴终点 (xlims(1), ylims(2)) y_end = ds2nfu(xlims(1), ylims(2)); % 绘制x轴箭头 annotation(‘arrow’, [origin(1), x_end(1)], [origin(2), x_end(2)], … ‘Color’, ‘k’, ‘LineWidth’, 1.5, ‘HeadWidth’, 10, ‘HeadLength’, 10); % 绘制y轴箭头 annotation(‘arrow’, [origin(1), y_end(1)], [origin(2), y_end(2)], … ‘Color’, ‘k’, ‘LineWidth’, 1.5, ‘HeadWidth’, 10, ‘HeadLength’, 10); 1.在x箭头处设置xlabel为物料填充率 (%),在y箭头处设置ylabel为功率 (KW)。 2.只画竖网格线 3.画一条y=30的横线,虚线,棕色 4.画一条x=30的竖线,虚线,棕色
08-26
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值