python - Plotly Express 散点图不会绘制 x 变量的完整范围

标签 python plotly visualization plotly-express

我正在尝试使用 Plotly Express 创建散点图,绘制 75 个国家/地区样本随时间变化的平均温度。但是,图表上仅显示了 34 个国家/地区,并且图例中并未显示所有县。

可以显示的变量数量是否有限制,或者是否缺少某些参数。我的代码如下供引用;

fig=px.scatter(df, x= 'Country', y='Temperature', animation_frame='YR',
           animation_group='Country',size='Temperature', color='Country', 
           hover_name='Country',width=1000, height=600, 
           range_y=[2,35])

完整代码

!pip install plotly_express
import pandas as pd
import numpy as np
import plotly.express as px

df = pd.DataFrame(np.random.randint(0,100,size=(75, 1)))
df= df.rename(columns={0: "Temperature"}) 
df['Country']=['United States','Afghanistan','Albania','Algeria','American Samoa','Andorra','Angola','Anguilla','Antarctica','Antigua And Barbuda',
         'Argentina','Armenia','Aruba','Australia','Austria','Azerbaijan','Bahamas','Bahrain','Bangladesh','Barbados','Belarus','Belgium','Belize',
         'Benin','Bermuda','Bhutan','Bolivia','Bosnia And Herzegowina','Botswana','Bouvet Island','Brazil','Brunei Darussalam','Bulgaria',
         'Burkina Faso','Burundi','Cambodia','Cameroon','Canada','Cape Verde','Cayman Islands','Central African Rep','Chad','Chile','China',
         'Christmas Island','Cocos Islands','Colombia','Comoros','Congo','Cook Islands','Costa Rica','Cote D`ivoire','Croatia','Cuba','Cyprus',
         'Czech Republic','Denmark','Djibouti','Dominica','Dominican Republic','East Timor','Ecuador','Egypt','El Salvador','Equatorial Guinea',
         'Eritrea','Estonia','Ethiopia','Falkland Islands (Malvinas)','Faroe Islands','Finland','France','French Guiana','French Polynesia',
         'French S. Territories']
df['YR']=[2015, 2016,2017, 2018, 2019,2015, 2016,2017, 2018, 2019,2015, 2016,2017, 2018, 2019,2015, 2016,2017, 2018, 2019,
      2015, 2016,2017, 2018, 2019,2015, 2016,2017, 2018, 2019,2015, 2016,2017, 2018, 2019,2015, 2016,2017, 2018, 2019,
      2015, 2016,2017, 2018, 2019,2015, 2016,2017, 2018, 2019,2015, 2016,2017, 2018, 2019,2015, 2016,2017, 2018, 2019,
      2015, 2016,2017,2015, 2016,2017,2015, 2016,2017,2015, 2016,2016,2017,2015, 2016]

fig=px.scatter(df, x= 'Country', y='Temperature', animation_frame='YR',
           animation_group='Country',size='Temperature', color='Country', 
           hover_name='Country',width=1000, height=600, 
           range_y=[2,35])
fig

最佳答案

x 轴刻度已变细,因此未全部显示。如果您在 tickvals 中给出列表中的所有国家/地区,它们都会显示出来。

fig=px.scatter(df, x='Country',
               y='Temperature',
               animation_frame='YR',
               animation_group='Country',
               size='Temperature',
               color='Country',
               hover_name='Country',
               width=1000,
               height=600,
               #range_y=[2,35]
              )

fig['layout']['sliders'][0]['pad']['t'] = 150
fig['layout']['updatemenus'][0]['pad']['t'] = 150
fig.update_layout(
    autosize=False,
    width=1000,
    showlegend=True
)
fig.update_xaxes(type='category', tickvals=df['Country'].tolist())
fig.show()

enter image description here

关于python - Plotly Express 散点图不会绘制 x 变量的完整范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72204526/

相关文章:

python - 等值线世界地图未显示所有国家

javascript - 鸡尾酒配方数据可视化

python - 递归调用函数

javascript - 如何消除 Plotly 图表顶部的空白区域?

python - Python 的 argparse 模块中具有混合类型 args 输入的参数解析器

在plot_ly中重新定义动画 slider 的标签

graph - 聚类图可视化技术

visualization - 什么是用于创建状态图和动画的良好可视化库?

python - 如何从 PyTorch 模型的特定层获取输出?

Python在sys.path中搜索文件的方法