Matplotlib Colorbar 缺少 1 个必需的位置参数:'mappable'
Matplotlib Colorbar missing 1 required positional argument: 'mappable'
我想为下面的数据框创建一个散点图:
df_sample.head(10)
duration distance speed
0 26.299999 3.569 8.1
1 6.000000 0.739 7.4
2 25.700001 2.203 5.1
3 34.400002 2.876 5.0
4 3.000000 0.656 13.1
5 29.299999 3.704 7.6
6 10.200000 2.076 12.2
7 4.000000 0.774 11.6
8 9.200000 1.574 10.3
9 10.800000 0.782 4.3
使用下面的代码几乎可以完成它。我想根据速度(黄色:最慢和蓝色:最快)向图形添加颜色条,最终我在最后一行的 fig.colorbar(ax=ax)
处遇到错误。请指教:什么是mappable
?
with plt.style.context('seaborn-ticks'):
fig, ax = plt.subplots(figsize = (10, 6))
ax.set_title('Relationship between Distance & Duration', fontdict={'fontsize': 18, 'fontweight': 'bold'}, loc='left', pad=20)
ax.scatter(x=df_sample.duration.values, y=df_sample.distance.values, c=df_sample.speed.values, cmap=cm.YlGnBu)
# remove top & right spines
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
# equivalent to 'sns.despine(offset = 5)'
ax.spines['left'].set_position(('outward', 5))
ax.spines['left'].set_linewidth(1.2)
ax.spines['bottom'].set_position(('outward', 5))
ax.spines['bottom'].set_linewidth(1.2)
# set ticks & ticklabels
xloc = np.arange(0, 45, 5)
ax.set_xticks(xloc)
ax.set_xticklabels(labels=xloc, fontdict={'fontsize': 12})
ax.set_xlabel('Minute(s)', fontdict={'fontsize': 14, 'fontweight': 'bold'})
yloc = np.arange(6)
ylab = [f"{int(num)}" for num in yloc]
ax.set_yticks(yloc)
ax.set_yticklabels(labels=ylab, fontdict={'fontsize': 12})
ax.set_ylabel("Distance (KM)" , fontdict={'fontsize': 14, 'fontweight': 'bold'})
fig.colorbar(ax=ax);
您可以将散点图分配给一个变量,例如:
sp=ax.scatter(x=df_sample.duration.values, y=df_sample.distance.values, c=df_sample.speed.values, cmap=cm.YlGnBu)
然后将其作为 mappable object 传递给颜色条:
fig.colorbar(sp)
我想为下面的数据框创建一个散点图:
df_sample.head(10)
duration distance speed
0 26.299999 3.569 8.1
1 6.000000 0.739 7.4
2 25.700001 2.203 5.1
3 34.400002 2.876 5.0
4 3.000000 0.656 13.1
5 29.299999 3.704 7.6
6 10.200000 2.076 12.2
7 4.000000 0.774 11.6
8 9.200000 1.574 10.3
9 10.800000 0.782 4.3
使用下面的代码几乎可以完成它。我想根据速度(黄色:最慢和蓝色:最快)向图形添加颜色条,最终我在最后一行的 fig.colorbar(ax=ax)
处遇到错误。请指教:什么是mappable
?
with plt.style.context('seaborn-ticks'):
fig, ax = plt.subplots(figsize = (10, 6))
ax.set_title('Relationship between Distance & Duration', fontdict={'fontsize': 18, 'fontweight': 'bold'}, loc='left', pad=20)
ax.scatter(x=df_sample.duration.values, y=df_sample.distance.values, c=df_sample.speed.values, cmap=cm.YlGnBu)
# remove top & right spines
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
# equivalent to 'sns.despine(offset = 5)'
ax.spines['left'].set_position(('outward', 5))
ax.spines['left'].set_linewidth(1.2)
ax.spines['bottom'].set_position(('outward', 5))
ax.spines['bottom'].set_linewidth(1.2)
# set ticks & ticklabels
xloc = np.arange(0, 45, 5)
ax.set_xticks(xloc)
ax.set_xticklabels(labels=xloc, fontdict={'fontsize': 12})
ax.set_xlabel('Minute(s)', fontdict={'fontsize': 14, 'fontweight': 'bold'})
yloc = np.arange(6)
ylab = [f"{int(num)}" for num in yloc]
ax.set_yticks(yloc)
ax.set_yticklabels(labels=ylab, fontdict={'fontsize': 12})
ax.set_ylabel("Distance (KM)" , fontdict={'fontsize': 14, 'fontweight': 'bold'})
fig.colorbar(ax=ax);
您可以将散点图分配给一个变量,例如:
sp=ax.scatter(x=df_sample.duration.values, y=df_sample.distance.values, c=df_sample.speed.values, cmap=cm.YlGnBu)
然后将其作为 mappable object 传递给颜色条:
fig.colorbar(sp)