简体   繁体   English

如何在散点图 plot 上显示 x 轴值?

[英]How to show x-axis values on scatter plot?

I've made a scatter plot on google co-lab and am unable to see the x-axis.我在 google co-lab 上做了一个散点图 plot,但看不到 x 轴。 This is in python, usuing matplotlib library.这是在 python 中,使用 matplotlib 库。

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.出现散点图 plot,但在 x 轴标签应位于的位置有 2 个黑条。

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.它显示了 x 轴和 y 轴。 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

The reason you are seeing a black bar is because all the 1300+ dates are written very close to each other.您看到黑条的原因是因为所有 1300 多个日期都写得非常接近。 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.只需将 X 轴更改为仅显示年份(一个选项),这样就可以处理了。 Below is the updated code.下面是更新后的代码。 But, note that I ran it locally and NOT on google colab.但是,请注意,我是在本地运行的,而不是在 google colab 上运行的。 But, as the changes are in plt axis, should be ok.但是,由于变化是在 plt 轴上进行的,所以应该没问题。 You will need to add the 4 lines after plt.scatter() as in below code... More options on the date formatter can be found here您需要在plt.scatter()之后添加 4 行,如下面的代码所示...可以在此处找到有关日期格式化程序的更多选项

My Code:我的代码:

import matplotlib.pyplot as plt
import pandas as pd
#from google.colab import drive #Commented as I am running this locally
#drive.mount('/content/drive') #Commented as I am running this locally
#weather_data = '/content/drive/MyDrive/Austin Weather Data/austin_weather.csv'#Commented as I am running this locally

weather = pd.read_csv('austin_weather.csv')

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

x = weather['Date']
y = weather['TempAvgF']
ax = plt.scatter(x, y)

#### New code here ####
import matplotlib.dates as mdate
locator = mdate.YearLocator()
plt.gca().xaxis.set_major_locator(locator)
plt.gca().xaxis.set_major_formatter(mdate.DateFormatter('%Y'))
#### New code end ####

plt.rcParams["figure.figsize"] = (5,8)
plt.show

My output我的 output

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM