Home > Back-end >  How to show x-axis values on scatter plot?
How to show x-axis values on scatter plot?

Time:06-16

I've made a scatter plot on google co-lab and am unable to see the x-axis. This is in python, usuing matplotlib library.

import matplotlib.pyplot as plt
import pandas as pd
from google.colab import drive
drive.mount('/content/drive')
weather_data = '/content/drive/MyDrive/Austin Weather Data/austin_weather.csv'
weather = pd.read_csv(weather_data)

ausweather = weather[['Date', 'TempAvgF']]
ausweather.head()

x = weather['Date']
y = weather['TempAvgF']


plt.scatter(x, y)
plt.rcParams["figure.figsize"] = (30,25)
plt.show

The scatter plot appears, but with 2 black bars where the x axis labels should be.

CodePudding user response:

I do not have the dataset that you are using but I run the code using giving a simple array. and it shows both the x and y-axis. I hope this will work or if it does not work kindly share the data set. Code:

      import matplotlib.pyplot as plt
      import pandas as pd
      # from google.colab import drive
      # drive.mount('/content/drive')
      # weather_data = '/content/drive/MyDrive/Austin Weather              
      #Data/austin_weather.csv'
      # weather = pd.read_csv(weather_data)

      # ausweather = weather[['Date', 'TempAvgF']]
      # ausweather.head()

      x = [1,2,3,4,5,6,7,8,9,10]
      y = [1,4,9,16,25,36,49,64,81,100]


      plt.scatter(x, y)
      plt.rcParams["figure.figsize"] = (30,25)
      plt.show

CodePudding user response:

The reason you are seeing a black bar is because all the 1300 dates are written very close to each other. This might not be the correct setting. Just change the X-axis to show only the year (one option), so this can be taken care. Below is the updated code. But, note that I ran it locally and NOT on google colab. But, as the changes are in plt axis, should be ok. You will need to add the 4 lines after plt.scatter() as in below code... More options on the date formatter can be found enter image description here

  • Related