Plotly Express 散点图不绘制整个范围的 x 变量

Plotly Express Scatter plots does not plot full range of x variables

我正在尝试使用 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-axis 个刻度线变细了,所以它们没有全部显示出来。如果您在 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()