如何跳过Matplotlib图中的一些X值以降低密度?
我试图在过去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');
Dataset:
https://drive.google.com/uc?id=1O-7DuL6-bkPBpz7mAUZ7M62P6EOyngG2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很难没有示例数据,但是一个选项是仅显示一个数据指出原始数据框中的每个
k
数据点,然后用直线段插入丢失的天数。 (这基本上是降采样。)例如,要显示每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:
to this:
There are other approaches such as nonlinear interpolation or showing a rolling average, but this should serve as a starting point.