How can I make a scatter plot using proplot? (Python) - python

I just downloaded the ProPlot package and am wondering how I can make a scatter plot. I looked into this example, but as I tried
import proplot as plt
plt.figure(...)
plt.scatter(...)
it returns me AttributeError: module 'proplot' has no attribute 'scatter' . My plot worked when I did
import proplot as plt
import matplotlib.pyplot as plt
plt.figure(...)
plt.scatter(...)
And I did see the effect of proplot in this way. However, I don't think it's right to import two packages with the same notation plt. Is there a better way I can generate the scatter plot using proplot? Thanks!

Proplot does not offer a direct replacement for the scatter plot at the top level of the proplot module. The only way to construct it is to call the method of an axis object:
import proplot as pplt
fig, ax = pplt.subplots()
ax.scatter()
Or alternatively:
fig = pplt.figure()
ax = fig.subplot()
ax.scatter(...)
Note that if you import matplotlib after proplot under the same name plt alias, you won't be able to use the proplot interface under plt since it'll be replaced by matplotlib. It is not "wrong" to do so, since it is legal python, but surely not what you intended to accomplish.

Related

Statistical significance on matplotlib / seaborn graph?

I finished analyzing my data and want to show that they are statistically significant using the t-test_ind. However, I haven't found anything functional to show this other than what was referenced in (How does one insert statistical annotations (stars or p-values) into matplotlib / seaborn plots?):
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
from statannot import add_stat_annotation
ax = sns.barplot(x=x, y=y, order=order)
add_stat_annotation(ax, data=df, x=x, y=y,
boxPairList=[(order[0], order[1]), (order[0], order[2])],
test='t-test_ind',
textFormat='star',
loc='outside')
Using this approach however, whenever I try to save the plot using plt.savefig() the added significancies using the add_stat_annotation are discared (matplotlib does not seem to recognize the added annotations). Using the loc='inside' option messes up my plot so it isn't really an option.
I am therefore asking if there is some simpler way to add the sigificancies directly in matplotlib / seaborn or if you can plt.savefig() with enough border / padding to include everything.
It was mainly a xlabel cut off problem. So in future applications I would use the add_stat_annotation from webermarcolivier/statannot. To save your files use one of the following possibilities:
import matplotlib.pyplot as plt
plt.tight_layout() # Option 1
plt.autoscale() # Option 2
plt.savefig('filename.png', bbox_inches = "tight") # Option 3
Hope this will help someone for future use.

Matplotlib: how do I know which colormap is being used?

I played around with colormaps, trying many of them, trying to make my own, both in matplotlib and seaborn.
However now I would like to know which colormap I am using. How can I do that? Is there a command like matplotlib.whichColormap ?
Usually there would be no need to find out the colormap you are using because you define that yourself. I.e. when calling
plt.imshow(..., cmap="viridis")
you already know that you are using "viridis".
If you still feel it would be useful to get that information from an existing ScalarMappable, you may use get_cmap() and it's name attribute:
import matplotlib.pyplot as plt
import numpy as np
a = np.random.rand(4,5)
fig, ax = plt.subplots()
im = ax.imshow(a, cmap="viridis")
cm = im.get_cmap()
print(cm.name) # prints viridis

How modules know each other

I can plot data from a CSV file with the following code:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('test0.csv',delimiter='; ', engine='python')
df.plot(x='Column1', y='Column3')
plt.show()
But I don't understand one thing. How plt.show() knows about df? I'll make more sense to me seeing, somewhere, an expression like:
plt = something(df)
I have to mention I'm just learning Python.
Matplotlib has two "interfaces": a Matlab-style interface and an object-oriented interface.
Plotting with the Matlab-style interface looks like this:
import matplotlib.pyplot as plt
plt.plot(x, y)
plt.show()
The call to plt.plot implicitly creates a figure and an axes on which to draw.
The call to plt.show displays all figures.
Pandas is supporting the Matlab-style interface by implicitly creating a figure and axes for you when df.plot(x='Column1', y='Column3') is called.
Pandas can also use the more flexible object-oriented interface, in which case
your code would look like this:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('test0.csv',delimiter='; ', engine='python')
fig, ax = plt.subplots()
df.plot(ax=ax, x='Column1', y='Column3')
plt.show()
Here the axes, ax, is explicitly created and passed to df.plot, which then
calls ax.plot under the hood.
One case where the object-oriented interface is useful is when you wish to use
df.plot more than once while still drawing on the same axes:
fig, ax = plt.subplots()
df.plot(ax=ax, x='Column1', y='Column3')
df2.plot(ax=ax, x='Column2', y='Column4')
plt.show()
From the pandas docs on plotting:
The plot method on Series and DataFrame is just a simple wrapper
around :meth:plt.plot() <matplotlib.axes.Axes.plot>
So as is, the df.plot method is an highlevel call to plt.plot (using a wrapper), and thereafter, calling plt.show will simply:
display all figures and block until the figures have been closed
as it would with for all figures plotted with plt.plot.
Therefore, you don't see plt = something(df) as you would expect, because matpotlib.pyplot.plot is being called behind the scene by df.plot.
According to http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.show , the plt.show() itself doesn't know about the data, you need to pass the data as parameters.
What you are seeing should be the plot of pandas library, according to the usage http://pandas.pydata.org/pandas-docs/stable/visualization.html#basic-plotting-plot.
Hope this solves your question.

Writing astronomical units in the labels of plot

I use matplotlib to plot my data but I need to write in labels the unit and I it doesn't work the way it works in latex. For instance in latex by using siunitx package then I can write \arcsecond unit but how should it be done in matplotlib?
You can do it like this:
import pylab as plt
from matplotlib.ticker import FormatStrFormatter
x = range(10)
plt.plot(x)
plt.gca().xaxis.set_major_formatter(FormatStrFormatter('%d unit'))

Is there any way to ask Basemap not show the plot?

I am trying to use mpl_toolkits.basemap on python and everytime I use a function for plotting like drawcoastlines() or any other, the program automatically shows the plot on the screen.
My problem is that I am trying to use those programs later on an external server and it returns 'SystemExit: Unable to access the X Display, is $DISPLAY set properly?'
Is there any way I can avoid the plot to be shown when I use a Basemap function on it?
I just want to save it to a file so later I can read it externally.
My code is:
from mpl_toolkits.basemap import Basemap
import numpy as np
m = Basemap(projection='robin',lon_0=0)
m.drawcoastlines()
#m.fillcontinents(color='coral',lake_color='aqua')
# draw parallels and meridians.
m.drawparallels(np.arange(-90.,120.,10.))
m.drawmeridians(np.arange(0.,360.,60.))
Use the Agg backend, it doesn't require a graphical environment:
Do this at the very beginning of your script:
import matplotlib as mpl
mpl.use('Agg')
See also the FAQ on Generate images without having a window appear.
The easiest way is to put off the interactive mode of matplotlib.
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
#NOT SHOW
plt.ioff()
m = Basemap(projection='robin',lon_0=0)
m.drawcoastlines()
#m.fillcontinents(color='coral',lake_color='aqua')
# draw parallels and meridians.
m.drawparallels(np.arange(-90.,120.,10.))
m.drawmeridians(np.arange(0.,360.,60.))

Categories

Resources