When I run the below code, I get a figure with gradient color (from black to orange). Please look at the attached figure. Whereas I want to get a figure only with single color, orange (not figure with a gradient color). How can I do that?
My code:
#!/usr/bin/python3
import numpy as np
import pylab as plot
import matplotlib.pyplot as plt
import numpy, scipy, pylab, random
from matplotlib.ticker import MultipleLocator
import matplotlib as mpl
from matplotlib.ticker import MaxNLocator
import seaborn as sns
import pandas as pd
fig, ax = plt.subplots(figsize=(4, 2))
df = pd.read_csv('input.txt', sep="\s\s+", engine='python')
sns.kdeplot(data=df, label = "s1", color = "orange", cmap=None)
plt.xlabel('x', fontsize=7)
plt.ylabel('y', fontsize=7)
for axis in ['top','bottom','left','right']:
ax.spines[axis].set_linewidth(0.5)
plt.savefig("plot.png", dpi=300, bbox_inches='tight')
input.txt:
0.43082 0.45386
0.35440 0.91632
0.16962 0.85031
0.07069 0.54742
0.31648 1.06689
0.57874 1.17532
0.18982 1.01678
0.31012 0.54656
0.31133 0.81658
0.53612 0.50940
0.36633 0.83130
0.37021 0.74655
0.28335 1.30949
0.11517 0.63141
0.24908 1.04403
-0.28633 0.46673
-0.13251 0.33448
-0.00568 0.53939
-0.03536 0.76191
0.24695 0.92592
Using Seaborn v0.11.2.
Solution:
The expected output is obtained if you specify x
and y
arguments of seaborn.kdeplot
as the column names of the DataFrame (after appropriately reading the data as suggested in Timeless's answer, i.e., by specifying header=None
in pd.read_csv
):
x, y: vectors or keys in data
Variables that specify positions on the x and y axes.
df = pd.read_csv('input.txt', sep="\s\s+", engine='python', header=None)
print(df)
sns.kdeplot(data=df, x=0, y=1, color = "orange")
df
:
0 1
0 0.43082 0.45386
1 0.35440 0.91632
2 0.16962 0.85031
3 0.07069 0.54742
4 0.31648 1.06689
5 0.57874 1.17532
6 0.18982 1.01678
7 0.31012 0.54656
8 0.31133 0.81658
9 0.53612 0.50940
10 0.36633 0.83130
11 0.37021 0.74655
12 0.28335 1.30949
13 0.11517 0.63141
14 0.24908 1.04403
15 -0.28633 0.46673
16 -0.13251 0.33448
17 -0.00568 0.53939
18 -0.03536 0.76191
19 0.24695 0.92592
Expected figure output:
sns.kdeplot
plots distribution of each column in the data when data
is passed to it without specifying x
and y
:
The code in the question throws an error:
AttributeError: 'Line2D' object has no property 'cmap'
If you now don't pass cmap
to sns.kdeplot
and use:
df = pd.read_csv('input.txt', sep="\s\s+", engine='python',)
sns.kdeplot(data=df, label = "s1", color="orange")
it returns (notice the legend labels of the line plots):