Reputation: 521
I have a dict
with 200 keys and values that generates this graph.
Also i compute the average of these values and plot that too. I would like the avg value to be the same color as the line. The code that i use is this :
with open('path_to_json/values.json', 'r') as outfile:
a = json.load(outfile)
sum = 0
for k, v in a.items():
sum += v
avg = sum / len(a.values())
fig, ax = plt.subplots(1,1)
p=ax.plot(list(a.keys()),list(a.values()))
p=ax.plot([0, len(a.values())], [avg, avg], "--")
yt = ax.get_yticks()
yt = np.append(yt, avg)
xticks = np.arange(0, len(a.values()), 10)
ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f'))
ax.set_xticks(xticks)
ax.set_yticks(yt)
plt.show()
Upvotes: 4
Views: 469
Reputation: 11224
Have a look at the overview for the Axes
class, especially the "Ticks and tick labels" section: There's get_yticklabels
and get_yticklines
which return the objects which both have a set_color
method.
Minimal example:
import matplotlib.pyplot as plt
import numpy as np
plt.plot([0, 1], [0, 1])
ax = plt.gca()
ax.set_yticks(np.append(ax.get_yticks(), 0.55))
ax.get_yticklabels()[-1].set_color("red")
ax.get_yticklines()[-2].set_color("red")
plt.show()
Upvotes: 3