This question already has answers here:
How to plot one line in different colors
(5 answers)
Closed 5 years ago.
Various versions of this question have been asked before, and I'm not sure if I'm supposed to ask my question on one of the threads or start a new thread. Here goes:
I have a pandas dataframe where there is a column (eg: speed) that I'm trying to plot, and then another column (eg: active) which is, for now, true/false. Depending on the value of active, I'd like to color the line plot.
This thread seems to be the "right" solution, but I'm having an issue:
seaborn or matplotlib line chart, line color depending on variable The OP and I are trying to achieve the same thing:
Here's a broken plot/reproducer:
Values=[3,4,6, 6,5,4, 3,2,3, 4,5,6]
Colors=['red','red', 'red', 'blue','blue','blue', 'red', 'red', 'red', 'blue', 'blue', 'blue']
myf = pd.DataFrame({'speed': Values, 'colors': Colors})
grouped = myf.groupby('colors')
fig, ax = plt.subplots(1)
for key, group in grouped:
group.plot(ax=ax, y="speed", label=key, color=key)
The resultant plot has two issues: not only are the changed color lines not "connected", but the colors themselves connect "across" the end points:
What I want to see is the change from red to blue and back look like it's all one contiguous line.
Color line by third variable - Python seems to do the right thing, but I am not dealing with "linear" color data. I basically am assigning a set of line colors in a column. I could easily set the values of the color column to numericals:
Colors=['1','1', '1', '2','2'...]
if that makes generating the desired plot easier.
There is a comment in the first thread:
You could do it if you'll duplicate points when color changed, I've
modified answer for that
But I basically copied and pasted the answer, so I'm not sure that comment is entirely accurate.
Setup
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
Values=[3,4,6, 6,5,4, 3,2,3, 4,5,6]
Colors=['red','red', 'red', 'blue','blue','blue', 'red', 'red', 'red', 'blue', 'blue', 'blue']
myf = pd.DataFrame({'speed': Values, 'colors': Colors})
Solution
1. Detect color change-points and label subgroups of contiguous colors, based on Pandas "diff()" with string
myf['change'] = myf.colors.ne(myf.colors.shift().bfill()).astype(int)
myf['subgroup'] = myf['change'].cumsum()
myf
colors speed change subgroup
0 red 3 0 0
1 red 4 0 0
2 red 6 0 0
3 blue 6 1 1
4 blue 5 0 1
5 blue 4 0 1
6 red 3 1 2
7 red 2 0 2
8 red 3 0 2
9 blue 4 1 3
10 blue 5 0 3
11 blue 6 0 3
2. Create gaps in the index in which to fit duplicated rows between color subgroups
myf.index += myf['subgroup'].values
myf
colors speed change subgroup
0 red 3 0 0
1 red 4 0 0
2 red 6 0 0
4 blue 6 1 1 # index is now 4; 3 is missing
5 blue 5 0 1
6 blue 4 0 1
8 red 3 1 2 # index is now 8; 7 is missing
9 red 2 0 2
10 red 3 0 2
12 blue 4 1 3 # index is now 12; 11 is missing
13 blue 5 0 3
14 blue 6 0 3
3. Save the indexes of each subgroup's first row
first_i_of_each_group = myf[myf['change'] == 1].index
first_i_of_each_group
Int64Index([4, 8, 12], dtype='int64')
4. Copy each group's first row to the previous group's last row
for i in first_i_of_each_group:
# Copy next group's first row to current group's last row
myf.loc[i-1] = myf.loc[i]
# But make this new row part of the current group
myf.loc[i-1, 'subgroup'] = myf.loc[i-2, 'subgroup']
# Don't need the change col anymore
myf.drop('change', axis=1, inplace=True)
myf.sort_index(inplace=True)
# Create duplicate indexes at each subgroup border to ensure the plot is continuous.
myf.index -= myf['subgroup'].values
myf
colors speed subgroup
0 red 3 0
1 red 4 0
2 red 6 0
3 blue 6 0 # this and next row both have index = 3
3 blue 6 1 # subgroup 1 picks up where subgroup 0 left off
4 blue 5 1
5 blue 4 1
6 red 3 1
6 red 3 2
7 red 2 2
8 red 3 2
9 blue 4 2
9 blue 4 3
10 blue 5 3
11 blue 6 3
5. Plot
fig, ax = plt.subplots()
for k, g in myf.groupby('subgroup'):
g.plot(ax=ax, y='speed', color=g['colors'].values[0], marker='o')
ax.legend_.remove()
I took a crack at it. Following the comments in the other question that you linked lead me to this. I did have to get down to matplotlib and couldn't do it in pandas itself. Once I converted the dataframe into lists, its pretty much the same code as the one from the mpl page.
I create the dataframe similar to yours:
vals=[3,4,6, 6,5,4, 3,2,3, 4,5,6]
colors=['red' if x < 5 else 'blue' for x in vals]
df = pd.DataFrame({'speed': vals, 'danger': colors})
Converting the vals and index into lists
x = df.index.tolist()
y = df['speed'].tolist()
z = np.array(list(y))
Break down the vals and index into points and then create line segments
out of them.
points = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
Create the colormap based on the criteria used while creating the dataframe. In my case, speed less than 5 is red and rest is blue.
cmap = ListedColormap(['r', 'b'])
norm = BoundaryNorm([0, 4, 10], cmap.N)
Create the line segments and assign the colors accordingly
lc = LineCollection(segments, cmap=cmap, norm=norm)
lc.set_array(z)
Plot !
fig = plt.figure()
plt.gca().add_collection(lc)
plt.xlim(min(x), max(x))
plt.ylim(0, 10)
Here is the output:
Note: In the current code, the color of the line segment is dependent on the starting point. But hopefully, this gives you an idea.
I'm still new to answering questions here. Let me know if I need to add/remove some details. Thanks!
Related
I have the following dataframe
import pandas as pd
test = pd.DataFrame({'y':[1,2,3,4,5,6], 'label': ['bottom', 'top','bottom', 'top','bottom', 'top']})
y label
0 1 bottom
1 2 top
2 3 bottom
3 4 top
4 5 bottom
5 6 top
I would like to add a new column, agg_y, which would be the the max(y) if label=="bottom" and min(y) if label=="top". I have tried this
test['min_y'] = test.groupby('label').y.transform('min')
test['max_y'] = test.groupby('label').y.transform('max')
test['agg_y'] = np.where(test.label == "bottom", test.max_y, test.min_y)
test.drop(columns=['min_y', 'max_y'], inplace=True)
which gives the correct result
y label agg_y
0 1 bottom 5
1 2 top 2
2 3 bottom 5
3 4 top 2
4 5 bottom 5
5 6 top 2
I am just looking fora one-liner solution, if possible
Your solution in one line solution is:
test['agg_y'] = np.where(test.label == "bottom",
test.groupby('label').y.transform('max'),
test.groupby('label').y.transform('min'))
Solution without groupby, thank you #ouroboros1:
test['agg_y'] = np.where(test.label == 'bottom',
test.loc[test.label.eq('bottom'), 'y'].max(),
test.loc[test.label.ne('bottom'), 'y'].min())
Another idea is mapping values, idea is similar like ouroboros1 solution:
d = {'bottom':'max', 'top':'min'}
test['agg_y'] = test['label'].map({val:test.loc[test.label.eq(val),'y'].agg(func)
for val, func in d.items()})
print (test)
y label agg_y
0 1 bottom 5
1 2 top 2
2 3 bottom 5
3 4 top 2
4 5 bottom 5
5 6 top 2
i would like to plot a seaborn count plot per below :
df3 = pd.DataFrame({'Class' : [1,1, 2 ,2, 2, 3, 3,3], 'check' : [0,1,0,1,0,1,0,1]})
df3
Class check
0 1 0
1 1 1
2 2 0
3 2 1
4 2 0
5 3 1
6 3 0
7 3 1
sns.countplot(data =df3, y = 'Class', hue = 'check', orient = 'v')
I would like to get the result like this but :
the blue line to represent all counts not 0s only, so the first blue line would have count of 2, the 2nd blue line count of 3...
Or even more ideal would be instead of 2 lines per row to have only 1 line, with total value (count of 0s and 1s) and count of 1s on it.
From this,
to This:
IIUC, try:
pd.crosstab(df3['Class'], df3['check']).plot.barh(stacked=True)
Output:
import matplotlib.ticker as mticker
ax = pd.crosstab(df3['Class'], df3['check']).plot.barh(stacked=True)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.xaxis.set_major_locator(mticker.MultipleLocator(1))
_ = ax.set_xlabel('Count')
Output:
I need to show n (e.g. 5) box plots. How can I do it?
df =
col1 col2 col3 col4 col5 result
1 3 1 1 4 0
1 2 2 4 9 1
1 2 1 3 7 1
This is my current code. But it does not display the data inside plots. Also, plots are very thin if n is for example 10 (is it possible to go to a new line automatically?).
n=5
columns = df.columns
i = 0
fig, axes = plt.subplots(1, n, figsize=(20,5))
for ax in axes:
df.boxplot(by="result", column = [columns[i]], vert=False, grid=True)
i = i + 1
display(fig)
This example is for Azure Databricks, but I appreciate just a matplotlib solution as well if it's applicable.
I am not sure I got what you are trying to do, but the following code will show you the plots. You can control the figure sizes by changing the values of (10,10)
Code:
df.boxplot(by="result",figsize=(10,10));
Result:
To change the Vert and show the grid :
df.boxplot(by="result",figsize=(10,10),vert=False, grid=True);
I solved it myself as follows:
df.boxplot(by="result", column = columns[0:4], vert=False, grid=True, figsize=(30,10), layout = (3, 5))
If you want additional row to be generated, while fixing the number of columns to be constant: adjust the layout as follows:
In [41]: ncol = 2
In [42]: df
Out[42]:
v0 v1 v2 v3 v4 v5 v6
0 0 3 6 9 12 15 18
1 1 4 7 10 13 16 19
2 2 5 8 11 14 17 20
In [43]: df.boxplot(by='v6', layout=(df.shape[1] // ncol + 1, ncol)) # use floor division to determine how many row are required
I want to plot factor plots for my data. I tried doing it the below way but the chart values didn't turn out as expected.
df = pd.DataFrame({'subset_product':['A','A','A','B','B','C','C'],
'subset_close':[1,1,0,1,1,1,0]})
prod_counts = df.groupby('subset_product').size().rename('prod_counts')
df['prod_count'] = df['subset_product'].map(prod_counts)
g = sns.factorplot(y='prod_count',x='subset_product',hue='subset_close',data=df,kind='bar',palette='muted',legend=False,ci=None)
plt.legend(loc='best')
However, my plots all have the same height, meaning it didn't separate the data into '1' and '0'.
Example: For A, the blue bar should have height = 1, and the green bar should have height = 2.
The problem is your 'prod_count'.
print(df)
# subset_close subset_product prod_count
# 0 1 A 3
# 1 1 A 3
# 2 0 A 3
# 3 1 B 2
# 4 1 B 2
# 5 1 C 2
# 6 0 C 2
You are telling seaborn that y is 3 when subset_close == 1 & subset_product == A and y is also 3 when subset_close == 0 & subset_product == A.
Below should do what you want.
# Count the number of each (`subset_close`, `subset_product`) combination.
df2 = df.groupby(['subset_product', 'subset_close']).size().reset_index(name='prod_count')
# Plot
g = sns.factorplot(y='prod_count', x='subset_product', hue='subset_close', data=df2,
kind='bar', palette='muted', legend=False, ci=None)
plt.legend(loc='best')
plt.show()
print(df2)
# subset_product subset_close prod_count
# 0 A 0 1
# 1 A 1 2
# 2 B 1 2
# 3 C 0 1
# 4 C 1 1
I want to draw bar chart for below data:
4 1406575305 4
4 -220936570 2
4 2127249516 2
5 -1047108451 4
5 767099153 2
5 1980251728 2
5 -2015783241 2
6 -402215764 2
7 927697904 2
7 -631487113 2
7 329714360 2
7 1905727440 2
8 1417432814 2
8 1906874956 2
8 -1959144411 2
9 859830686 2
9 -1575740934 2
9 -1492701645 2
9 -539934491 2
9 -756482330 2
10 1273377106 2
10 -540812264 2
10 318171673 2
The 1st column is the x-axis and the 3rd column is for y-axis. Multiple data exist for same x-axis value. For example,
4 1406575305 4
4 -220936570 2
4 2127249516 2
This means three bars for 4 value of x-axis and each of bar is labelled with tag(the value in middle column). The sample bar chart is like:
http://matplotlib.org/examples/pylab_examples/barchart_demo.html
I am using matplotlib.pyplot and np. Thanks..
I followed the tutorial you linked to, but it's a bit tricky to shift them by a nonuniform amount:
import numpy as np
import matplotlib.pyplot as plt
x, label, y = np.genfromtxt('tmp.txt', dtype=int, unpack=True)
ux, uidx, uinv = np.unique(x, return_index=True, return_inverse=True)
max_width = np.bincount(x).max()
bar_width = 1/(max_width + 0.5)
locs = x.astype(float)
shifted = []
for i in range(max_width):
where = np.setdiff1d(uidx + i, shifted)
locs[where[where<len(locs)]] += i*bar_width
shifted = np.concatenate([shifted, where])
plt.bar(locs, y, bar_width)
If you want you can label them with the second column instead of x:
plt.xticks(locs + bar_width/2, label, rotation=-90)
I'll leave doing both of them as an exercise to the reader (mainly because I have no idea how you want them to show up).