Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

Python: Using a dictionary for matplotlib colors and hatching

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:
matplotlib hatching with lists

question from:https://stackoverflow.com/questions/66049669/python-using-a-dictionary-for-matplotlib-colors-and-hatching

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Just replace what you iterate over to be the dict keys and then access the color or hatch within the code:

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': []})
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'}}

for ax, key in zip(axes.flat, example_dict.keys()):
    ax.plot(x, y)
    ax.fill_betweenx(y, 0, 1, facecolor=example_dict[key]['color'], hatch=example_dict[key]['hatch'])
    ax.set_xlim(0, 0.1)
    ax.set_ylim(0, 1)
    ax.set_title(str(example_dict[key]['hatch']))

plt.tight_layout()
plt.show()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...