Plotly ColorBar方向不改变

发布于 2025-01-29 20:19:00 字数 3751 浏览 2 评论 0 原文

我使用了以下解决方案的代码但配色栏不旋转。它只会改变位置。线 fig.update_traces(colorBar = dict(entientation ='h',y = -0.25,x = 0.5))不仅应该更改配色键的位置,而且还应使其水平,这是也在文档

我目前正在使用python = 3.8.10,并且plitly = 5.8.0

这是使用中的完整代码:

import pandas as pd
import numpy as np
import plotly.graph_objects as go 
from scipy.interpolate import griddata
import plotly.io as pio

df = pd.DataFrame({'hidden_layer_sizes': {0: 25,
  1: 25,  2: 25,  3: 25,  4: 25,  5: 50,  6: 50,  7: 50,  8: 50,  9: 50,  10: 75,
  11: 75,  12: 75,  13: 75,  14: 75,  15: 100,  16: 100,  17: 100,  18: 100,  19: 100,  20: 125,
  21: 125,  22: 125,  23: 125,  24: 125,  25: 150,  26: 150,  27: 150,  28: 150,  29: 150}, 
'max_iter': {0: 100,  1: 200,  2: 300,  3: 400,  4: 500,  5: 100,  6: 200,  7: 300,  8: 400,  9: 500,  
10: 100,  11: 200,  12: 300,  13: 400,  14: 500,  15: 100, 16: 200,  17: 300,  18: 400,  19: 500,  
20: 100,  21: 200,  22: 300,  23: 400,  24: 500,  25: 100,  26: 200,  27: 300,  28: 400,  29: 500}, 
'Score': {0: 0.9270832984321359,  1: 0.9172223807360554,  2: 0.9202868292420568,  3: 0.9187318693456508,
  4: 0.9263589700182026,  5: 0.9325454241272417,  6: 0.9351742112383672,  7: 0.934706441722599,
  8: 0.9350294733755595,  9: 0.9334167352798914,  10: 0.9355533396303661,  11: 0.9327821227628682,
  12: 0.9333376163633981,  13: 0.9322875868305249,  14: 0.9345524934883098,  15: 0.9341786678949748,
  16: 0.9306931295155753,  17: 0.9332227354795629,  18: 0.9312008571438402,  19: 0.9335295484755572,
  20: 0.9333167395841182,  21: 0.9315595511169302,  22: 0.9301811416101524,  23: 0.9314818362895073,
  24: 0.9308551601915486,  25: 0.9296559215457606,  26: 0.9284091216867709,  27: 0.9318823563281231,
  28: 0.9295666150206443,  29: 0.9291284919738931},
 'Time': {0: 119.91294360160828,  1: 256.4710912704468,  2: 266.6792154312134,  3: 326.7445312023163,
  4: 256.8881601810455,  5: 183.77022705078124,  6: 359.7090343952179,  7: 383.6012378692627,
  8: 416.3133870601654,  9: 425.7837643623352,  10: 225.39801173210145,  11: 516.9914848804474,
  12: 562.7134436607361,  13: 585.6752841472626,  14: 560.5802517414093,  15: 267.22873797416685,
  16: 646.1253435134888,  17: 811.1979314804078,  18: 780.6058969974517,  19: 789.9369702339172,
  20: 394.0711458206177,  21: 890.7988158226013,  22: 1065.5482338428496,  23: 996.5119229316712,
  24: 1096.0208141803741,  25: 524.0947244644165,  26: 1182.684538602829,  27: 1348.3343998908997,
  28: 1356.0255290508271,  29: 1053.8607951164245}})


xi = np.linspace(min(df["hidden_layer_sizes"]), max(df["hidden_layer_sizes"]), num=100)
yi = np.linspace(min(df["max_iter"]), max(df["max_iter"]), num=100)

x_grid, y_grid = np.meshgrid(xi,yi)
z_grid = griddata((df["hidden_layer_sizes"],df["max_iter"]),df["Score"],(x_grid,y_grid),method="cubic")

fig = go.Figure(go.Surface(x=x_grid, y=y_grid, z=z_grid, showlegend=True))
fig.update_layout(title="Test",
                  width=600, height=600, template="none")

fig.update_layout(legend = dict(orientation="h", x = -0.25, y = -0.10))
fig.update_traces(colorbar = dict(orientation='h', y = -0.25, x = 0.5))

fig.show()

<

a href =“ https://i.sstatic.net/srz93.png” rel =“ nofollow noreferrer">Resulting Plot

Any idea how to fix this?是否有一个仍然有效的版本?

I used the code of the solution of the following stackoverflow question but the colorbar does not rotate. It only changes the position. The line fig.update_traces(colorbar = dict(orientation='h', y = -0.25, x = 0.5)) should not only change the position of the colorbar but also make it horizontal, which is also stated in the documentation.

I am currently using python=3.8.10 and plotly=5.8.0

Here is the full code in use:

import pandas as pd
import numpy as np
import plotly.graph_objects as go 
from scipy.interpolate import griddata
import plotly.io as pio

df = pd.DataFrame({'hidden_layer_sizes': {0: 25,
  1: 25,  2: 25,  3: 25,  4: 25,  5: 50,  6: 50,  7: 50,  8: 50,  9: 50,  10: 75,
  11: 75,  12: 75,  13: 75,  14: 75,  15: 100,  16: 100,  17: 100,  18: 100,  19: 100,  20: 125,
  21: 125,  22: 125,  23: 125,  24: 125,  25: 150,  26: 150,  27: 150,  28: 150,  29: 150}, 
'max_iter': {0: 100,  1: 200,  2: 300,  3: 400,  4: 500,  5: 100,  6: 200,  7: 300,  8: 400,  9: 500,  
10: 100,  11: 200,  12: 300,  13: 400,  14: 500,  15: 100, 16: 200,  17: 300,  18: 400,  19: 500,  
20: 100,  21: 200,  22: 300,  23: 400,  24: 500,  25: 100,  26: 200,  27: 300,  28: 400,  29: 500}, 
'Score': {0: 0.9270832984321359,  1: 0.9172223807360554,  2: 0.9202868292420568,  3: 0.9187318693456508,
  4: 0.9263589700182026,  5: 0.9325454241272417,  6: 0.9351742112383672,  7: 0.934706441722599,
  8: 0.9350294733755595,  9: 0.9334167352798914,  10: 0.9355533396303661,  11: 0.9327821227628682,
  12: 0.9333376163633981,  13: 0.9322875868305249,  14: 0.9345524934883098,  15: 0.9341786678949748,
  16: 0.9306931295155753,  17: 0.9332227354795629,  18: 0.9312008571438402,  19: 0.9335295484755572,
  20: 0.9333167395841182,  21: 0.9315595511169302,  22: 0.9301811416101524,  23: 0.9314818362895073,
  24: 0.9308551601915486,  25: 0.9296559215457606,  26: 0.9284091216867709,  27: 0.9318823563281231,
  28: 0.9295666150206443,  29: 0.9291284919738931},
 'Time': {0: 119.91294360160828,  1: 256.4710912704468,  2: 266.6792154312134,  3: 326.7445312023163,
  4: 256.8881601810455,  5: 183.77022705078124,  6: 359.7090343952179,  7: 383.6012378692627,
  8: 416.3133870601654,  9: 425.7837643623352,  10: 225.39801173210145,  11: 516.9914848804474,
  12: 562.7134436607361,  13: 585.6752841472626,  14: 560.5802517414093,  15: 267.22873797416685,
  16: 646.1253435134888,  17: 811.1979314804078,  18: 780.6058969974517,  19: 789.9369702339172,
  20: 394.0711458206177,  21: 890.7988158226013,  22: 1065.5482338428496,  23: 996.5119229316712,
  24: 1096.0208141803741,  25: 524.0947244644165,  26: 1182.684538602829,  27: 1348.3343998908997,
  28: 1356.0255290508271,  29: 1053.8607951164245}})


xi = np.linspace(min(df["hidden_layer_sizes"]), max(df["hidden_layer_sizes"]), num=100)
yi = np.linspace(min(df["max_iter"]), max(df["max_iter"]), num=100)

x_grid, y_grid = np.meshgrid(xi,yi)
z_grid = griddata((df["hidden_layer_sizes"],df["max_iter"]),df["Score"],(x_grid,y_grid),method="cubic")

fig = go.Figure(go.Surface(x=x_grid, y=y_grid, z=z_grid, showlegend=True))
fig.update_layout(title="Test",
                  width=600, height=600, template="none")

fig.update_layout(legend = dict(orientation="h", x = -0.25, y = -0.10))
fig.update_traces(colorbar = dict(orientation='h', y = -0.25, x = 0.5))

fig.show()

And the resulting plot:

Resulting Plot

Any idea how to fix this? Is there a version where this still works?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬シ愛 2025-02-05 20:19:00

回答我自己的问题,以便有相同问题的人得到解决方案。

我正在使用Jupyter Lab,这显然就是这样做的原因。快速修复是将 fig.show()中的渲染器更改为 png ,否则。我还没有尝试过所有渲染器,但是 fig.show(Renderer ='png')为我工作。有关所有可能的渲染器,请参见文档: plotly documentation-显示python 显示数字

Answering my own question so people with the same problem get a solution.

I was using Jupyter Lab, which apparently was the reason for this. A quick fix is to change the renderer in fig.show() to png or else. I havent tried all of the renderers, but fig.show(renderer='png') worked for me. See the documentation for all possible renderers: Plotly Documentation - Displaying Figures in Python

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文