You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Python绘制的柱状图中特定柱子的颜色

解决方案

要实现指定站点柱子单独着色,你可以先生成一个与站点数量匹配的颜色列表,默认值统一为灰色,再修改目标站点对应位置的颜色值为tomato即可,修改后的绘图相关代码如下:

# 配置颜色:默认全为灰色,指定站点设为tomato
target_site = "替换为你要高亮的实际站点名称"  # 填入你要单独着色的站点完整列名
colors = ["gray"] * len(annualmean)
# 获取目标站点在索引中的位置
target_idx = annualmean.index.get_loc(target_site)
colors[target_idx] = "tomato"

# 绘图部分
fig = plt.figure(figsize=(8,4))
axs = fig.add_subplot(1,2,1)
# 把color参数替换为上面生成的colors列表
ax = annualmean.plot(kind='bar', ax=axs, color = colors, legend=None)
plt.axhline(y=5, xmin=0, xmax=1, linestyle=':', linewidth=1, color = 'k')
plt.xticks([],[])    
ax.set_ylabel('annual mean concentration of PM10 ($\mu g.m^{-3}$)')

如果需要同时高亮多个站点,可遍历目标站点列表修改对应位置颜色:

target_sites = ["站点1_pm10", "站点2_pm10"]
colors = ["gray"] * len(annualmean)
for site in target_sites:
    target_idx = annualmean.index.get_loc(site)
    colors[target_idx] = "tomato"

内容的提问来源于stack exchange,提问作者bot_genius

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 05:15:05