cannot show label in PyPlot called from Julia [duplicate] - matplotlib

This question already has an answer here:
matplotlib label doesn't work
(1 answer)
Closed 4 years ago.
I cannot render the label in PyPlot called from Julia. Does anyone know why?
using PyPlot
x = 0:0.1:10
y = x.^2
plot(x, y, label="label")
The above code renders only the plot without the label. I tried this both at Julia1.0 and Julia0.7, but the results were the same. The working environment is Ubuntu16.04, and I have already installed matplotlib for Python3 in my computer via pip.

As has been mentioned in the comments, to actually render a legend you have to call legend().
using PyPlot
x = 0:0.1:10
y = x.^2
plot(x, y, label="label")
legend()
This is not Julia specific but works the same way in Python.

Related

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 put legend outside the plotting area in Julia pyplot

I am trying to find a way to put the Julia pyplot backend legend outside of the plotting area. I found some posts discussing this but I can't find a consistent answer. For example I find this post: https://discourse.julialang.org/t/location-of-the-legend-in-pyplot/1311
but the problem is that it seems axes() is no longer available in pyplot.
P.S. User ImportanceOfBeingErnest asked me to put the code. The following code is from post that I put the link to:
x = 0:0.1:3*pi
y = sin(x)
z = cos(x)
ax = axes()
plot(x,y,label="sin(x)")
plot(x,z,label="cos(x)")
grid("on")
legend(bbox_to_anchor=[1.05,1],loc=2,borderaxespad=0)
ax[:set_position]([0.06,0.06,0.71,0.91])
And the error I get:
MethodError: no method matching axes()
Closest candidates are:
axes(!Matched::Core.SimpleVector) at essentials.jl:593
axes(!Matched::Core.SimpleVector, !Matched::Any) at essentials.jl:594
axes(!Matched::Base.Generator) at generator.jl:52
...
Stacktrace:
[1] top-level scope at In[3]:7
Assuming you are on Julia 1.1 you can use this code:
using PyPlot
x = 0:0.1:3*pi
y = sin.(x)
z = cos.(x)
ax = plt.axes()
plot(x,y,label="sin(x)")
plot(x,z,label="cos(x)")
grid(true)
legend(bbox_to_anchor=[1.05,1],loc=2,borderaxespad=0)
ax.set_position([0.06,0.06,0.71,0.91])

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

Hide matplotlib descriptions in jupyter notebook [duplicate]

This question already has answers here:
Disable the output of matplotlib pyplot
(5 answers)
Closed 6 years ago.
I am not sure what is the correct term for this, but here is what I see when I plot something:
The plots is actually what I want so see, but jupyter notebook also outputs some text: <matplotlib.axes._subplots.AxesSubplot at 0x1263354d0>, <matplotlib.figure.Figure at 0x1263353d0> which I am trying to get rid of.
After some searching, the only thing I was able to find is plt.ioff(), which didn't help me. Is there a way to get rid of the text?
You can finish the corresponding (matplotlib) line with a semicolon ;
This is a bit of a workaround, but it should work consistently:
1. Assign the plotting function to a variable (which could also be useful if you need to access some plot elements later on)
plt.figure(figsize=(3, 3))
plot = plt.plot(range(10),
[x*x for x in range(10)],
'o-')
2. Add a "pass" at the bottom of the cell (or an equivalent operation with no consequence)
plt.figure(figsize=(3, 3))
plt.plot(range(10),
[x*x for x in range(10)],
'o-')
pass
3. Add a semicolon at the end of the last statement
plt.figure(figsize=(3, 3))
plt.plot(range(10),
[x*x for x in range(10)],
'o-');

Gridlines in Julia PyPlot

I'm using the "PyPlot" package in Julia, and I want to add gridlines at specified locations. I'm not familiar enough with Python/Matlab to use their documentation pages to help - the commands differ in Julia. I want a basic plot, with gridlines on both axes at intervals of 1:
using PyPlot
fig=figure("Name")
grid("on")
scatter([1,2,3,4],[4,5,6,7])
Help appreciated...
PyPlot is just an interface to Matplotlib, so the commands
to customize the grid are Matplotlib's commands.
One way to configure the gridlines on both axes at intervals of 1 (for the given data) is:
using PyPlot
fig=figure(figsize=[6,3])
ax1=subplot(1,1,1) # creates a subplot with just one graphic
ax1[:xaxis][:set_ticks](collect(1:4)) # configure x ticks from 1 to 4
ax1[:yaxis][:set_ticks](collect(4:7)) # configure y ticks from 4 to 7
grid("on")
scatter([1,2,3,4],[4,5,6,7])
This code was tested inside an IJulia's notebook, and produces the following output:
Take a look at Various Julia plotting examples using PyPlot.
tested with Julia Version 0.4.3
The values where grid lines are drawn can be controlled by passing an array to the xticks() and yticks() functions.
A simple example:
using PyPlot
fig=figure("Name")
grid("on")
xticks(0:5)
yticks(3:8)
scatter([1,2,3,4],[4,5,6,7])
If you want it to be more flexible you can figure out the limits based on your data and set the tick interval to something else.
One little more dynamic way to configure the x-axis of the grid could be:
x_data = [1,2,3,4]
x_tick_interval = 2;
x_tick_start = minimum(xdata)
x_tick_end = maximum(xdata)
xticks(x_tick_start:x_tick_interval:x_tick_end)