具有共享日期时间颜色条的散点图

问题描述 投票:0回答:1

我想根据经度和纬度绘制散点图。散点图的大小取决于另一个值“LF”。

longitude = df['evlo']
latitude = df['evla']
lf = df['LF']

# Define the size based on the LF values
size = lf

还有另一列包含日期信息,我将其转换为日期时间格式:

df['dates'] = pd.to_datetime(df['fold'].str[0:8], format='%Y%m%d')

df['dates'] = df['dates'].dt.strftime('%Y-%m-%d')

输出采用以下格式:

1       2020-04-20
2       2020-04-20
3       2020-04-20
4       2020-04-21
           ...    
1485    2020-06-12
1486    2020-06-12
1487    2020-06-12
1488    2020-06-12
1489    2020-06-12

我想使用共享颜色条绘制间隔 15 天的散点子图。我已经完成了这项工作,但似乎每个子图都有自己的 colorba,例如每个子图中都存在蓝色和红色,但在实际情况下不应该出现。以下是我的尝试:

`import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
import pandas as pd
import matplotlib.cm as cm
import matplotlib.colors as colors

# Filter data based on LF values

df_lf_more_than_1 = df[df['LF'] > 1]

# Get the minimum and maximum dates in the filtered data
min_date = df_lf_more_than_1['dates'].min()
max_date = df_lf_more_than_1['dates'].max()


# Create a figure with four subplots
fig, axes = plt.subplots(2, 2, figsize=(12, 10),sharex=True, sharey=True)
axes = axes.flatten()    
for i, ax in enumerate(axes):
    # Calculate the start and end dates for the current interval
    start_date = min_date + pd.DateOffset(days=i * 15)
    end_date = start_date + pd.DateOffset(days=14)

    
    # Filter the data for the current interval
    df_interval = df_lf_more_than_1[(df_lf_more_than_1['dates'] >= start_date)
                                    & (df_lf_more_than_1['dates'] <= end_date)]
    # Plot the scatter plot for the current interval
    scatter = ax.scatter(df_interval['evlo'], df_interval['evla'], 
                         s=df_interval['LF']*5,
                         c=mdates.date2num(df_interval['dates']), cmap='jet')
        
 
    # Set the title of the subplot to the start and end dates
    ax.set_title(f'Interval: {start_date.strftime("%Y-%m-%d")} - {end_date.strftime("%Y-%m-%d")}')

    ax.xaxis.set_major_formatter(plt.FormatStrFormatter('%.2f'))
        

cax = fig.add_axes([0.15, 0.05, 0.7, 0.03])  # Position of the colorbar
cbar = fig.colorbar(cm.ScalarMappable(norm=norm, cmap='jet'), cax=cax, orientation='horizontal')

plt.subplots_adjust(bottom=0.09)

plt.show()`

python matplotlib datetime colorbar
1个回答
0
投票

我相信发生这种情况是因为您在散点图时没有指定

vmin
vmax
。如果不指定数据范围,则使用颜色图的整个范围来覆盖数据。请参阅此处的描述。如果您为每个图制作单独的颜色条,您可以看到这一点:

from matplotlib import pyplot as plt
import numpy as np

# two subplots
fig, (ax1, ax2) = plt.subplots(nrows = 1, ncols = 2, figsize = (7, 3))
fig.suptitle("Not specifying vmin and vmax")

# plotting two plots with different time ranges
for ax, t1, t2 in zip([ax1, ax2], [0, 5], [5, 10]):
    longitude = np.random.uniform(10, 20, 50)
    latitude = np.random.uniform(50, 60, 50)
    t = np.linspace(t1, t2, 50)

    tmp = ax.scatter(x = longitude, y = latitude, c = t, cmap = "jet")
    plt.colorbar(tmp, ax = ax)

plt.show()

生成此图,其中两者的颜色范围相同,但值不同。

如果为所有子图指定相同的数据范围,例如在您的初始和最终时间步长中,所有点都将根据相同的颜色范围进行着色。在示例中,完整范围是从 0 到 10。

tmp = ax.scatter(x = longitude, y = latitude, c = t, cmap = "jet", vmin = 0, vmax = 10)

这产生了这个情节:

每个颜色条上的值相同,并且点按相同比例着色。

因此,为了使共享颜色条有意义,您需要确保颜色图的数据范围由所有子图共享,并且该数据范围对应于数据中的初始和最终时间步长。

© www.soinside.com 2019 - 2024. All rights reserved.