My interactive graphs using bokeh are not interactive - python

I was making a course on Datacamp about Interactive Graphs with Bokeh. So I tried to do the same in Jupyter Notebook. I put the link of my code here:
https://github.com/Kkkihon/Interactive-Data-Visualizations/blob/d014755354aaaa2a1e382373c519a18c3e1c7d42/Graphs
My problems are:
On the THIRD GRAPH my legend is just "region" but i want to change it to show all the countries of my data list in the column "region"
On the FOURTH GRAPH when I move the Slider the title of the graph should change and also the dots on the graph but it doesn't happen.
On the SIXTH GRAPH my graph should change the values on the axis but my values remains on life vs fertility.
I'm clearly new to using python language and I don't want someone to do everything for me either. I want to understand why I have these problems and correct myself.

Related

Click on a matplotlib figure and show variables

I have a question about matplotlib. I want to be able to click on a cell and show the value of the variable in that cell
In the figure below, I can display the E (x) and N (y) coordinates but I can't display the water level (z).
I tried looking into backends and interactive mode but that isn't really what I need. At the moment I am using matplotlib q5. I am relatively new to python so any advice on how I can click on a figure to show values (kind of like you can in excel) would be great.
thanks!
Emma

How can I arrange two faceted side-by-side charts horizontally in Altair?

Altair offers lovely feature to facet charts using facet method. For example, following dataset visualizes nicely:
print(df[['Year', 'Profile', 'Saison', 'Pos']].to_csv())
,Year,Profile,Saison,Pos
0,2017,6.0,Sommer,VL
1,2017,6.0,Winter,VL
13,2017,6.0,Winter,HL
12,2017,6.0,Sommer,HL
18,2017,6.0,Sommer,HR
6,2017,6.0,Sommer,VR
7,2017,6.0,Winter,VR
19,2017,6.0,Winter,HR
14,2018,5.5,Winter,HL
8,2018,5.5,Winter,VR
15,2018,5.5,Sommer,HL
20,2018,4.3,Winter,HR
21,2018,5.0,Sommer,HR
3,2018,5.5,Sommer,VL
2,2018,6.2,Winter,VL
9,2018,4.5,Sommer,VR
17,2019,4.5,Sommer,HL
11,2019,4.2,Sommer,VR
22,2019,3.5,Winter,HR
10,2019,5.28,Winter,VR
5,2019,4.6,Sommer,VL
4,2019,4.9,Winter,VL
16,2019,4.0,Winter,HL
23,2019,4.5,Sommer,HR
with the following command:
alt.Chart(df).mark_bar().encode(x='Year:O', y='Profile:Q').facet(row='Saison:N', column='Pos:N')
But, as you can seem I have still a lot of place horizontally and would like to use it by rearranging Winter plot right next to the Summer plot:
I understand that I already used column grid to facet over attribute Pos, but visually for me Winter and Sommer plots are two separate plots (just like here), which I'd like to place side by side.
I tried to create two different charts in the same cell and using html emit them side by side, but in Jupyter environment there is a limitation on just one Altair/Vega plot per cell.
Is there any method I can use to arrange these charts horizontally?
In Altair, there is no good way to do this, because faceted charts cannot be nested according to the Vega-Lite schema. However, the Vega-Lite renderer actually does handle this in some cases, despite it technically being disallowed by the schema.
So you can hack it by doing something like this:
chart = alt.Chart(df).mark_bar().encode(
x='Year:O',
y='Profile:Q'
).facet('Saison:N')
spec = alt.FacetChart(
data=df,
spec=chart,
facet=alt.Facet('Pos:N')
).to_json(validate=False)
print(spec)
The resulting spec can be pasted by hand into http://vega.github.io/editor to reveal this (vega editor link):
You'll even notice that the vega editor flags parts of the spec as invalid. This is admittedly not the most satisfying answer, but it sort of works.
Hopefully in the future the Vega-Lite schema will add actual support for nested facets, so they can be used more directly from Altair.

Plot Graphs in separated window

I have a long Jupyter notebook code and there is many cells, which are redrawing the actual graph plot. When I am running cells after changing their contents I need to check the plot, but I always need to scroll up and down. I would prefer to watch the plot changes in separated window (I am using two monitors), so I will change the cell content, run the cell, and then just turn my head and see the plot - without any scrolling. Is there a way how to do that? I know it can be done by Spyder, but I want to do it in Jupyter notebook, since I use a lot of notebook advantages, such as Latex notes and headings between cells. Thanks a lot for any advice!
It would be great if you could tell us how you print your graph (what library ?). Ipython provide magic command. For example, if you use matplotlib to plot some figures, just add %matplotlib qt on top of your cell to make the plots appear in a separate window.
See the list of magic command available here.

Show new matplotlib graph further down Jupyter notebook

I'm learning Matplotlib and using a Jupyter notebook to track each thing that I learn. However, I ran into a problem because I have multiple cells with matplotlib code. In one of my first cells, I run plt.show(), which outputs a plot beneath the cell. Further down the page, I have some other code which plots new points, resizes an axis, etc., then runs plt.show()....which works, but applies the changes to the original plot that was created after the first cell.
Is there any way to get a new plot window to display beneath whichever cell I am running?
(The reason I want to do this: The first cell might be an example showing how to plot a basic set of points. I want this to display its own simple plot. Further down the page, I resize axes and change the style of graph. However, when this plots, I want to see a separate plot, or maybe the same plot redone (as in, it can keep the original points I plotted -- no need to clear the whole thing) but with the new changes, beneath this more complex cell.)
UPDATE: Images.
In Image 1, I have run the first cell of code. The graph displays beneath the cell. Just as I want.
In this second image, I've now run the lower block of code (marked [3]). The changes, however, are applied to the plot sitting above it, because that's where it was originally created. But I'd like a new plot, or maybe not a clean new plot, but at least some way to make that plot display beneath cell [3] that I just ran.
In the comments, you mentioned that you're using the %matplotlib notebook magic, because it allows interactivity.
One option is to stop using interactivity.
As you found, you can turn off interactivity with plt.ioff(). You could also stop using %matplotlib notebook altogether and instead use %matplotlib inline (called at the top of the notebook). With %matplotlib inline, you don't need to call plt.show().
But you want to use interactivity.
So what you should do is define a new figure after you've plotted your first figure. To do this, call plt.figure() after the first plot, before the code for the second.

Bokeh line graph looping

I’ve been working on bokeh plots and I’m trying to plot a line graph taking values from a database. But the plot kind of traces back to the initial point and I don’t want that. I want a plot which starts at one point and stops at a certain point (and circle back). I’ve tried plotting it on other tools like SQLite browser and Excel and the plot seems ok which means I must be doing something wrong with the bokeh stuff and that the data points itself are not in error.
I’ve attached the images for reference and the line of code doing the line plot. Is there something I’ve missed?
>>> image = fig.line(“x”, “y”, color=color, source=something)
(Assume x and y are integer values and I’ve specified x and y ranges as DataRange1d(bounds=(0,None)))
Bokeh does not "auto-close" lines. You can see this is the case by looking at any number of examples in the docs and repository, but here is one in particular:
http://docs.bokeh.org/en/latest/docs/gallery/stocks.html
Bokeh's .line method will only "close up" if that is what is in the data (i.e., if the last point in the data is a repeat of the first point). I suggest you actually inspect the data values in source.data and I believe you will find this to be the case. Then the question is why is that the case and how to prevent it from doing that, but that is not really a Bokeh question.

Categories