How do I add custom calculated error bars to seaborn bar plots? [duplicate] - matplotlib

This question already has answers here:
How to plot errorbars on seaborn barplot
(1 answer)
Seaborn: Making barplot by group with asymmetrical custom error bars
(1 answer)
How to add error bars on a grouped barplot from a column
(4 answers)
Closed last month.
The situation: university gave us an Excel document. We have a table, just write in our data and we get some tables and plots out. I think the plots are ugly and since I need it for a public presentation, I wanted to redo the plot with seaborn. I managed to actually plot the bars. The only trouble now: I can't add the variance to the bars.
This is my table:
Label,Mean,Error
"Appearance",2.50,0.45
"Functionality",1.90,0.32
"Predictability",2.740,0.52
"Inefficiency",1.701,2.41
This is my code:
import seaborn as sb
import matplotlib.pyplot as plt
import pandas as pd
if __name__ == '__main__':
csv = pd.read_csv('res.csv')
sb.set_theme(style = "darkgrid")
sb.barplot(x = "Mean", y = "Label", data = csv, errorbar="sd")
# plt.savefig('myfile.png', bbox_inches="tight")
plt.show()

Related

Change histogram bars color [duplicate]

This question already has answers here:
Matplotlib histogram with multiple legend entries
(2 answers)
Closed 4 years ago.
I want to colour different bars in a histogram based on which bin they belong to. e.g. in the below example, I want the first 3 bars to be blue, the next 2 to be red, and the rest black (the actual bars and colour is determined by other parts of the code).
I can change the colour of all the bars using the color option, but I would like to be able to give a list of colours that are used.
import numpy as np
import matplotlib.pyplot as plt
data = np.random.rand(1000)
plt.hist(data,color = 'r')
One way may be similar to approach in other answer:
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
data = np.random.rand(1000)
N, bins, patches = ax.hist(data, edgecolor='white', linewidth=1)
for i in range(0,3):
patches[i].set_facecolor('b')
for i in range(3,5):
patches[i].set_facecolor('r')
for i in range(5, len(patches)):
patches[i].set_facecolor('black')
plt.show()
Result:

Legend not dispalyed [duplicate]

This question already has answers here:
Adding a legend to PyPlot in Matplotlib in the simplest manner possible
(6 answers)
Closed 3 years ago.
I am trying to draw a simple graph in python using matplotlib.I am not able to use pyplot.legend() method to display legend.Please help me.
I looked on the net and found a simple code which says it works:
import numpy as np
import matplotlib.pyplot as plt
# generate random data for plotting
x = np.linspace(0.0,100,50)
y = np.random.normal(size=50)
plt.plot(x,y)
# call method plt.legend
plt.legend(['line plot 1'])
plt.show()
from the site
http://queirozf.com/entries/matplotlib-examples-displaying-and-configuring-legends.
My code is below:
import matplotlib.pyplot as plt
%matplotlib inline
views = [123,56,64,54,223,5523]
days = range(1,7)
plt.xlabel("Days")
plt.ylabel("Views")
plt.title("You Tube views")
plt.legend(["Youtube views"])
plt.plot(days,views)
plt.show()
Write plt.legend(["Youtube views"]) next plt.plot(days,views)
plt.xlabel("Days")
plt.ylabel("Views")
plt.title("You Tube views")
plt.plot(days,views)
plt.legend(["Youtube views"])
plt.show()

How to remove all padding in matplotlib subplots when using images [duplicate]

This question already has answers here:
How to combine gridspec with plt.subplots() to eliminate space between rows of subplots
(1 answer)
How to remove the space between subplots in matplotlib.pyplot?
(5 answers)
Closed 3 years ago.
When creating subplots with matplotlib i cannot get tight layout where there would not be any spaces between subplot items.
import numpy as np
import matplotlib.pyplot as plt
fig, axes = plt.subplots(3, 3, figsize=(10,10),gridspec_kw = {'wspace':0, 'hspace':0})
for i, ax in enumerate(axes.ravel()):
im = ax.imshow(np.random.normal(size=200).reshape([10,20]))
ax.axis('off')
plt.tight_layout()
Subplots would consist of images. Seems like there is way to do this when you are not using images. So i assume, there is some configuration about imshow().
I would like to keep aspect ratio of images, but make subplots compact as possible.
this is what i get now, but as you can see, there is a lot of row padding
https://imgur.com/a/u4IntRV

Specify Saved Image Dimensions in Seabron FacetGrid [duplicate]

This question already has answers here:
How to change the figure size of a seaborn axes or figure level plot
(13 answers)
How to change a figure's size in Python Seaborn package
(7 answers)
Closed 4 years ago.
I cannot override the Seaborn defaults on image size.
I would think that the plt.figure(figsize=(8.5,11)) would specify the actual dimensions of the image saved. It does not. The dimensions of the saved file are 4.5" by 2.25". That is different from 8.5" x 11".
import pandas as pd
import seaborn as sb
from matplotlib import pyplot as plt
plt.figure(figsize=(8.5,11))
df = sb.load_dataset('tips')
g = sb.FacetGrid(df, col = "time")
g.map(plt.hist, "tip")
plt.savefig("size_test_plot.jpg")
plt.show()
What is the solution?

Saving a barplot in matplotlib.pyplot [duplicate]

This question already has answers here:
Matplotlib (pyplot) savefig outputs blank image
(5 answers)
Closed 5 years ago.
I have two python lists - a (entries are strings) and b (numerical). I plot them with the following snippet (works perfectly) -
import matplotlib.pyplot as plt
plt.bar(names, values)
plt.suptitle('Average Resale Price (SGD) vs Flat Model')
plt.xticks(rotation='82.5')
plt.show()
Now I try to save the above figure -
plt.savefig('foo.png',dpi=400)
However I end up getting a white figure! How do I save the barplot ?
It's not hard. Try to put plt.savefig('foo.png',dpi=400) before plt.show():
import matplotlib.pyplot as plt
names=['alex', 'simon', 'beta']
values=[10,20,30]
plt.bar(names, values)
plt.suptitle('Average Resale Price (SGD) vs Flat Model')
plt.xticks(rotation='82.5')
plt.savefig('foo.png',dpi=400)
plt.show()