【问题标题】:Create scatterplot over line plot with matplotlib [duplicate]使用 matplotlib 在折线图上创建散点图 [重复]
【发布时间】:2021-12-10 09:59:54
【问题描述】:

所以我对 python 很陌生,我必须在我已经使用气候数据制作的线图之上创建一个 散点图。我已经有了散点图的数据框,其中包含 1837 年到 2020 年之间一个站点的月平均温度。 折线图显示了三个图表,描述了该时期的平均温度、最低温度和最高温度,x 轴显示月份,y 轴显示温度(以摄氏度为单位)。 谁能帮我使用哪个代码将散点图添加到折线图的顶部? (我猜是使用 plt.scatter()

【问题讨论】:

标签: python pandas dataframe matplotlib scatter-plot


【解决方案1】:

您需要在同一张图上绘制散点图和折线图,如下所示:

import random
import matplotlib.pyplot as plt


fig= plt.figure(figsize=(4, 3))
ax = plt.axes()
ax.scatter([random.randint(1, 200) for i in range(100)],
[random.randint(1, 200) for i in range(100)])
ax.plot([1, 200], [1,200])

ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title('Scatter and Line plots')
plt.show()

这又会返回下面的情节:

【讨论】:

  • 好的,太好了!以及如何设置 x 轴的标称比例?应该是“一月、二月、三月、四月……”等。
  • 使用位置=(range(12)) 标签=(['Jan','Feb','Mar','Apr','May','Jun','Jul',' Aug','Sep', 'Oct','Nov', 'Dec']) plt.xticks(positions,labels)
  • 如果答案对您有所帮助并解决了您的问题,请接受它作为您问题的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-11
  • 2018-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-30
  • 1970-01-01
相关资源
最近更新 更多