Using the Python WITH statement to create temporary variable - python-3.x

Suppose I have Pandas data. Any data. I import seaborn to make a colored version of the correlation between varibales. Instead of passing the correlation expression into the heatmap fuction, and instead of creating a one-time variable to store the correlation output, how can I use the with statement to create temporary variable that no longer existss after the heatmap is plotted?
Doesn't work
# Assume: season = sns, Data is heatmapable
with mypandas_df.correlation(method="pearson") as heatmap_input:
# possible other statements
sns.heatmap(heatmap_input)
# possible other statements
If this exissted, then after seaborn plots the map, heatmap_input no longer exists as a variable. I would like tat functionality.
Long way
# this could be temporary but is now global
tcbtbing = mypandas_df.correlation(method="pearson")
sns.heatmap(tcbtbing)
Compact way
sns.heatmap( mypandas_df.correlation(method="pearson") )
I'd like to use the with statement (or similar short) construction to avoid the Long Way and the Compact way, but leave room for other manipulations, such as to the plot itself.

You need to implement enter and exit for the class you want to use it.
see: Implementing use of 'with object() as f' in custom class in python

Related

Is it possible to query a (Python) Bokeh data source's or Pandas dataframe identifying name?

In the beginning of a bokeh plot configuration I define my dataset that is to be used for plotting:
source = ColumnDataSource(data=df_1hz_climb)
output_notebook()
Is it possible (like for adaptive title reasons) to get somehow the name of the given dataframe -in this example here it is 'df_1hz_climb'- ?
Something like
source.data.name
does not exist. (This leads to the AttributeError: 'PropertyValueColumnData' object has no attribute 'name').
And source.name is empty, resp. "None" as not beeing defined above.
As my question might be confusing I try it in a more general way. It might clarify my question:
I create a Pandas dataframe - in this example an empty one- with:
import pandas as pd
my_dataframe = pd.DataFrame()
print(my_dataframe)
Is it possible to call the identifyer of this specific dataframe, namely my_dataframe as a string?
Any hint for me?

Display seaborn plots at some point later in code

Let's say at some point in my code, I have following two graphs: i.e. graph_p_changes and graph_p_contrib
line_grapgh_p_changes = df_p_change[['year','interest accrued', 'trade debts', 'other financial assets']].melt('year', var_name='variables', value_name='p_changes')
graph_p_changes = sns.factorplot(x="year", y="p_changes", hue='variables', data=line_grapgh_p_changes, height=5, aspect=2)
graph_p_changes.set(xlabel='year', ylabel='percentage change in self value across the years')
line_grapgh_p_contrib = df_p_contrib[['year','interest accrued', 'trade debts', 'other financial assets']].melt('year', var_name='variables', value_name='p_changes')
graph_p_contrib = sns.factorplot(x="year", y="p_changes", hue='variables', data=line_grapgh_p_contrib, height=5, aspect=2)
graph_p_contrib.set(xlabel='year', ylabel='percentage chnage in contribution to total value')
Now at some point later in my code, I just want to display one of the above two graphs. But when I do plt.show(), it displays both of the above graphs in my jupyter notebook. How can I display only one graph at any point in my code.
You'll want to refer to the assigned variable for each plot and then add .fig after that to redisplay it in a Jupyter notebook cell.
Specifically, in your case you'd reference graph_p_changes.fig or graph_p_contrib.fig in a cell and execute that cell to see an individual plot again.
This is similar to how you can show Seaborn's ClusterGrids again, see here. Because the title of your question said 'seaborn plots', I'll add for sake of completeness, this doesn't hold for plots like Seaborn's line plot (lineplot) or bar plot (barplot) , that produce AxesSubplot objects. There you use .figure, for example ax.figure to recall most of the examples listed on Seaborn's lineplot documentation.
Example catplots with code
This is using example code from here and seaborn's catplot documentation (see below) to make two plots. If this code was in one cell and then that cell was run, you'd see two plots in the output below it.
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
titanic = sns.load_dataset("titanic")
exercise = sns.load_dataset("exercise")
g = sns.catplot("alive", col="deck",
col_wrap=3, data=titanic[titanic.deck.notnull()],
kind="count", height=2.5, aspect=.8)
another_plot = sns.catplot(x="time", y="pulse", hue="kind", data=exercise)
Later, each can be displayed again individually as output of other cells with g.fig or another_plot.fig, depending on which plot you want to show.
Additionaly, I'll suggest to improve your long-term code viability, you may want to move on to using catplot in your plotting calls as that is what factorplot is now called in seaborn. See here where it says "factorplot still exists and will pass its arguments through to catplot() with a warning. It may be removed eventually, but the transition will be as gradual as possible."
UPDATE:
OP commented that what was desired was code allowing interspersed stdout/stderr output with plots at precise points among that stream and not just at the end.
For some reason, Seaborn plots (even simple line plots) don't seem to get 'captured' correctly with io.capture_output(), and so I had to use the %%capture cell magic command in the producing cell and combine the output in a separate cell. However, Plotly plots I tried based on example code are captured by io.capture_output() and allow facile intermixing all in the same cell. This is all illustrated in an example notebook available here; it is best viewed in static form here at nbviewer because nbviewer renders the Plotly plots while GitHub doesn't. The top of that notebook includes a link where you can launch an active Jupyter session where it will run.
Update related to this UPDATE:
In an insightful answer to 'seaborn stop figure from being visualized', ffrosch suggests you "can temporarily disable/enable the inline creation with plt.ioff() and plt.ion()." This may offer yet another way to fine-tune when Seabor plots show among the output and/or offer another way to constrain ouput since %%capture cell magic worked yet io.capture_output() did not. (I have yet to try this.)

How to solve this data set

I am importing a Data set from quandl using API. Everything is perfect, however the time series I am importing is reversed. By this I mean if I used the .head method to print the first elements in the data set, I will get the latest Data set figures and printing the tail will get oldest figures
df = pd.read_csv("https://www.quandl.com/api/v3/datasets/CHRIS/CME_CD4.csv?api_key=H32H8imfVNVm9fcEX6kB",parse_dates=['Date'],index_col='Date')
df.head()
This should be a pretty easy fix if I understand. Credit to behzad.nouri on this answer Right way to reverse pandas.DataFrame?.
You just need to reverse the order of your dataframe using the line below.
df = df.reindex(index=df.index[::-1])

Python matplotlib pyplot module always draws on existing figure window when figure title is the same

I am a new python user but an experienced Matlab user. I am recently debugging a python script, and when I manually re-run the script multiple times, I found a somewhat annoying issue of matplotlib: it always draws on existing figure window, overlapping on existing plot, if the figure title is the same.
The script I am debugging looks like this:
import matplotlib.pyplot as plt
# Some calculations here
plt.figure('Results') # The script will only create one figure
# plot the data
# End of the script
A simple search on Google shows that if I don't explicitly specify figure title, or give each figure a different handle, matplotlib can create separate figure windows, and true, it works.
However, is there a way to create multiple figure windows with the same title, without giving them different handles (which in my case, I had to do it manually) in python? In Matlab it will always create separate figure window no matter what figure title you give it.
The argument to figure is an identifier. If it is left empty anew figure will be created, else the figure with that identifier will be activiated. The documentation makes this rather clear:
matplotlib.pyplot.figure(num=None, ...)
num : integer or string, optional, default: none
If not provided, a new figure will be created, and the figure number will be incremented. The figure objects holds this number in a number attribute. If num is provided, and a figure with this id already exists, make it active, and returns a reference to it. If this figure does not exists, create it and returns it. If num is a string, the window title will be set to this figure’s num.
Hence in order to create a new figure, leave this argument out or specify differing ones. In order to set the window's title, use set_window_title.
The following will create two figures with the same window title.
import matplotlib.pyplot as plt
plt.figure()
plt.gcf().canvas.set_window_title('Results')
plt.plot([1,2,3])
plt.figure()
plt.gcf().canvas.set_window_title('Results')
plt.plot([2,3,1], color="crimson")
plt.show()
From the first paragraph of your question, ...
when I manually re-run the script multiple times, I found a somewhat
annoying issue of matplotlib: it always draws on existing figure
window
I think that simply clearing the figure (at the start of the script) would make your repeated runs of the script useable.
import matplotlib.pyplot as plt
# compute results here - random here as a standin.
import numpy as np
x = np.random.randn(500)
plt.figure("Results"); plt.clf()
# plot results here...
plt.hist(x, bins=20, histtype='step')
Now, each time you run the script, you will draw the results on a blank canvas and not over the top of the old results.
The figures below illustrate the difference, after 3 runs of the script (in ipython): left - without the plt.clf(), and right - with plt.clf() at the start.

Using IronPython to set "Data Limit by Expression" on a visualization

How do I use IronPython to set the Data Limit By Expression field on a visualization?
(I mean an example of a simple IP script to set the Data Limit By Expression field on a visualization, I couldn't find one on the internet)
for any type of chart, if this is the only operation you need to do, you can use code like:
from Spotfire.Dxp.Application.Visuals import Visualization
viz = v.As[Visualization]()
print viz.Data.WhereClauseExpression # prints Python's nil value None
viz.Data.WhereClauseExpression = "[Column] = 'Value'"
print viz.Data.WhereClauseExpression # prints the above expression
in this example, v is a parameter pointing to the desired visualization. you could also look it up by name or ID or some other method.
if you're already manipulating this visualization with a script and just want to add a data limit, you can add this to your existing script without importing the Visualization class. every visualization type's Data object has this WhereClauseExpression property

Resources