如何使用bokeh和javascript旋转2D直角坐标图?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用bokeh和javascript旋转2D直角坐标图?相关的知识,希望对你有一定的参考价值。
我有一个使用散景绘制的传感器数据(作为X,Y值)。这是数据集。
29.10005846, 2825.85017059
58.19703126, 2825.40069823
87.3804953, 2827.6501992
116.69880415, 2831.59626167
146.0860764, 2825.22562962
175.72769172, 2832.55428516
205.39128101, 2837.57632879
234.76457514, 2837.30410676
264.52489707, 2840.71040038
295.70165552, 2851.70993106
324.82402502, 2846.52671738
355.10704268, 2850.96965755
384.17776663, 2845.17986138
415.75912923, 2856.90625441
446.73379702, 2859.31196525
477.46944178, 2862.45121044
506.74485391, 2856.39819581
539.08800868, 2866.75306207
571.22446286, 2871.73930102
601.79112244, 2870.59862136
634.3319722, 2877.92146332
663.93304837, 2871.2369995
696.28746445, 2875.91111943
729.31092594, 2880.09488964
761.99469592, 2884.03416821
793.50966269, 2882.78379613
828.93370595, 2894.64849527
859.8374535, 2889.79316796
894.18450783, 2894.00674946
929.42799081, 2901.78782992
964.61113444, 2908.19898895
999.37514285, 2912.29983413
1030.64806964, 2905.62446929
1068.93411191, 2916.2674542
1105.62923064, 2922.87683702
1138.21213352, 2917.85831375
1173.41956398, 2918.97285477
1211.9584303, 2925.92647946
1249.0823802, 2929.84951959
1285.21000202, 2930.56910014
1326.82773484, 2942.7049057
1364.62643266, 2945.21827702
1399.69210799, 2939.79557161
1439.55115712, 2944.99175993
1481.90652918, 2954.14590682
1514.53538924, 2943.1755902
1559.22929442, 2954.84094452
1600.55674453, 2957.69963105
1637.2325992, 2952.43042528
1677.42657892, 2952.8047806
1719.8123034, 2956.12003158
1767.56032239, 2967.46465972
1806.8694417, 2962.45216343
1853.69251649, 2969.98738285
1899.24608144, 2974.34115766
1950.00576648, 2985.60437947
2003.9418305, 2999.11089158
2044.87205902, 2993.17945707
2088.18184745, 2990.00093177
2143.26141283, 3002.52419079
2234.1779667, 3062.69975889
2279.8759927, 3057.57444029
2357.29487703, 3094.39184053
4804.20254038, 3283.35178606
4875.73780654, 3257.86384643
4980.18381913, 3253.97758561
5053.87808427, 3228.36263597
5154.71206986, 3218.52023091
5302.82234979, 3235.58636518
5438.64081559, 3240.79420497
5499.38056483, 3200.71592041
5499.87948139, 3125.63620567
5517.7821715, 3061.07495953
5507.67854268, 2981.71864374
5520.91373402, 2914.5429387
5505.28888418, 2834.1944714
5534.18144278, 2777.3548132
5543.62215243, 2710.99509979
5531.5203973, 2634.84555415
5509.81635979, 2554.06591172
5539.47030903, 2498.83926961
5531.94143712, 2427.20104983
5549.28309334, 2366.96982447
5560.83090619, 2303.37157941
5536.01039906, 2226.58434863
5550.26052973, 2166.1883695
5580.68940051, 2112.10672436
5565.52712576, 2041.10063751
5574.34462275, 1978.35968086
5578.3421267, 1915.33368307
5577.83804411, 1851.17577601
5588.98604201, 1791.19960425
5610.95569928, 1734.73489061
5592.39377117, 1665.0396116
5601.55214103, 1605.16124152
5613.92753886, 1546.33327213
5601.51631032, 1481.03039306
5631.97963857, 1427.20193063
5618.43169708, 1361.31784138
5611.69236747, 1298.65652616
5624.76745036, 1240.80261501
5620.60839772, 1179.32618028
5628.72672423, 1119.62335805
5643.88832151, 1062.34345401
5658.46669961, 1004.8699465
5635.96478156, 941.11475442
5657.21271901, 884.88431555
5671.11660281, 826.31560396
5674.36980157, 767.20489771
5686.93061725, 709.35263063
5684.99170484, 649.73326519
5687.40217417, 590.74572301
5681.32837762, 530.04138107
5698.44409736, 472.50277164
5687.04941217, 412.64147093
5706.96609699, 355.05206357
5706.32496038, 296.05986993
5692.12689548, 235.58524105
5711.2427887, 177.48748841
5720.76589843, 118.83406638
5.72200000e+03, 3.50371449e-13
5742.69551652, -59.13716764
5737.78294662, -118.18569087
5740.25984235, -177.38642081
5738.12890994, -236.48596763
5731.34319138, -296.35489305
5756.93203551, -357.15198233
5732.00392321, -414.89760701
5753.33916696, -476.04351678
5760.08049989, -536.37452841
5794.58671647, -1402.92864585
5843.55839607, -1479.73283795
5832.84550345, -1541.10425763
5841.73478955, -1607.98496529
5824.86723109, -1668.05357833
5858.1810423, -1743.06020423
5872.09160852, -1814.34730444
5887.38180656, -1885.69866728
5881.88940421, -1950.94490866
5894.59323361, -2022.76904521
5912.09286275, -2097.06627988
5895.44215869, -2160.92636467
5911.22611518, -2236.02455604
5898.13569104, -2300.77299404
5903.59021275, -2445.34713281
5905.69353714, -2517.77358898
5888.61228559, -2582.46884782
5899.08409071, -2659.82102607
5876.82648806, -2722.94682821
5923.83253849, -2820.44837854
5923.22473252, -2895.35106129
5956.55925054, -2988.02575202
5954.81780289, -3064.30317273
5930.02350585, -3129.19258278
5954.97597439, -3222.53039465
5947.71306941, -3298.22834929
5871.69925895, -3335.58942504
5745.41808476, -3342.57087154
5609.31579714, -3341.17004775
5524.50373187, -3369.52511737
5388.56322647, -3363.22618245
5287.06208047, -3376.01889172
5162.43314744, -3371.77104771
5052.84083396, -3376.20030606
4966.61363705, -3393.07736138
4852.70510423, -3389.07275983
4742.66968277, -3385.41183317
4670.39496337, -3406.95933144
4580.74996115, -3415.62963353
4492.83421987, -3422.6224847
4376.06567981, -3405.44771888
4291.44888539, -3411.12744764
4210.28987747, -3417.9508697
4129.62241433, -3424.8275453
4056.74426274, -3435.50316355
3940.21548321, -3407.10770975
3871.93813914, -3418.37973997
3803.10716241, -3427.94047661
3720.65020079, -3424.91037597
3645.10561425, -3425.37721441
3578.60986064, -3432.96263675
3507.34638432, -3434.64733858
2893.55838871, -3563.06326791
2841.77811359, -3573.88488247
2779.64053045, -3570.60548388
2602.68039125, -3489.22108514
2548.90444216, -3492.85873529
2494.78742369, -3493.69213163
2443.78902021, -3497.88282032
2389.58494826, -3496.44230255
2385.06301035, -3569.49904561
2401.78443073, -3675.90975791
2419.25324096, -3787.24830927
2420.90768068, -3877.27313993
2430.8566591, -3983.94589608
2438.65796754, -4092.51063742
2441.155801, -4194.32561388
2460.58620048, -4329.65536158
2436.73209536, -4392.36447662
2377.09104075, -4390.84128431
2324.91171468, -4403.99653939
2280.81250824, -4430.37056038
2225.65157996, -4434.85276471
2177.23087256, -4452.1457442
2139.01122676, -4490.57980352
2074.62656718, -4475.53500788
2029.6644125, -4499.39533411
1888.92542215, -4560.26937248
1839.62809431, -4573.91172572
1794.61325812, -4598.20174131
1747.16922933, -4616.43755336
1694.3752087, -4620.1010435
1645.90160431, -4637.59085182
1597.08563128, -4651.4558459
1545.32386543, -4656.26568732
1495.46397356, -4666.21768714
1481.00812898, -4790.28296887
1400.80196287, -4704.89477681
1352.28016726, -4719.06975465
1308.39499086, -4750.09805666
1256.34525058, -4751.71775375
1205.13815347, -4755.67850375
1152.44585984, -4756.37514712
1106.11265926, -4779.68103382
1059.42139941, -4802.53581959
1010.52831851, -4816.12640174
958.28366174, -4817.61729734
910.84436141, -4839.02247869
862.01650187, -4854.05269342
811.4932438, -4859.71271942
765.58101628, -4894.48686866
712.55482721, -4890.36088834
663.76756256, -4909.33077139
614.54123383, -4926.82088896
562.52684302, -4921.95891398
513.26071212, -4941.41553013
461.76580885, -4949.50637314
412.34502107, -4972.93390099
362.8523913, -5000.85334139
312.08318844, -5016.30143467
260.56791842, -5022.24505176
208.6234055, -5040.68460377
163.72708285, -5268.45655219
109.23928507, -5258.86554103
6.46001187e-13, -5.27500000e+03
-54.62696749, -5304.71873848
-109.70119799, -5325.87031828
-164.32104453, -5317.46166835
-222.65011788, -5402.41389797
-276.06085699, -5338.86752067
-334.36558679, -5389.63817472
-388.40249273, -5365.96156375
-452.3774005, -5467.31658929
-507.53918211, -5450.42016533
-570.36280258, -5500.5078196
-632.64155659, -5544.02062233
-687.34781215, -5518.35790658
-743.60008678, -5507.02350739
-806.46038229, -5541.62626417
-1700.26388839, -5937.34609989
-1780.98882, -5764.12767234
-1852.45034073, -5783.57646575
-1921.03692636, -5791.72004914
-1999.72401918, -5827.43724523
-2078.34545863, -5859.31473421
-2150.25767264, -5866.335819
-2223.2877073, -5877.55457402
-2296.77544184, -5887.88778508
-2379.28906505, -5918.66746362
-2451.0873843, -5917.44840573
-2519.73447184, -5910.29298694
-2596.92746034, -5921.58118797
-2679.13976942, -5941.93016586
-2921.64086885, -1908.95642525
-3009.40447539, -1922.37501635
-3017.16485643, -1883.86974845
-3008.23341286, -1835.51293479
-3309.7376695, -1174.64060848
-3265.85543633, -1121.33726816
-3073.98993757, -985.17504217
-3022.82714669, -934.56516157
-3087.07803546, -919.12469391
-3022.3579769, -866.0763589
-3338.47467732, -846.00403599
-3312.16315925, -802.52053337
-3316.35430883, -767.46993317
-3342.63524003, -737.37280402
-3338.30667676, -700.44952132
-3319.95817416, -660.38074002
-3324.6167842, -625.78929217
-3326.94605498, -590.82226365
-3321.01739929, -554.55606898
-3330.50368071, -520.9474376
-3340.72407103, -486.76347565
-3313.84781985, -448.04980404
-3360.95515628, -419.22480544
-3349.1973151, -382.77714732
-3349.97735617, -347.95935556
-3292.70118205, -307.19362902
-3059.08553467, -190.31734432
-3068.87234918, -159.22155763
-3096.34917484, -128.15142391
-3329.39267224, -103.46706753
-3349.27748335, -69.57254848
-3.35600000e+03, -6.16487199e-13
-3384.82053342, 34.85622714
-3374.28427336, 69.50282396
-3373.38968622, 104.24502355
-3350.15605695, 138.07025064
-3376.48916711, 174.59067667
-3374.51234606, 209.35000906
-3377.16465704, 244.4481116
-3382.44121474, 279.87037865
-3420.20337375, 318.48686346
-3438.5633874, 356.55410642
-3403.90943586, 388.42831055
-3383.85190819, 421.48103569
-3332.75509364, 450.01387292
-3137.01603501, 589.91049835
-3202.26394052, 636.96990138
-3194.55639281, 669.70549732
-3210.92832419, 707.7310908
-3272.96036458, 1558.31814848
-3269.3181862, 1598.08622964
-3283.07955091, 1646.9115527
-3214.37454942, 1654.09227556
-3248.46776092, 1714.17216354
-3538.1839898, 2310.91541479
-3262.19185838, 2278.27682233
-3285.76583307, 2345.44492372
-3300.2185479, 2407.44315325
-3282.04241179, 2447.25021345
-3306.78325991, 2519.0937799
-3289.34927925, 2559.76665325
-3221.32655297, 2560.52343069
-3152.08867832, 2558.89369924
-3097.40924423, 2568.78025019
-3045.61067581, 2579.21240138
-2978.79987961, 2575.77081225
-2925.88084358, 2583.14348986
-2861.98669859, 2579.65911258
-2804.65069515, 2581.72006193
-2759.69911259, 2593.34336485
-2647.40778876, 2647.40778876
-2602.51167934, 2656.66971204
-2577.09446374, 2685.49215694
-2495.88017972, 2655.05520253
-2440.63507974, 2650.45456621
-2396.67468799, 2658.04278369
-2358.58560205, 2670.58850402
-2312.64284519, 2673.55719419
-2265.59820712, 2674.33912657
-2214.9537508, 2669.82038381
-2166.07115983, 2667.25171863
-2126.66574992, 2674.54328589
-2079.96968754, 2671.83871125
-2038.40586557, 2674.82158792
-2002.38378826, 2684.44783233
-1960.0155574, 2685.88235311
-1919.46956917, 2688.01889373
-1879.66148684, 2690.42686109
-1833.80190835, 2683.22018495
-1802.26983592, 2697.28742231
-1761.27211728, 2695.61134604
-1722.65473322, 2696.75001996
-1688.43878495, 2704.1668716
-1649.56568231, 2703.48331967
-1612.46276191, 2706.00514439
-1572.44035317, 2701.72303091
-1537.61932849, 2705.59989663
-1503.37104589, 2709.92186204
-1465.86487372, 2707.670802
-1432.7618579, 2714.02919265
-1393.29586094, 2706.4113959
-1364.45628904, 2718.82751113
-1326.72260596, 2712.97016697
-1298.71610471, 2726.48793861
-1263.78528976, 2726.32935307
-1233.58858134, 2734.64060015
-1195.72096082, 2725.22061196
-1135.80442726, 2742.07445249
-1102.28425772, 2740.63595086
-1069.64185684, 2740.66238309
-1040.6559024, 2749.66094143
-1007.81065553, 2748.02596105
-975.63401337, 2749.00477845
-944.67712513, 2751.33897026
-912.20096092, 2748.58243589
-882.93413786, 2754.97301406
-852.74640375, 2758.18646413
-823.24580625, 2765.04816278
-794.45426815, 2772.41743174
-763.73939389, 2772.73838258
-733.35799063, 2773.68834904
-700.0904479, 2762.67503785
-669.94451803, 2764.99536035
-640.76073738, 2768.82460214
-610.27670283, 2766.48718522
-582.16780796, 2774.58206643
-552.88597259, 2779.54548466
-524.04997479, 2784.10858695
-493.9546892, 2781.48049877
-465.45157438, 2787.40575301
-436.26063968, 2789.08688539
-406.30908601, 2788.55463038
-377.70705669, 2793.58164
-349.16834253, 2799.30749804
-320.21108141, 2801.76102895
-290.8270742, 2799.9365373
-261.58368894, 2803.82416954
-232.45020927, 2803.37937144
-204.00496432, 2811.60860977
-174.73181303, 2808.56988404
-145.90559918, 2812.21755135
-116.32460647, 2810.59381376
-87.50126966, 2815.64069579
-58.60708412, 2821.3913606
现在,我想单击角落并将值存储到新数组中。我想旋转通过单击创建的新数据集。但是在旋转它们时,似乎反向旋转不起作用。这是代码:
output_file("callback.html")
plot = figure(plot_width=400, plot_height=400)
plot.toolbar.logo = None
tapX = []
tapY = []
tools = "pan,wheel_zoom,reset,hover"
data = {'x_values': ld2.x,
'y_values': ld2.y}
source = ColumnDataSource(data=data)
print(data)
source1 = ColumnDataSource(data=dict(x=tapX, y=tapY))
lidarPlot= plot.circle(x='x_values', y='y_values',hover_color="red", source=source)
customPlot= plot.scatter('x', 'y', source=source1,
line_color='red', fill_alpha=0.6, size=10)
plot.patch('x', 'y',source=source1, line_width=5, color="red", alpha=0.2)
draw_tool = PointDrawTool(renderers=[customPlot])
callback = CustomJS(args=dict(sensor=source, xy=source1 ), code="""
// getting lidar value and tabX & tapY
console.log("start")
var sensorData = sensor.data;
console.log(sensorData)
var a = sensorData['x_values'];
var b = sensorData['y_values'];
""")
callbackForRotation = CustomJS(args=dict(xy=source1 ), code=""" console.log("end")
var data = xy.data;
var x = data['x'];
var y = data['y'];
// getting slider value
var rotationAngle = cb_obj.value;
var radians = (Math.PI / 180) * rotationAngle;
console.log("radians", radians);
cos = Math.cos(radians);
sin = Math.sin(radians);
cx=2;
cy=2;
if(cb_obj.x) {
x.push(cb_obj.x);
y.push(cb_obj.y);
console.log(cb_obj.x);
}
if(x.length> 0 && radians) {
for (var i = 0; i < x.length; i++) {
x[i] = (cos * (x[i] - cx)) + (sin * (y[i] - cy)) + cx,
y[i] = (cos * (y[i] - cy)) - (sin * (x[i] - cx)) + cy;
}
}
xy.change.emit();
""")
rotation = Slider(start=-180, end=180, value=0, step=0.1, title="Rotate ")
#callback.args["slider"] = rotation # sending slider value into the callback function
rotation.js_on_change('value', callbackForRotation)
plot.js_on_event('tap', callbackForRotation)
layout = column(rotation, plot) # create two row, 1. rotation 2. plotting
plot.add_tools(draw_tool)
plot.toolbar.active_tap = draw_tool
show(layout)
答案
我无法在不花费太多时间的情况下真正检查您的代码,因为您没有提供任何数据。另外,为了将来参考,请不要从代码中排除导入。
没有运行它,这是我看到的错误:
- [
radians = cos = Math.cos(radians)
-radians =
可能是剩余的,必须删除] - 将相同的回调函数附加到滑块和轻击事件-您可以执行此操作,但此时不能使用
cb_obj
。在第一种情况下,cb_obj
将是滑块,在第二种情况下将是事件。我将只创建2个单独的回调,这些回调通过Python中的字符串操作共享一些代码。 - 请勿更改原始数据。这样,如果您前后旋转几次,由于浮点算法不精确,错误将开始累积。而是使用表达式或转换:https://docs.bokeh.org/en/latest/docs/user_guide/data.html#customjstransform
以上是关于如何使用bokeh和javascript旋转2D直角坐标图?的主要内容,如果未能解决你的问题,请参考以下文章