I'm trying to change the style of sns PairGrid graph. Namely, I want to add a frame around each of the grid graphs. What one graph looks like right now:
How I want it to look:
I've already spent a ton of time reading the documentation and searching the Internet, but I haven't found any suitable answer. Is it even possible with PairGrid?
Thank you all for your help.
Use the despine=False option of PairGrid:
import seaborn as sns
penguins = sns.load_dataset("penguins")
g = sns.PairGrid(penguins, despine=False)
g.map(sns.scatterplot)
Example:
Related
using Python I would like to plot a curve for the function y=cosh(x)*cos(5x) in my Jupyter Notebook.
In other words:
(cosine hyperbolicus of x) times (cosine of 5x)
How do I do this?
What do I need to import?
Thank you very much in advance.
Greetings
Specify the range of values for x that you need.
You can use Seaborn on top of Matplotlib to make it prettier, but this is optional:
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-5,5,0.1) # start,stop,step
y= (np.cosh(x))*(np.cos(5*x) )
# set a grey background (use sns.set_theme() if seaborn version 0.11.0 or above)
sns.set(style="darkgrid")
plt.plot(x,y)
plt.show()
You will need to import a plotting library and a maths library. The most commonly used plotting library is matplotlib, and for maths it's numpy. For plotting, bokeh is a an alternative to matplotlib, which I think is great because graphs are interactive by default. The disadvantage is that because it's not as widely used as matplotlib, you're less likely to find help on it in terms of StackOverflow answers and tutorials.
Anyway, to the code:
# Import the necessary packages and modules
import matplotlib.pyplot as plt
import numpy as np
# Set your x-range and calculate y
xmin = -2.5
xmax = 2.5
numPoints = 100
x = np.linspace(xmin, xmax, numPoints)
y = np.cosh(x)*np.cos(5*x)
# Plot -- it really can be this simple [1]
plt.plot(x,y)
Both of the graphing libraries above give you flexible options on where to place the axes, legends, titles, and so on. I recommend searching for beginner's tutorials on them to learn this stuff in depth.
[1] There are two ways to plot in matplotlib. What is shown here is the MATLAB-like interface. The other method is to use the object-based interface, which takes a bit more of getting used to, and requires a bit more boilerplate code, but that's what you will end up using once you demand more control over the appearance of your plots.
I recommend starting with the MATLAB-like commands first. The documentation has a good beginner's tutorial: https://matplotlib.org/stable/tutorials/introductory/pyplot.html
I've been trying to change the tick label of my axis. I've imported them from a csv, and have changed the label, but I can't change the tick. I'm just starting out, so if I have any of the terms wrong just let me know. My code is outlined below.
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv ('countries.csv')
uk = data[data.ISO_3_CODE == "GBR"]
plt.plot(uk.date_epicrv)
plt.plot(uk.CumCase)
plt.xlabel('Time')
plt.ylabel("Cumulative infection rate")
plt.show
The csv can be found at https://data.humdata.org/dataset/coronavirus-covid-19-cases-and-deaths if you need it. Sorry I couldn't be more specific, trying to learn a new skill and I believe I'm using the wrong terms to search.
Well, I feel like a fool. I was formatting my graph wrong.
Where I had
plt.plot(uk.date_epicrv)
plt.plot(uk.CumCase)
I should have used
plt.plot(uk.date_epicrv, uk.CumCase)
I was trying to plot both graphs on the x-axis. Closing the question now, hope this helps anyone with the issue down the line.
I have a dataset and I plotted using plotly python
The name of the models is shown as a legend on the right side which makes it a little difficult to interpret. Is there any way to write the model name just above the color circle represent on the graph similar to what shown below? Something similar in mathplotlib will also help me.
You need to add labels in you scatter statement.
import plotly.express as px
#df data
fig = px.scatter(df, x="latency", y="AP", text="type", size_max=60)
fig.update_traces(textposition='top center')
It's not clear to me why plotting is done like this:
import pandas as pd
import matplotlib.pyplot as plt
df.boxplot(column='initial_cost', by='Borough', rot=90)
plt.show()
How is the dataframe tied to plt.show()? I've done a few web searches and even took a look at the documentation(!) but couldn't find anything addressing this specifically.
I would expect something more like:
boxplot = df.boxplot(column='initial_cost', by='Borough', rot=90)
plt.show(boxplot)
Or even something like this:
boxplot = df.boxplot(column='initial_cost', by='Borough', rot=90)
boxplot.plt.show()
Matplotlib provides a MATLAB-like state-machine, the pyplot module, that takes care under the hood of instantiating and managing all the objects you need to draw a plot.
Pandas hooks into that in the same fashion. When you call it takes care of loading pyplot and creating a matplotlib Figure, Axes, several Line2D objects and everything that makes a boxplot.
When you call plt.show() it will track all the figures you created with the state-machine API, create a GUI with those figures and take care of displaying it.
If you need more control, you can of course do it all yourself with the object-oriented API. Create a figure, axes, manually draw the canvas, it's all there if needed.
As far as I've seen the common practice is a mix of both: hook into the object-oriented API when needed but still let pyplot take care of displaying or saving everything to a file.
I've used seaborn plots several times from an online course. Originally plotted graphs are so different as that of my computer's. Is this because of anything on code or in graphics?
Plot on my computer:
Original plot
Supposing the code being run is exactly the same, the reason would be that you are using a newer version of seaborn than the "online course".
In order to have your graphics appear in the same manner as in the online tutorial you may call
import seaborn as sns
sns.set()