This question already has answers here:
Python Matplotlib Boxplot Color
(4 answers)
Closed 2 years ago.
I am struggling to plot a boxplot for custom column names of dataframe and afterwards fill them with custom different colors.
df=pd.DataFrame([[1,2,3],[3,4,3],[3,4,3]],columns=['x','y','z'])
df.boxplot(column=['x', 'y'])
plt.show()
I can't customize colours of my boxplots. Is there any way how to do it with simple code?
Try using this:
df = pd.DataFrame([[1,2,3],[3,4,3],[3,4,3]], columns=['x','y','z'])
box = plt.boxplot([df['x'], df['y']], patch_artist=True)
colors = ['blue', 'green']
for patch, color in zip(box['boxes'], colors):
patch.set_facecolor(color)
plt.show()
Related
This question already has answers here:
Remap values in pandas column with a dict, preserve NaNs
(11 answers)
How to edit a seaborn legend title and labels for figure-level functions
(2 answers)
Edit legend title and labels of Seaborn scatterplot and countplot
(3 answers)
Closed 2 days ago.
I am trying to change a specific part of the legend in my plot in seaborn.
I wrote the following code to display a plot in seaborn. The plot can be seen below.
ax = sns.lineplot(data = weekly_port_ret,
x='week',y='R',hue='high_leverage',style='gsector')
ax.set(title='Weekly Portfolio Returns - Daily Rebalancing',
xlabel='Week in 2020',
ylabel='Weekly Return'
)
plt.show()
I am just trying to change where it says "20.0" in the legend to "Industrial", and where it says "45.0" to "IT". Does anyone know how to do this?
My plot:
You can assign the gsector column to the values (Industrial and IT) and map it so that you can see the legend as you want... Updated code below.
I used some dummy data, but your code should work as well.
Refer assign and map for more info..
mysector = {20:'Industrial', 45:'IT'}
ax = sns.lineplot(data = weekly_port_ret.assign(gsector=weekly_port_ret['gsector'].map(mysector)),
x='week',y='R',hue='high_leverage',style='gsector')
ax.set(title='Weekly Portfolio Returns - Daily Rebalancing',
xlabel='Week in 2020',
ylabel='Weekly Return'
)
plt.show()
Plot
This question already has answers here:
Matplotlib scatter plot legend
(5 answers)
Matplotlib - Adding legend to scatter plot [duplicate]
(1 answer)
Matplotlib scatter plot with legend
(6 answers)
Closed last month.
I'm trying to set the legend according to categorical values set for color but it's not working.
import matplotlib.pyplot as plt
colors = finalDf['sales'].astype('category').cat.codes
scatter = plt.scatter(x= finalDf['principal component 1'],
y= finalDf['principal component 2'],
c = finalDf['sales'].astype('category').cat.codes)
plt.xlabel('PC1')
plt.ylabel('PC2')
plt.legend(labels=list(finalDf['sales'].unique()))
plt.show()
(https://i.stack.imgur.com/YwSoG.png)
I tried various combinations of the 'sales' data columns but it was in vain. I want to know if there is a solution other than using seaborn library as the task recquires matplotlib. Thank you
This question already has answers here:
How to add value labels on a bar chart
(7 answers)
Bar labels in matplotlib/Seaborn
(1 answer)
Closed last month.
I'm looking to display the actual count value of a bar in countplot graph.
When I enter
sns.countplot(x=df["Party"])
I get an output of
output
I'm just looking to have the actual value displayed above each bar.
I don't know what dataset you're using so I'm gonna use a hypothetical one, but this should work.
import seaborn as sns
import pandas as pd
df = pd.read_csv(my_path_to_csv) # hypothetical csv file
ax = sns.barplot(x='x', y='y', data=df) # barplot usage
# the actual part that adds labels
for i in ax.containers:
ax.bar_labels(i,)
Try Uisng matplotlib.pyplot.bar_label
python 3
matplotlib should be > 3.4
Then
plot = sns.countplot(x=df["Party"])
plot.bar_label(plot.containers[0], label_type='edge')
plt.show()
This question already has answers here:
Trying to add color gradients to Matplotlib chart
(1 answer)
Python: Barplot colored according to a third variable
(1 answer)
Changing color scale in seaborn bar plot
(5 answers)
Closed 2 months ago.
I have monthly rain data from a list, where values are on the Y-axis and months on the X-axis.
This is my color palette:
sns.color_palette("crest", as_cmap=True)
This is my code to barplot the data:
plt.figure(figsize=(8,4), tight_layout=True)
colors = sns.color_palette("crest")
plt.bar(bocas_rain['Date'], bocas_rain['Pr'], color=colors)
plt.xlabel('Months')
plt.ylabel('Rain in mm')
plt.title('Rain in Bocas')
plt.show()
The result I am getting is this:
How can I make the highest values of my data match the dark blue colors from the palette?
This question already has answers here:
No outlines on bins of Matplotlib histograms or Seaborn distplots
(3 answers)
Closed 5 years ago.
df3['a'].plot.hist(color='blue',xlim=(0,1))
I want to know how can it show the line in the histogram figure.
Make the top figure showed as bottom figure. Thank you!
Pass the edgecolor argument to hist.
df3['a'].plot.hist(color='blue',
edgecolor='black',
xlim=(0,1))
Demo
df = pd.DataFrame(dict(vals=np.random.normal(size=100)))
df.plot.hist(edgecolor='black')