如何跳过Matplotlib图中的一些X值以降低密度?

发布于 2025-02-13 16:25:15 字数 922 浏览 1 评论 0原文

我试图在过去20年中绘制最低和最高每日温度值。由于两者之间有太多天,所以我的绘图图看起来太复杂了。 如何改变几天的频率以降低图形的密度? 换句话说,我想设置它的天气为一天,然后在图中2天跳过而不会更改数据框架。

fig, ax = plt.subplots()
colors = ["Orange", "Blue"]
for i,col in enumerate(weather_data.columns):
  if col is "Date": continue
  ax.plot('Date', col, data=weather_data)

ax.set_xlabel("Date")
ax.set_ylabel("Temperature (Celcius)")

# set 15 xticks to prevent overlapping
ax.set_xticks(np.arange(0, weather_data.shape[0],weather_data.shape[0] / 15))
ax.legend()
fig.autofmt_xdate()
ax.set_title('Time Plot of Weather');

数据集: https://drive.google.com/uc?id=1o-7dul6-bkpbpz7mauz7m62p6eoyngg2

I'm trying to plot minimum and maximum daily temperature values for last 20 years. Since there are too many days in between, my plot graph looks too complicated.
How can I make change the frequency of days to reduce the density of my graph?
In other words, I want to set that it gets the weather of one day and then skips following 2 days in the plot without changing the dataframe.

fig, ax = plt.subplots()
colors = ["Orange", "Blue"]
for i,col in enumerate(weather_data.columns):
  if col is "Date": continue
  ax.plot('Date', col, data=weather_data)

ax.set_xlabel("Date")
ax.set_ylabel("Temperature (Celcius)")

# set 15 xticks to prevent overlapping
ax.set_xticks(np.arange(0, weather_data.shape[0],weather_data.shape[0] / 15))
ax.legend()
fig.autofmt_xdate()
ax.set_title('Time Plot of Weather');

enter image description here

Dataset:
https://drive.google.com/uc?id=1O-7DuL6-bkPBpz7mAUZ7M62P6EOyngG2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愛放△進行李 2025-02-20 16:25:15

很难没有示例数据,但是一个选项是仅显示一个数据指出原始数据框中的每个k数据点,然后用直线段插入丢失的天数。 (这基本上是降采样。)

例如,要显示每5个数据点,请更改以下行:

ax.plot('Date', col, data=weather_data)

对此:

ax.plot('Date', col, data=weather_data.iloc[::5])

还有其他方法,例如非线性插值或显示滚动平均值,但这应该作为起点。

Hard to say without sample data, but one option is to show only one data point out of every k data points in the original DataFrame, and interpolate the missing days with straight line segments. (This is basically downsampling.)

For example, to show every 5 data points, change this line:

ax.plot('Date', col, data=weather_data)

to this:

ax.plot('Date', col, data=weather_data.iloc[::5])

There are other approaches such as nonlinear interpolation or showing a rolling average, but this should serve as a starting point.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文