Python matplotlib 轮廓阴影线为黑色而不是白色

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

我正在尝试使用contourf中的阴影线在变量上绘制显着性掩模。

我没有设法将舱口的颜色从白色更改为黑色(想要)。我尝试过边缘颜色、面部颜色、颜色和轮廓和轮廓代码的多种组合。任何帮助将不胜感激!

这是我的代码:

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import cartopy.crs as ccrs
from cartopy.feature import NaturalEarthFeature


all_plots = []
plt.rcParams.update({'hatch.color': 'k'})

land = NaturalEarthFeature('physical', 'land', '50m', edgecolor='face', facecolor='white')

# Create a 2x2 subplot (2 rows, 2 columns) for 4 plots, with Miller projection
fig, axs = plt.subplots(2, 2, figsize=(18, 9), subplot_kw={'projection': ccrs.Miller()})
plt.subplots_adjust(hspace=0.25, wspace=0.1)  # Adjust spacing

# Shared colorbar arguments
cmap = 'RdBu_r'

# Function to set up common gridlines and features
def setup_ax(ax, title):
    ax.coastlines(zorder=2)
    ax.add_feature(land, zorder=1)
    ax.set_extent([-180, 180, -1, 90], crs=ccrs.PlateCarree())  # Limit y-axis to 0-90 degrees
    gl = ax.gridlines(draw_labels=True, linewidth=0.6, zorder=1, color='dimgray', alpha=0.3)
    gl.left_labels = True
    gl.bottom_labels = True
    gl.top_labels = False
    gl.right_labels = False
    gl.xlabel_style = {'size': 10}
    gl.ylabel_style = {'size': 10}
    gl.ylocator = plt.FixedLocator(np.arange(0, 91, 20))  # Add y-axis gridlines at 0°, 20°, 40°, etc.
    gl.xlocator = plt.FixedLocator(np.arange(-180, 181, 60))  # Optional: adjust x-axis labels
    ax.set_title(title, fontsize=15)

# Plot 1: Strongest months of hailseason (May-August)
im = mean_sst_strongest_months_north.plot(ax=axs[0, 0], cmap=cmap, add_colorbar=False, 
vmin=vmin, vmax=vmax, transform=ccrs.PlateCarree())
# Overlay the significance mask for the first plot
cax1 = axs[0, 0].contourf(significant_mask_months_strongest_north.longitude, 
                      significant_mask_months_strongest_north.latitude, 
                      significant_mask_months_strongest_north, levels=[0.5, 1], 
                      colors='none', hatches=['oo'], transform=ccrs.PlateCarree())
setup_ax(axs[0, 0], 'Strongest months of hailseason (May-Aug)')
for ax in axs.flat:
    rect = Rectangle((5.5, 45.3), 5.4, 2.7, linewidth=1.5, edgecolor='limegreen', 
facecolor='none', transform=ccrs.PlateCarree())
    ax.add_patch(rect)

[... other 3 plots]

# Add a shared colorbar at the bottom
cbar = fig.colorbar(im, ax=axs, pad=0.06, orientation="horizontal", shrink=0.7, 
extend="both", aspect=50)
cbar.set_label('SST anomalies [K]', fontsize=15)

# Add a main title for the figure
fig.suptitle('Mean SST anomalies of 10 strongest hailyears in northern Switzerland', 
fontsize=18, y=0.97)

# Save the plot to the specified directory
plt.savefig("/sst_strongest_4periods_north_significance_95.jpeg", dpi=350, 
bbox_inches='tight', pad_inches=0.05)

# Close all open plots
plt.close('all')

情节现在看起来如何:

enter image description here

python matplotlib plot contour contourf
1个回答
0
投票

eцрвене звезде

您可以直接操作轮廓对象的“隐藏”属性

import matplotlib.pyplot as plt
import numpy as np

# we'll plot circles
x = y = range(-5, 6)
x, y = np.meshgrid(x, y)
z = np.sqrt(x*x+y*y)

fig, ax = plt.subplots()

# circles, not ellipses
ax.set_aspect(1)

# first the contours
c0 = ax.contour(x, y, z, levels=range(7))
# next a single filled contour
c1 = ax.contourf(x, y, z,
    levels=[3, 4], colors='w', hatches=['**'])
# you are not supposed to do this
c1._hatch_color = (1, .4, .4, 1.0)

plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.