How do you make ggplot plots of numpy arrays? - python

I know how to use ggplot for data frames, but is there a good way to make plots from numpy arrays directly? Or do I have to convert?

If you just want to plot things in a "ggplot-like style", you can use the matplotlib.style package:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import style
# use ggplot style sheet
style.use('ggplot')
plt.plot(np.random.randn(10))
Of course (as cel pointed out), with matplotlib it's still up to you to make sure your plots actually follow the conventions set out in Grammar of Graphics.

Related

Matplotlib shows nothing in Jupyter notebook

I am working with Matplotlib and came across with Object Oriented Method to create plots with Matplotlib. So I wrote the following code in Jupyter Notebook
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
x=np.linspace(0,5,11)
y=x**2
fig=plt.figure()
axes=fig.add_axes([0.1,0.1,0.8,0.8])
axes.plot(x,y)
But I don't get any plot after running it. I tried looking other answers but couldn't solve my problem. So, my question is how do Object Oriented Interface in Matplotlib actually works and how and why it is better than functional method of Matplotlib?
Thanks.

Python Heatmaps (Basic and Complex)

What's the best way to do a heatmap in python (2.7)? I've found the heatmap.py module, and I was wondering if people have any advice on using it, or if there are other packages that do a good job.
I'm dealing with pretty basic data, like xy = np.random.rand(1000,2) superimposed on an image.
Although there's another thing I want to try, which is doing a heatmap that's scaled to a different heatmap. E.g., I have
attempts = np.random.rand(5000,2)
successes = np.random.rand(500,2)
And I want a heatmap of the successes relative to the density of the attempts. Is this possible?
Seaborn is a pretty widely-used library for making nice-looking plots, and has a heatmap function. Seaborn uses matplotlib under the hood.
import numpy as np
import seaborn as sns
xy = np.random.rand(1000,2)
sns.heatmap(xy, yticklabels=100)
Regarding your second question, I'm not sure what you mean. But my advice would be to create a numpy array or pandas dataframe of "successes [scaled] relative to the density of the attempts", however you mean that, and then pass that scaled array or dataframe to sns.heatmap
You can plot very complex heatmap using python package PyComplexHeatmap: https://github.com/DingWB/PyComplexHeatmap
https://github.com/DingWB/PyComplexHeatmap/blob/main/examples.ipynb
The most basic heatmap you can get is an image plot:
import matplotlib.pyplot as plt
import numpy as np
xy = np.random.rand(100,2)
plt.imshow(xy, aspect="auto")
plt.colorbar()
plt.show()
Note that using more points than you have pixels to show the heatmap might not make too much sense.
There are of course also different methods to draw a heatmaps and you may go through the matplotlib example gallery and see which plot appeals most to you.

Matplotlib: different stacked bars?

I want to create a stacked bar plot with different amount of stacks for each bar. The general example for stacked bars works fine if my data are all homogenous, but I want something that rather looks like the shown example.
This turned out to be whole other level in Matplotlib (while still easy with some Excel-like tool, as you can see). Is there a convenient way of creating this kind of plot in Matplotlib? Thanks.
I guess you are working directly in matplotlib, but these days plotting data, especially for quick a view can be easily done with pandas, following your example we get:
import matplotlib.pyplot as plt
import matplotlib
matplotlib.style.use("ggplot")
import pandas as pd
import numpy as np
df = pd.DataFrame([pd.Series([10,20,40,10,np.nan]), pd.Series([20,10,30,10,10]), pd.Series([30,40, np.nan, np.nan, np.nan])], index=["Bar1", "Bar2", "Bar3"])
df.plot.bar(stacked=True)
plt.show()

Importing seaborn in python script messing up plot style

Attached below are two plots. The only difference in the script that produced them is that the second one had an additional line:
import seaborn as sns
I am not setting any seaborn style yet. Just importing seaborn is changing plot style though, even in plots not using seaborn. Is there any way I can import seaborn (to be used in other plots), and not have the style changed for plots that do not use it?
Check this
import seaborn.apionly as sns or from seaborn.apionly import lmplot
http://stanford.edu/~mwaskom/software/seaborn/whatsnew.html

Figure-specific vs general properties in matplotlib

I am trying to understand how methods and attributes are organized in matplotlib. For example, say I have a figure:
import matplotlib.pyplot as plt
my_fig = plt.imshow(image)
I have noticed that some figure properties are set via module methods, e.g.:
plt.axis('off')
while others are set for the figure itself using object methods:
my_fig.set_cmap('hot')
Can figure properties be specified in either way?
How can I turn off the axis by calling methods on my object my_fig?
The plt methods are part of the pyplot API, which is intended to provide Matlab-like convenience for interactive use (and certainly appears to be very influenced by Matlab). But it's just one small facet of the whole matplotlib API (which is much more OOP). In practice I seem to end up mixing them both myself in SW; it's largely a matter of taste whether you go through the pyplot API or access the objects. pyplot is certainly very convenient although as you want to do more complex/exotic things you'll find what you can do with pyplot alone limited and you'll need to get to know at least the full API's Axes, Figure, Legend and Path objects better.
Pyplot is a collection of command style functions that make matplotlib work like MATLAB, matplotlib.figure.Figure is part of the object-oriented API.
In most cases you can configure figure settings via itself like this:
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
image=mpimg.imread('stinkbug.png')
my_fig = plt.imshow(image)
my_fig.axes.axes.get_xaxis().set_visible(False)
my_fig.axes.axes.get_yaxis().set_visible(False)
plt.show()
enter code here
required stinkbug.png:
result:

Categories

Resources