Plot very small values with matplotlib in jupyter - python

I am trying to plot some extremely small values with matplotlib in jupyter notebook (on a macbook pro). However, regardless if I set the y-axis limits, all I get is a flat line. What I am after is something like the example (png) below with regard to y-axis notation. I also tried the same example outside of jupyter and I still get the same results. Here's the code suggested by Andrew Walker on my previous question:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize=(13,6))
ax = fig.add_subplot(111)
plt.hold(True)
xs = np.linspace(0, 1, 101)
ys = 1e-300 * np.exp(-(xs-0.5)**2/0.01)
ax.plot(xs, ys, marker='.')
Here's what I get:
And here's what I'm after:

The easiest thing to do is to just plot your values multiplied by 10^300, and then change the y-axis label:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize=(13,6))
ax = fig.add_subplot(111)
plt.hold(True)
xs = np.linspace(0, 1, 101)
ys = np.exp(-(xs-0.5)**2/0.01)
ax.plot(xs, ys, marker='.')
ax.set_ylabel(r'Value [x 10^{-300}]')

You can use the set_ylim method on your axes object to do what you need, simply change your code to this and it would do what you need:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize=(13,6))
ax = fig.add_subplot(111)
plt.hold(True)
xs = np.linspace(0, 1, 101)
ys = 1e-300 * np.exp(-(xs-0.5)**2/0.01)
ax.set_ylim([0,10^-299])
ax.plot(xs, ys, marker='.')
you may like to check This link for more info on this subject.

Related

Filling subplot with colormap - Matplotlib LogNorm does work in python 3 anymore

I had pretty nice plots looking like this created a while ago in python 2.7.
Now it appears that LogNorm does not work anymore.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
fig = plt.figure()
ax = fig.add_subplot(111)
# creating logspaced values for colorbar
x = np.logspace(-8,-3,6)
yarr = np.vstack((x,))
print(yarr)
# check if yarr is really logspaced
ax.plot(yarr, [1e1]*len(yarr), 'w.-')
# fill box with colorbar - this does not work anymore
ax.imshow(yarr, extent=(1e-8, 1e-3, 1, 1e4), norm=LogNorm(vmin=1e-8, vmax=1e-3))
ax.set_xscale("log")
ax.set_yscale("log")
Output now
Thanks in advance.
It was pointed out to me that it is a problem of matplotlib:
https://github.com/matplotlib/matplotlib/issues/7661/
import numpy as np
import matplotlib.pyplot as plt
tmp = np.arange(199).reshape(1, 199)
y = np.logspace(0, -4, 2)
x = np.logspace(-8, -3, 200)
fig, ax = plt.subplots()
ax.set_xscale('log')
ax.set_yscale('log')
ax.pcolormesh(x, y, tmp)
plt.show()
This solves the problem.

Does matplotlib change np.int to float automatically in x-axis?

I wonder why matplotlib changes the np.int to float in drawing especially in x-axis. Is that normal behavior of matplotlib or there might be something wrong in my jupyter status?
import numpy as np
import matplotlib.pyplot as plt
ys = np.arange(4)
print(ys)
fig, ax = plt.subplots(1,1, figsize=(16,5))
xs = np.arange(1,5,dtype=np.int)
print(xs)
ax.plot(xs, ys, marker='o')
ax.set_xlabel("dtype = int", fontsize=15)
ax.set_ylabel("y-values", fontsize=15)
plt.show()
It's just the tick locator, you can set it to integers only. See here for details:
import matplotlib as mpl
ax.xaxis.set_major_locator(mpl.ticker.MaxNLocator(integer=True))

plt.show() does not show the 3d scatter image

community,
I tried to create the 3d scatter by using matplotlib Axes3D on jupyter notebook.
However, it is not showing the image once I execute 'plt.show()'.
#pip install matplotlib
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
%matplotlib inline
fig = plt.figure()
ax =fig.add_subplot(111, projection = '3d')
x = dframe['CTR']
y = dframe['Clicks']
z = dframe['Avg. CPC']
ax.scatter(x, y, z, c='r', marker='o')
plt.show()
Your code works fine like this:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
# dummy data (your actual data should go here)
x = [1, 2, 3, 4]
y = x
z = x
ax.scatter(x, y, z, c="r", marker="o")
plt.show()
This shows:
May be something is wrong with your data. Also, since you are using plt.show() anyway, you can remove the %matplotlib inline line.

add_subplot() not working if fig = plt.figure() is in another cell in Ipython Notebook

I ran into a weird problem using matplotlib in Ipython Notebook. Here is the code:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax1.plot(np.random.randn(10), 'k--')
ax2 = fig.add_subplot(212)
ax2.plot(np.random.randn(10), 'r--')
This works fine and generates an inline figure with two subplots. However, if I put the same code into two cells like this:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax1.plot(np.random.randn(10), 'k--')
ax2 = fig.add_subplot(212)
ax2.plot(np.random.randn(10), 'r--')
Then there is no inline images generated at all.
By default, the inline backend closes a figure after a cell has been fully executed.
You're best approach is to merge those cells.

How to make a 3D scatter plot in matplotlib

I am currently have a nx3 matrix array. I want plot the three columns as three axis's.
How can I do that?
I have googled and people suggested using Matlab, but I am really having a hard time with understanding it. I also need it be a scatter plot.
Can someone teach me?
You can use matplotlib for this. matplotlib has a mplot3d module that will do exactly what you want.
import matplotlib.pyplot as plt
import random
fig = plt.figure(figsize=(12, 12))
ax = fig.add_subplot(projection='3d')
sequence_containing_x_vals = list(range(0, 100))
sequence_containing_y_vals = list(range(0, 100))
sequence_containing_z_vals = list(range(0, 100))
random.shuffle(sequence_containing_x_vals)
random.shuffle(sequence_containing_y_vals)
random.shuffle(sequence_containing_z_vals)
ax.scatter(sequence_containing_x_vals, sequence_containing_y_vals, sequence_containing_z_vals)
plt.show()
The code above generates a figure like:
Use the following code it worked for me:
# Create the figure
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Generate the values
x_vals = X_iso[:, 0:1]
y_vals = X_iso[:, 1:2]
z_vals = X_iso[:, 2:3]
# Plot the values
ax.scatter(x_vals, y_vals, z_vals, c = 'b', marker='o')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_zlabel('Z-axis')
plt.show()
while X_iso is my 3-D array and for X_vals, Y_vals, Z_vals I copied/used 1 column/axis from that array and assigned to those variables/arrays respectively.
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = plt.axes(projection='3d')
scatter plot
zdata = 15 * np.random.random(100)
xdata = np.sin(zdata) + 0.1 * np.random.randn(100)
ydata = np.cos(zdata) + 0.1 * np.random.randn(100)
ax.scatter3D(xdata, ydata, zdata);
Colab notebook
Using plotly - Easiest and most functional and nice plots
import plotly.express as px
df = px.data.iris()
fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width',
color='species')
fig.show()
https://plotly.com/python/3d-scatter-plots/

Categories