Use seaborn and matplotlib defaults in same ipython notebook - matplotlib

I am trying to use both seaborn and matplotlib defaults to create plots in an ipython notebook, each plot with it's own default mpl or sns style. I have followed the instructions outlined in this question, and this one, however they don't quite do what I need.
%matplotlib inline
import matplotlib.pyplot as plt
plt.plot([1,2,3,4], [1,4,9,16])
import seaborn as sns
plt.plot([1,2,3,4], [1,4,9,16])
sns.reset_orig()
plt.plot([1,2,3,4], [1,4,9,16])
The last plot saves without the grey border, however the size is still different than the original. And the inline display is not the same as the original. Ideally I would like to be able to set the style on a per plot basis. Does anyone have any suggestions on how to achieve this?

Related

How to keep auto aspect ratio with PowerBI python custom visual matplotlib plot, when facecolor set to transparent?

I have a Python custom visual inside a PowerBI report. Normally, this automatically fills the available space - and set the aspect ratio accordingly. However, when I set a facecolor background color (say None for a transparent visual, but also any other color), then the visual does not fill the space anymore, just keep the default matplotlib aspect ratio. Any ideas on how to force the chart to autofill?
This renders correctly with autofill:
import matplotlib.pyplot as plt
plt.plot(dataset['Column1'],dataset['Column2'])
plt.show()
But this has the aspect ratio problem:
import matplotlib.pyplot as plt
plt.figure(facecolor='red')
plt.plot(dataset['Column1'],dataset['Column2'])
plt.show()

In a GTK application using matplotlib, how can I set the color that appears while the figure is being redrawn after a window resize?

I can embed a matplotlib figure in a GTK application by following some of the examples, like this one in the matplotlib documentation. However, when I resize the application window, there is a flicker of white as the figure is being redrawn.
This isn't that much of a problem if the figure I am going to show also has a white background. But if I am using a dark figure style (i.e. to match the appearance of the rest of my application), there is always a white flicker until the figure is drawn in the correct style. This is somewhat jarring if the rest of the application is using a darker theme.
Is there a way I can control the background that is shown by a matplotlib backend while the figure is being drawn?
If I could set the color that appears while drawing, there would still be a flickering effect of the data and axes of the plot being redrawn, but at least the background would stay the same color before and after the figure is drawn.
To illustrate the unwanted effect, if I add a dark plot style to the example linked above, each time I resize the window I can see the white flicker.
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from matplotlib.backends.backend_gtk3agg import (
FigureCanvasGTK3Agg as FigureCanvas)
from matplotlib.figure import Figure
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('dark_background') # use the dark style so flicker is more apparent
win = Gtk.Window()
win.connect("delete-event", Gtk.main_quit)
win.set_default_size(400, 300)
win.set_title("Embedding in GTK")
fig = Figure(figsize=(5, 4), dpi=100)
ax = fig.add_subplot()
t = np.arange(0.0, 3.0, 0.01)
s = np.sin(2*np.pi*t)
ax.plot(t, s)
sw = Gtk.ScrolledWindow()
win.add(sw)
# A scrolled window border goes outside the scrollbars and viewport
sw.set_border_width(10)
canvas = FigureCanvas(fig) # a Gtk.DrawingArea
canvas.set_size_request(800, 600)
sw.add(canvas)
win.show_all()
Gtk.main()
(Resizing the window to very small sizes doesn't cause the flickering, presumably because once the figure is drawn at the smallest size, it does not get redrawn, just clipped.)
I'm using GTK 3, matplotlib 3.4.3, and Python 3.8.10 on Ubuntu.

Jupyter not showing proper matplotlib plots and seaborn plots

I tried to do a box plot with salary and loan. However its not showing even though the code is correct. And I tried with matplotlib plots, it doesnt work as well.
Here is the code

matplotlib: box plot for each category

My pandas data frame has two columns: category and duration. And
I use the following code to make a box plot of all data points.
import matplotlib.pyplot as plt
plt.boxplot(df.duration)
plt.show()
However, if I want one box fore each category, how do I modify the above code? Thanks!
In addition to Wen's answer, which is spot on, you might want to check out the seaborn library. It was made to do this kind of plot.
Seaborn is a Python visualization library based on matplotlib. It
provides a high-level interface for drawing attractive statistical
graphics.
Check the documentation for boxplots
Draw a box plot to show distributions with respect to categories.
sns.boxplot(data=df, x='category', y='duration')
We can do it with pandas
#df=pd.DataFrame({'category':list('aacde'),'duration':[1,3,2,3,4]}) sample data
df.assign(index=df.groupby('category').cumcount()).pivot('index','category','duration').plot(kind='box')

Plots Frame (including axes) blackened in Qtconsole

When I launch qtconsole with the --colors=linux option and plot something the frame of the plot is blackened so I cannot see the axes because the qtconsole background is also black.
I used to launch this before without problem but have this problem after a recent update of pandas. I am not sure about what changed but I thought there might be a setting I can change to fix this anyway without worrying about what the update modified that broke this.
It looks like the axes are set to transparent by default (this was not happening before).
The following plots as desired, showing the white axes on black background:
import pandas as pd
import matplotlib.pylab as plt
fig = plt.figure()
fig.patch.set_alpha(1)
temp = pd.Series(range(100))
temp.plot()
I would also like to set this behavior as the default one. I have not been able to do that yet. This seemed like a good lead,
http://matplotlib.org/users/customizing.html
but I could not find an option for exactly that yet.
Any suggestion is welcome. Thank you.