ipython notebook pylab inline - matplotlib.pyplot - how to display plot with scrollbar? - matplotlib

I am using ipython notebook with pylab --inline
import matplotlib.pyplot as plt
import pandas as pd
......
plt.figsize(14,8)
ax1=plt.subplot(311)
#data is a pandas data frame with timeseries stock data
# this plots the data
data.plot(ax=ax1)
This shows a plot of the stock data but its all displayed at once.
I would like to display just subrange of dates and have a scrollbar to control what range
How do I do it such that it works with the inline display of plots.

You can't. Inline display is a static PNG.
If you want something like that you will have to use a Javascript plotting library for now.
None that I know of will work out of the box with panda.

You can't manipulate plots in inline-mode after they have been drawn. However, you can scale,zoom and resize plots in the interactive mode. Just start your notebook without the inline-option and all plots will be generated in an extra window with the functionality you asked for:
ipython notebook --pylab

Related

A mysterious behavior of matplotlib - plot

I intended to plot a trend of daily BTC prices, which is recorded as a csv file, consisting of a sequence of 3892 numbers.
Being a csv file, I could use microsoft excel to plot the trend.
It looks like this;
Meanwhile, I attempted to do the same thing using pandas and matplotlib in a jupyter notebook, which doesn't look good;
What's wrong with my implementation?
One more thing is the implementing time ; it takes a quite a while, like one or two minute.
This is not the case when I put a code and implement like this ;
plt.plot(list(range(1000)),list(range(1000)))
Here is the full code for my attempt.
import pandas as pd
from matplotlib import pyplot as plt
df=pd.read_csv('BTC_inv_daily.csv')
Close=df['Close'].tolist()
N=len(Close)
plt.plot(list(range(N)),Close)
plt.show()

Matplotlib graph does not show in Python Interactive Window

The following is my code, but I can't get the plot to show on my Visual Studio Code even though I am running this on the Python Interactive Window, which should usually show a graph plot after running. The tables are showing just fine. I also do not get a default graph which pops up like it normally should. What am I doing wrong?
import yfinance as yf
import pandas as pd
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
import talib
df = pd.read_csv('filename.csv')
df = df.sort_index()
macd, macdsignal, macdhist = talib.MACD(df['Close'], fastperiod=12, slowperiod=26, signalperiod=9)
macd = macd.to_list()
macdsignal = macdsignal.to_list()
macdhist = macdhist.to_list()
df['macd'], df['macdsignal'], df['macdhist'] = macd,macdsignal,macdhist
ax = plt.gca()
print(df.columns)
df.plot(kind='line',x='Date',y='macd', color='blue',ax=ax)
df.plot(kind='line',x='Date',y='macdsignal', color='red', ax=ax)
plt.show()
The csv file has data that looks like this
The issue was with matplotlib.use('agg'), which does not support the show() function. This prevented the graph from being displayed on Visual Studio's Interactive Window. The matplotlib.use('agg') method can, however, be used for saving your graph in a .png format.
According to Matplotlib.org, agg is "the canonical renderer for user interfaces, which uses the Anti-Grain Geometry C++ library to make a raster (pixel) image of the figure". More information can be found at this link here

Visualize my matplotlib output

I run a several code and get
Draw = pf.plot_drawdown_periods(returns, top=5).set_xlabel('Date')
type(Draw)
<matplotlib.text.Text at 0x7fb063733f90>
How can make my result visible in different ways?
You need to explicitly call pyplot.show() to display the graphics.
import matplotlib.pyplot as plt
pf.plot_drawdown_periods(returns, top=5).set_xlabel('Date')
plt.show()

Change y-axis scaling fontsize in pandas dataframe.plot()

I am changing the font-sizes in my python pandas dataframe plot. The only part that I could not change is the scaling of y-axis values (see the figure below).
Could you please help me with that?
Added:
Here is the simplest code to reproduce my problem:
import pandas as pd
start = 10**12
finish = 1.1*10**12
y = np.linspace(start , finish)
pd.DataFrame(y).plot()
plt.tick_params(axis='x', labelsize=17)
plt.tick_params(axis='y', labelsize=17)
You will see that this result in the graph similar to above. No change in the scaling of the y-axis.
Ma
There are just so many features that you can control with the plotting capabilities of pandas, which leverages matplotlib. I found that seaborn is a lot easier to produce pretty charts, and you have a lot more control over the parameters of your plots.
This is not the most elegant solution, but it works; however, it has a seborn dependency:
%pylab inline
import pandas as pd
import seaborn as sns
import numpy as np
sns.set(style="darkgrid")
sns.set(font_scale=1.5)
start = 10**12
finish = 1.1*10**12
y = np.linspace(start , finish)
pd.DataFrame(y).plot()
plt.tick_params(axis='x', labelsize=17)
plt.tick_params(axis='y', labelsize=17)
I use Jupyter Notebook an that's why I use %pylab inline. The key element here is the use of
font_scale=1.5
Which you can set to whatver you want that produces your desired result. This is what I get:

X-axis labels on Seaborn Plots in Bokeh

I'm attempting to follow the violin plot example in bokeh, but am unable to add x-axis labels to my violins. According to the Seaborn documentation it looks like I should be able to add x-axis labels via the "names" argument, however, the following code does not add x-axis labels:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from bokeh import mpl
from bokeh.plotting import show
# generate some random data
data = 1 + np.random.randn(20, 6)
# Use Seaborn and Matplotlib normally
sns.violinplot(data, color="Set3", names=["kirk","spock","bones","scotty","uhura","sulu"])
plt.title("Seaborn violin plot in Bokeh")
# Convert to interactive Bokeh plot with one command
show(mpl.to_bokeh(name="violin"))
I believe that the issue is that I'm converting a figure from seaborn to matplotlib to bokeh, but I'm not sure at what level the x-axis labels go in.
I've confirmed that the labels are showing up in matplotlib before conversion to bokeh. I've also tried adding the labels to bokeh after conversion, but this results in a weird plot. I've created an issue for this problem with the bokeh developers here.
Since Bokeh 12.5 (April 2017), support for Matplotlib has been deprecated, so mpl.to_bokeh() is no longer available.