'Minimize repeating colors matplotlib line chart, when plotting pandas dataframe
I have seen that there is several questions about line colors for matplotlib, however have not been able to localize an example with dataframe.
A subset of my raw data is as follows (full set 8400r x 14c)
[
{
"datapoints": [
[
12151151,
1647944400
],
[
5452151,
1647944700
],
[
null,
1647945000
]
],
"targets": "port1.good_rx",
"tags": {
"nonNegativeDerivative": 1,
"name": "good_rx"
}
},
{
"datapoints": [
[
12354545,
1647944400
],
[
25454524,
1647944700
],
[
17222,
1647945000
]
],
"targets": "port2.good_rx",
"tags": {
"nonNegativeDerivative": 1,
"name": "good_rx"
}
}
]
This data is the normalized using
# https://github.com/esc/graphite2pandas/blob/cf914d0d8d5ef3f4e6d6f837366423c5674882ef/graphite2pandas.py
def _localize(index, tz):
index = index.tz_localize('UTC')
index = index.tz_convert('CET')
return index
def g2p(url, localize='CET'):
resp = requests.get(url)
decoded_json = json.loads(resp.content)
times, values, labels = [], [], []
for element in decoded_json:
labels.append(element['target'])
datapoints = list(zip(*element['datapoints']))
values.append(datapoints[0])
times.append(datapoints[1])
index = pandas.DatetimeIndex((numpy.array(times[0],dtype='datetime64[s]')))
if localize:
index = _localize(index, localize)
return pandas.DataFrame(
dict(((labels[i], values[i]) for i in range(len(values)))),
index=index)
Following these steps I then do some cleanup work on the columns and plot the chart.
This is when I realized that a get a lot of repetetive colors, so I tried to set the color using (np.random.random(), np.random.random(), np.random.random()) this resulted in all columns having the the same colors. How could I plot one column at the time in order to randomly generate color per column (line)
df = g2p(URL) #return sa pd.DataFrame() 8400 rows x 14 col - Time(index), bytes(row), port(column)
plot = df.plot(c=(np.random.random(), np.random.random(), np.random.random()))
plot.set_title("test")
fig = plot.get_figure()
fig.savefig("fileout.png", transparent=True, bbox_inches='tight')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|


