📌  相关文章
📜  Python Plotly – 如何为等值线图设置颜色条位置?

📅  最后修改于: 2022-05-13 01:54:41.258000             🧑  作者: Mango

Python Plotly – 如何为等值线图设置颜色条位置?

在本文中,我们将学习如何使用 Plotly 在Python中为等值线图设置颜色条位置。

彩条是从亮到暗或相反的渐变。它们非常适合可视化从低到高的数据,例如收入、温度或年龄。 Choropleth 地图用于绘制带有阴影或图案区域的地图,这些区域相对于数据变量被着色、阴影或图案化。它们由彩色多边形组成。它们用于表示地理区域内数量的空间变化。

在这里,我们将讨论如何使用不同的示例为等值线图设置颜色条位置以使其更加清晰。

示例 1:设置 X 轴的颜色条位置

Python3
# importing libraries
import plotly.express as px
  
# figure setup using dataset
fig = px.choropleth(locations=["CA", "TX", "NY"],
                    locationmode="USA-states",
                    color=[1, 2, 3], scope="usa",
                    title="Geeksforgeeks")
  
# set colorbar position for X-axis
fig.update_layout(coloraxis_colorbar_x=0.26)
  
fig.show()


Python3
# importing packages
import plotly.express as px
  
# using gapminder datasheet.
df = px.data.gapminder().query("year==2007")
  
# figure setup
fig = px.choropleth(df, locations="iso_alpha",
                      
                    # lifeExp is a column of gapminder
                    color="lifeExp",  
                      
                    # column to add to hover information
                    hover_name="country",  
                    color_continuous_scale=px.colors.sequential.Plasma)
  
# set colorbar position for Y-axis
fig.update_layout(coloraxis_colorbar_y=-0.3)
  
fig.show()


Python3
# importing packages
import plotly.express as px
  
# using the gapminder dataset
data = px.data.gapminder()
data_canada = data[data.country == 'Canada']
  
# plotting the bar chart
fig = px.scatter(data_canada, x='year', y='pop',
                 hover_data=['lifeExp', 'gdpPercap'], 
                 color='lifeExp',
                 labels={'pop': 'population of Canada'},
                 height=400, title="Geeksforgeeks")
  
# set colorbar position for X-axis
fig.update_layout(coloraxis_colorbar_x=0.9)
  
# set colorbar position for Y-axis
fig.update_layout(coloraxis_colorbar_y=0.1)
  
fig.show()


输出:

示例 2:设置 Y 轴的颜色条位置

Python3

# importing packages
import plotly.express as px
  
# using gapminder datasheet.
df = px.data.gapminder().query("year==2007")
  
# figure setup
fig = px.choropleth(df, locations="iso_alpha",
                      
                    # lifeExp is a column of gapminder
                    color="lifeExp",  
                      
                    # column to add to hover information
                    hover_name="country",  
                    color_continuous_scale=px.colors.sequential.Plasma)
  
# set colorbar position for Y-axis
fig.update_layout(coloraxis_colorbar_y=-0.3)
  
fig.show()

输出:

示例 3:同时设置 X 轴和 Y 轴的颜色条位置

在这里我们也可以看到,这种方法也适用于另一个图。

Python3

# importing packages
import plotly.express as px
  
# using the gapminder dataset
data = px.data.gapminder()
data_canada = data[data.country == 'Canada']
  
# plotting the bar chart
fig = px.scatter(data_canada, x='year', y='pop',
                 hover_data=['lifeExp', 'gdpPercap'], 
                 color='lifeExp',
                 labels={'pop': 'population of Canada'},
                 height=400, title="Geeksforgeeks")
  
# set colorbar position for X-axis
fig.update_layout(coloraxis_colorbar_x=0.9)
  
# set colorbar position for Y-axis
fig.update_layout(coloraxis_colorbar_y=0.1)
  
fig.show()

输出: