Legend not dispalyed [duplicate] - matplotlib

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()

Related

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

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()

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?

jupyter qtconsole, matplotlib: scale displayed image to window width [duplicate]

This question already has answers here:
How do I change the size of figures drawn with Matplotlib?
(14 answers)
Closed 4 years ago.
I'd like to scale images that I display in jupyter qtconsole to window width.
All images are shown just in the size of 5x5cm, nevertheless how large they really are. Tried that with a 1k by 1k image and a 7,7k by 7,7k image. Shows the same.
I don't think it's a matter of astropy but matplotlib or jupyter qtconsole.
Couldn't find anything on docu of matplotlib or jupyter qtconsole.
code is:
`from astropy.io import fits
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
img_file = 'rxcj0920.1+8602.fits' # 1kx1k
hdu_list = fits.open(img_file)
img_data = hdu_list[0].data
plt.imshow(img_data, cmap='gist_heat', origin='lower', norm=LogNorm(), vmin=400, vmax= 65e3), plt.colorbar()`
Does
import matplotlib
matplotlib.rcParams['figure.figsize'] = (10.,10.)
have any effect?

remove white spaces in images subplot in python and save it [duplicate]

This question already has answers here:
Reduce the gap between rows when using matplotlib subplot?
(3 answers)
Closed 5 years ago.
I want to create a subplot of spectrograms in python3, using the following code.
My problem is that I have white spaces between plots and
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
j=0
plt.clf()
f, axarr= plt.subplots(4,5, gridspec_kw = {'wspace':0, 'hspace':0})
f.tight_layout()
for i, ax in enumerate(f.axes):
j=j+1
im = ax.imshow(syllable_1.transpose(), vmin=0, vmax=syllable_1.max(),
cmap='pink_r')
plt.xticks([], [])
#ax[j].autoscale_view('tight')
#ax.set_xticklabels([])
#ax.set_yticklabels([])
#plt.subplots_adjust(left=0.1, right=0.85, top=0.85, bottom=0.1)
plt.subplots_adjust(wspace=0, hspace=0)
plt.savefig("myfig9.png", bbox_inches='tight')
the result is as follows:
could you please suggest me some solutions to solve it.
Thanks in advance
Just to lest you know, I add aspect='auto' to my plot code and it is solved. I used the following link. It seems that I did not use good keywords for search. Thanks

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()