I am trying to use a dictionary to control the color and hatching of a fill on a matplotlib plot using fill_betweenx()
.
I have had success using lists like in the example below. However, I am struggling to work out how I could use a dictionary in a similar way. The intention is that the number in the first part of the dictionary relates to a column in a dataframe and when I come to plot the data it should lookup the relevant hatch and color arguments from the dictionary.
What would be the best way to achieve this?
Here is an example dictionary that I am wanting to use in place of the lists
example_dict = {1:{'lith':'sandstone', 'hatch':'.', 'color':'yellow'},
2:{'lith':'fine sand', 'hatch':'..', 'color':'yellow'},
3:{'lith':'mudstone', 'hatch':'-', 'color':'green'},
4:{'lith':'laminated shale', 'hatch':'--', 'color':'green'}}
Working code using lists.
import matplotlib.pyplot as plt
y = [0, 1]
x = [1, 1]
fig, axes = plt.subplots(ncols=4,nrows=1, sharex=True, sharey=True,
figsize=(10,5), subplot_kw={'xticks': [], 'yticks': []})
colors = ['yellow', 'yellow', 'green', 'green']
hatchings = ['.', '..', '-', '--']
for ax, color, hatch in zip(axes.flat, colors, hatchings):
ax.plot(x, y)
ax.fill_betweenx(y, 0, 1, facecolor=color, hatch=hatch)
ax.set_xlim(0, 0.1)
ax.set_ylim(0, 1)
ax.set_title(str(hatch))
plt.tight_layout()
plt.show()
This generates:
question from:
https://stackoverflow.com/questions/66049669/python-using-a-dictionary-for-matplotlib-colors-and-hatching