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

I am trying to use Plotly Express to create a scatter plot plotting the average temperature over time in a sample of 75 countries. However, only 34 countries are displayed on the chart, and not all counties are displayed in the legend.

Is there a limit to the amount of variables that can be displayed, or is there some parameter that I'm missing. My code is below for reference;

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])

Full code

!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


Solution 1:[1]

The x-axis ticks were thinned out so they were not all displayed. if you give all countries in a list in tickvals, they will all be displayed.

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1