I hope everyone's healthy and keeping safe.
I currently have the following plot. I want to ultimately plot a Gaussian distribution like this at x=0, y=0. The orange lines are basically the 95% confidence interval.:
Should I try swapping the axis and plotting? Is there a better way to do it? I am currently plotting in matplotlib and python. Are there better libraries to plot? Please let me know.
Thank you!
I have the following code:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF, ConstantKernel
noise = 1.0
X = np.arange(0, 1, 0.01).reshape(-1, 1)
kernel = ConstantKernel(10**2) * RBF(length_scale=0.35)
gp = GaussianProcessRegressor(kernel=kernel, alpha=noise**2, optimizer=None)
gp_mean, gp_std = gp.predict(X, return_std=True)
# Create the figure and the axes
fig, ax = plt.subplots()
ax.plot(X, gp_mean, 'k-', label='Zero-Mean GP')
ax.fill_between(X.ravel(), gp_mean + 1.96*gp_std, gp_mean - 1.96*gp_std, alpha=0.30, label='95% confidence interval')
ax.grid()
ax.legend(prop={'size': 12})
ax.set_xlim([-0.02, 1.0])
ax.set_ylim([-30.0, 30.0])
ax.tick_params(axis='both', labelsize=14)
ax.set_xlabel(r'$x$', fontsize=14)
plt.show()
You could draw the pdf of a gaussian normal on the y-axis as follows:
import numpy as np
from scipy import stats
from matplotlib import pyplot as plt
gp_mean = 0
gp_std = 12
gaussian = stats.norm(gp_mean, gp_std)
fig, ax = plt.subplots()
ys = np.linspace(*gaussian.ppf([0.001, 0.999]), 200)
ax.plot(gaussian.pdf(ys), ys, color='deepskyblue', label='gaussian normal')
ax.axhspan(*gaussian.ppf([0.05, 0.95]), color='chocolate', alpha=0.2, label='95% confidence interval')
ax.plot(0, gp_mean, marker='o', color='crimson', label='mean')
ax.set_xlim(0, 0.5)
ax.legend(prop={'size': 12})
plt.show()
PS: To also draw the pdf and mean at x = 0.5, you can add:
ax.plot(0.5 + gaussian.pdf(ys), ys, color='deepskyblue')
ax.plot(0.5, gp_mean, marker='o', color='crimson')
ax.set_xlim(0, 1)
Related
I would like to reproduce this plot in Python: (https://i.stack.imgur.com/6CRfn.png)
Any idea how to do this?
I tried to do a normal plt.scatter() but I can't draw this axes on the zero, for example.
That's a very general question... Using plt.scatter() is certainly a good option. Then just add the two lines to the plot (e.g. using axhline and axvline).
Slightly adapting this example:
import numpy as np
import matplotlib.pyplot as plt
# don't show right and top axis[![enter image description here][1]][1]
import matplotlib as mpl
mpl.rcParams['axes.spines.right'] = False
mpl.rcParams['axes.spines.top'] = False
# some random data
N = 50
x = np.random.randint(-10, high=11, size=N, dtype=int)
y = np.random.randint(-10, high=11, size=N, dtype=int)
colors = np.random.rand(N)
area = (30 * np.random.rand(N))**2 # 0 to 15 point radii
# creating a vertical and a horizontal line
plt.axvline(x=0, color='grey', alpha=0.75, linestyle='-')
plt.axhline(y=0, color='grey', alpha=0.75, linestyle='-')
# scatter plot
plt.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.show()
I want to plot several normal distributions, and add labels to each one in its line color. However the color does not seem to update.
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import scipy.stats as stats
def single_plot(mu, sigma, ax, label=None):
x = np.linspace(mu - 4*sigma, mu + 4*sigma, 1000)
ax = sns.lineplot(x, stats.norm.pdf(x, mu, sigma), ax=ax, label=label, zorder=2)
#my code to get color
color = ax.get_lines()[0].get_c() #fetch color of line
ax.text(mu, max(stats.norm.pdf(x, mu, sigma)), label, fontsize=16, color=color)
When put to use, however, this does not update the color with each line. If I try:
fig, ax = plt.subplots()
ax = single_plot(mu=1000, sigma=100, ax=ax, label='test1')
ax = single_plot(mu=1500, sigma=200, ax=ax, label='test2')
fig.show()
I am getting this figure. The label for "test2" was not updated.
I am wondering where I was wrong and how to fix this problem.
In matplotlib, how can I change the font size of a latex symbol?
I have the following code:
import matplotlib.pyplot as plt
import seaborn as sns
# get x and y from file
plt.plot(x, y, linestyle='--', marker='o', color='b')
plt.xlabel(r'$\alpha$ (distance weighted)', fontsize='large')
plt.ylabel('AUC')
plt.show()
But I get the following graph:
Notice that the $\alpha$ is still small.
To increase the size of the fonts set the desired value to fontsize. One way to mitigate the difference between the "normal" font and the "latex" one is by using \mathrm. The example below shows the behaviour of doing this:
import matplotlib.pyplot as plt
import seaborn as sns
x = np.arange(10)
y = np.random.rand(10)
fig = plt.figure(1, figsize=(10,10))
for i, j in zip(np.arange(4), [10,15,20,30]):
ax = fig.add_subplot(2,2,i+1)
ax.plot(x, y, linestyle='--', marker='o', color='b')
ax.set_xlabel(r'$\mathrm{\alpha \ (distance \ weighted)}$', fontsize=j)
ax.set_ylabel('AUC')
plt.show()
I'm a fan of the Seaborn package for making nice-looking plots using Matplotlib. But I can't seem to figure out how to show minor gridlines in my plots.
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sbn
x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)
fig, ax = plt.subplots(1, 1)
ax.scatter(x, y)
ax.grid(b=True, which='major')
ax.grid(b=True, which='minor')
gives:
Any thoughts here? Also any thoughts on how to adjust the style of the Seaborn gridlines that do show up...in particular, I'd love to make them narrower.
Wound up combining CT Zhu's answer with tcaswell's hint:
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sbn
x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)
fig, ax = plt.subplots(1, 1)
ax.scatter(x, y)
ax.get_xaxis().set_minor_locator(mpl.ticker.AutoMinorLocator())
ax.get_yaxis().set_minor_locator(mpl.ticker.AutoMinorLocator())
ax.grid(b=True, which='major', color='w', linewidth=1.0)
ax.grid(b=True, which='minor', color='w', linewidth=0.5)
That's because the minor ticks are not yet defined, so we need to add for example:
ax.set_xticks(np.arange(0,8)-0.5, minor=True)
ax.set_yticks([-1.25, -0.75, -0.25,0.24,0.75,1.25], minor=True)
In my code, I take the logarithm of two data series and plot them. I would like to change each tick value of the x-axis by raising it to the power of e (anti-log of natural logarithm).
In other words. I want to graph the logarithms of both series but have x-axis in levels.
Here is the code that I'm using.
from pylab import scatter
import pylab
import matplotlib.pyplot as plt
import pandas as pd
from pandas import Series, DataFrame
import numpy as np
file_name = '/Users/joedanger/Desktop/Python/scatter_python.csv'
data = DataFrame(pd.read_csv(file_name))
y = np.log(data['o_value'], dtype='float64')
x = np.log(data['time_diff_day'], dtype='float64')
fig = plt.figure()
plt.scatter(x, y, c='blue', alpha=0.05, edgecolors='none')
fig.suptitle('test title', fontsize=20)
plt.xlabel('time_diff_day', fontsize=18)
plt.ylabel('o_value', fontsize=16)
plt.xticks([-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4])
plt.grid(True)
pylab.show()
let matplotlib take the log for you:
fig = plt.figure()
ax = plt.gca()
ax.scatter(data['o_value'] ,data['time_diff_day'] , c='blue', alpha=0.05, edgecolors='none')
ax.set_yscale('log')
ax.set_xscale('log')
If you are using all the same size and color markers, it is faster to use plot
fig = plt.figure()
ax = plt.gca()
ax.plot(data['o_value'] ,data['time_diff_day'], 'o', c='blue', alpha=0.05, markeredgecolor='none')
ax.set_yscale('log')
ax.set_xscale('log')
The accepted answer is a bit out of date. At least pandas 0.25 natively supports log axes:
# logarithmic X
df.plot.scatter(..., logx=True)
# logarithmic Y
df.plot.scatter(..., logy=True)
# both
df.plot.scatter(..., loglog=True)