I executed the code below and no errors showed in the process, but plt.show() could not work and showed nothing in the end! I'm confused and want to know why......
from mpl_finance import candlestick_ohlc
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import quandl
df = quandl.get("EOD/AAPL", authtoken="fzTPb-TWywaPkbdAS1VF")
df['Date'] = df.index.map(mdates.date2num)
ohlc = df[['Date','Open','High','Low','Close']]
f1, ax = plt.subplots(figsize = (10,5))
candlestick_ohlc(ax, ohlc.values, width=.6, colorup='green', colordown='red')
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m'))
plt.show()
In jupyter notebook it makes sense to define a backend, e.g. %matplotlib inline or %matplotlib notebook.
When then running your code in a singe cell it should show you the plot.
If you need to run it in seperate cells, plt.show() will not know what to show from previous cells. So in that case, state the figure instead,
f1
to show the figure f1.
Related
I would like to know if the behavior of the following code is expected.
The first figure (Series) is saved as I would expect. The second (DataFrame) is not.
If this is not a bug, how can I achieve my (obvious) goal?
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
fig = plt.figure()
pd.Series(np.random.randn(100)).plot()
fig.savefig('c:\\temp\\plt_series.png')
fig = plt.figure()
pd.DataFrame(np.random.randn(100,2)).plot()
fig.savefig('c:\\temp\\plt_df.png')
After saving the figure, close the current plot using plt.close() to close the current figure, otherwise the old one is still active even if the next plot is being generated. You can also use plt.close('all') to be sure all open figures are closed.
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
%matplotlib notebook
plt.figure(figsize=(12, 6))
CasData.pivot(index='year', columns='CasualtyNumber', values='People').plot(kind='bar')
plt.title('Casualties per year')
plt.xlabel('Year', fontsize=5)
plt.ylabel('Number of Casualties')
plt.show()
My plot bar graph using matplotlib.pyplot isn't showing.
I don't know why but my bar graph isn't showing. I've tried different ways.
If someone could help me out please. I'd appreciate it. Thank you.
Remove the line %matplotlib notebook.
It is overriding the previous line (these two lines are setting the backend). inline returns static plots, notebook is used for interactivity.
You also do not need the plt.show() line. This is taken care of by the inline backend.
This answer explains more about the backends: https://stackoverflow.com/a/43028034/6709902
I'm not really sure about your code as it seems incomplete but if you're using pivot I assumed you're pulling the data from a ".csv" file.
import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas as pd
%matplotlib notebook
CasData = pd.read_csv('data.csv')
CasData.pivot_table(index='year', columns='CasualtyNumber', values='People').plot(kind='bar')
plt.title('Casualties per year')
plt.xlabel('Year',fontsize='5')
plt.ylabel('Number of Casualties')
plt.show()
You need to provide the data in order to plot something and I don't
see you providing any.
Emphasis : I am looking for a way to display multiple tables or charts from a cell.
I have the following code where I have a table and a chart (and a few more) that I would like to display all those visuals in the out put shell after running it.
I tried importing matplotlib and plt.show() but they don't seem to work.
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
tab = [[49.0,35.5,26.7,17.5], [46.7,34.0,26.0,17.1]]
tab = list(map(list, zip(*tab)))
tab_DataFrame = pd.DataFrame(tab, columns=["Mass(g)", "Volume(mL)"])
tab_DataFrame.head()
plt.show()
ax1 = tab_DataFrame.plot.line(x="Mass(g)", y="Volume(mL)",style='-o')
plt.show()
Thanks in advance.
I am plotting values that are of order 10^-8 and I would like my inline plot in jupyter to output the yaxis ticks in that (scientific) notation. I tried :
plt.gca().yaxis.set_major_formatter(FormatStrFormatter('%.1E'))
as well as
plt.gca().yaxis.get_major_formatter().set_powerlimits((0, -10))
and
plt.ticklabel_format(style='sci')
but nothing seems to work. What am I doing wrong? I have the following example:
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
import mpld3
mpld3.enable_notebook()
import matplotlib.ticker as mtick
a=5*10**-8
b=3*10**-8
x=np.arange(0,10,0.01)
y=a*x+b
plt.figure(figsize=(12,5))
plt.subplot(1,2,1)
plt.plot(x,y)
# plt.ticklabel_format(style='sci')
# plt.gca().yaxis.get_major_formatter().set_powerlimits((0, -10))
plt.gca().yaxis.set_major_formatter(mtick.FormatStrFormatter('%.0e'))
plt.show()
Any pointers would be helpful as I don't find anything on this, besides ticks format of an axis in matplotlib, enter link description here or Change x axes scale in matplotlib
NOTE: If I comment out the lines with import mpld3 and mpld3.enable_notebook() then it works but cannot interact with the plot... Is there some special treatment of matplotlib when plotting inline in jupyter?
Thanks!
You can use set_yticklabels to have a similar looking output.
ax = plt.gca()
ax.set_yticklabels(['10^-8','2*10^-8','3*10^-8','4*10^-8'])
I'm trying to get a bigger chart. However, the figure method from matplotlib does not seem to be working properly.
I get a message, which is not an error:
import pandas.io.data as web
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
...
plt.figure(figsize=(20,10))
df2['media']= df2['SPY']*.6 + df2['TLT']*.4
df2.plot()
plt.show()
What's wrong with my code?
You can skip the first plt.figure() and just use the argument figsize:
df2.plot(figsize=(20,10))
See docs.