[英]How to avoid colors mixing up between two plot types in matplotlib
我有以下结果:
这是一个plt.scatter
有plt.bar
的。
出于某种原因,条形图被着色,但我希望它具有特定颜色,它不属于散点图 plot 使用的 colors 的一部分。 我该如何做到这一点?
我当前的代码:
from collections import OrderedDict
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
def scatter_plot_from_to_delta(start, end, title="Delta times per day"):
filtered = OrderedDict()
# Generating filrered score dict (K=participant, V=list of each day's delta, with 0 as filtered out)
for name in names:
score = list()
for delta in scores.get(name):
if delta > end or delta < start:
delta = 0
score.append(delta)
filtered[name] = score
figure(figsize=(20, 11)) # size of the whole graph
# Preparing data for listed participants
listed = OrderedDict() # (K=name displayed in the graph, V=amount of times displayed)
latest_listed_day = 1 # ends up determining the last X axis value in the graph
for key, value in filtered.items():
listed[key] = dict()
listed[key]['amnt'] = 0
x = list()
y = list()
for i, val in enumerate(value):
if val != 0:
x.append(i+1)
latest_listed_day = max(i+1, latest_listed_day)
y.append(val)
listed[key]['amnt'] += 1
if listed[key]['amnt'] == 0:
del listed[key]
else:
listed[key]['x'] = x
listed[key]['y'] = y
ordered_counter = OrderedDict(sorted(listed.items(), reverse=True, key=lambda item: item[1]['amnt']))
print(ordered_counter)
# Scatter plot
for key, value in ordered_counter.items():
x = value['x']
y = value['y']
plt.scatter(x, y, s=200) # s=size of the dots
for i, _ in enumerate(x):
plt.annotate(key, (x[i]+0.15, y[i]-0.27)) # marking names by each dot
# Bar chart for the amount of listed participants on each day
for i in range(num_challenges-1):
participants_listed_that_day = 0
for _, val in filtered.items():
s = val[i]
if s != 0:
participants_listed_that_day += 1
if participants_listed_that_day != 0:
plt.bar(i+1, participants_listed_that_day)
# Displaying the total per day above each bar
plt.text(i+0.908,
participants_listed_that_day+0.15,
str(participants_listed_that_day),
fontsize=18)
# Concatenate (sorted) amount of times displayed for more info in legend
displayed = list()
for key, value in ordered_counter.items():
concat = "(" + str(value['amnt']) + ") " + key
displayed.append(concat)
# Legend of listed participants
plt.legend(displayed,
scatterpoints=11,
loc='center left',
bbox_to_anchor=(1, 0.5),
ncol=1,
fontsize=16)
plt.xticks(range(1,latest_listed_day+1)) # ensure x axis tick for every day
plt.xlim((0.5,latest_listed_day+1.5)) # force displayed x range
plt.title(title, fontsize=30)
plt.xlabel("Day")
plt.ylabel("Delta (in seconds)")
plt.show()
You can choose two discrete or qualitative colormaps that have distinct colors from each other (and also enough colors to exceed the number of categories in your data), such as tab10
and tab20b
, then update the scatter plot and bar chart portions of your function.
import matplotlib.cm as cm
# Scatter plot
for key, value in ordered_counter.items():
x = value['x']
y = value['y']
plt.scatter(x, y, s=200, cmap=cm.tab10) # s=size of the dots
for i, _ in enumerate(x):
plt.annotate(key, (x[i]+0.15, y[i]-0.27)) # marking names by each dot
# Bar chart for the amount of listed participants on each day
for i in range(num_challenges-1):
participants_listed_that_day = 0
for _, val in filtered.items():
s = val[i]
if s != 0:
participants_listed_that_day += 1
if participants_listed_that_day != 0:
plt.bar(i+1, participants_listed_that_day, color=cm.tab20b)
# Displaying the total per day above each bar
plt.text(i+0.908,
participants_listed_that_day+0.15,
str(participants_listed_that_day),
fontsize=18)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.