3D plotting of points - python-3.x

I want to use python to plot some specific points in 3D given their coordinates. I want to use the matplotlib library but I'm not sure if there's an easy way of doing this.
Let's say I want to plot the following points:
(1,0,0)
(2,2,2)
(-1,2,0)
(1,2,1)

Since some of the examples around are overly complicated, a minimal example for a 3D scatter plot in matplotlib would look like this:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig, ax = plt.subplots(subplot_kw=dict(projection='3d') )
points = [(1,0,0), (2,2,2), (-1,2,0), (1,2,1)]
x,y,z = zip(*points)
ax.scatter(x,y,z, s=100)
plt.show()

Related

Python Plotly Scatter Plot on 3D Mesh

I have an STL(mesh) file that I want to render on plotly. I found this library which is using
import plotly.graph_objects as go
the go object of plotly.
and I want to make a scatter plot on the same graph and not sure how to do that.
At the end the problem is using the plotly graph_object with ploty express object together (at least this is the solution that I think can be done. ) If you know how to render a mesh and render some points on it it would be great.
Thank you
You need to add the traces to a single Figure.
For example:
import plotly.graph_objects as go
import numpy as np
pts = np.loadtxt(np.DataSource().open('https://raw.githubusercontent.com/plotly/datasets/master/mesh_dataset.txt'))
x, y, z = pts.T
mesh = go.Mesh3d(x=x, y=y, z=z, color='lightpink', opacity=0.50)
scatter = go.Scatter3d(x=x, y=y, z=z, mode='markers')
fig = go.Figure(data=[mesh, scatter])
fig.show()

Plotting d orbital diagrams using matplotlib (or seaborn)

guys, I'm a chemist and I've finished an experiment that gave me the energies of a metal d orbitals.
It is relatively easy to get the correct proportion of energies in Excel 1 and use a drawing program like Inkscape to draw the diagram for molecular orbitals (like I did with this one below 2) but I’d love to use python to get a beautiful diagram that considers the energies of my orbitals like we see in the books.
My first attempt using seaborn and swarmplot is obviously too far from the correct approach and maybe (probably!) is not the correct way to get there. I'd be more than happy to achieve something like the right side here in 3.
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
Energies = [-0.40008, -0.39583, -0.38466, -0.23478, -0.21239]
orbitals = ["dz2", "dxy", "dyz", "dx2y2", "dxz"]
df = pd.DataFrame(Energies)
df["Orbitals"] = pd.DataFrame(orbitals)
sns.swarmplot(y=df[0], size=16)
Thanks for any help.
1 The excel one
2 Drawn by hand using the excel version as the model
3 Extracted from literature
You can draw anything you like deriving from basic shapes and functions in matplotlib. Energy levels could be simple markers, the texts can be produced by annotate.
import numpy as np
import matplotlib.pyplot as plt
Energies = [-0.40008, -0.39583, -0.38466, -0.23478, -0.21239]
orbitals = ["$d_{z^2}$", "$d_{xy}$", "$d_{yz}$", "$d_{x^2 - y^2}$", "$d_{xz}$"]
x = np.arange(len(Energies))
fig, ax = plt.subplots()
ax.scatter(x, Energies, s=1444, marker="_", linewidth=3, zorder=3)
ax.grid(axis='y')
for xi,yi,tx in zip(x,Energies,orbitals):
ax.annotate(tx, xy=(xi,yi), xytext=(0,-4), size=18,
ha="center", va="top", textcoords="offset points")
ax.margins(0.2)
plt.show()

How to plot legend with diff color correspond to diff value in matplotlib python?

I am trying to plot subarea of landuse dataset, I want to get a legend like in the picture. I am using plt.imshow() to plot img, the input is 2D array, the value of array is land use code.
How can I get a legend like this:
You can add proxy artists to a plot as shown in the matplotlib legend guide.
Example code from the guide:
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
red_patch = mpatches.Patch(color='red', label='The red data')
plt.legend(handles=[red_patch])
plt.show()
which gives
Not sure how to get a legend title though.

How to make a graph using matplotlib with equally spaced powers of 10? [duplicate]

I want to plot a graph with one logarithmic axis using matplotlib.
I've been reading the docs, but can't figure out the syntax. I know that it's probably something simple like 'scale=linear' in the plot arguments, but I can't seem to get it right
Sample program:
import pylab
import matplotlib.pyplot as plt
a = [pow(10, i) for i in range(10)]
fig = plt.figure()
ax = fig.add_subplot(2, 1, 1)
line, = ax.plot(a, color='blue', lw=2)
pylab.show()
You can use the Axes.set_yscale method. That allows you to change the scale after the Axes object is created. That would also allow you to build a control to let the user pick the scale if you needed to.
The relevant line to add is:
ax.set_yscale('log')
You can use 'linear' to switch back to a linear scale. Here's what your code would look like:
import pylab
import matplotlib.pyplot as plt
a = [pow(10, i) for i in range(10)]
fig = plt.figure()
ax = fig.add_subplot(2, 1, 1)
line, = ax.plot(a, color='blue', lw=2)
ax.set_yscale('log')
pylab.show()
First of all, it's not very tidy to mix pylab and pyplot code. What's more, pyplot style is preferred over using pylab.
Here is a slightly cleaned up code, using only pyplot functions:
from matplotlib import pyplot
a = [ pow(10,i) for i in range(10) ]
pyplot.subplot(2,1,1)
pyplot.plot(a, color='blue', lw=2)
pyplot.yscale('log')
pyplot.show()
The relevant function is pyplot.yscale(). If you use the object-oriented version, replace it by the method Axes.set_yscale(). Remember that you can also change the scale of X axis, using pyplot.xscale() (or Axes.set_xscale()).
Check my question What is the difference between ‘log’ and ‘symlog’? to see a few examples of the graph scales that matplotlib offers.
if you want to change the base of logarithm, just add:
plt.yscale('log',base=2)
Before Matplotlib 3.3, you would have to use basex/basey as the bases of log
You simply need to use semilogy instead of plot:
from pylab import *
import matplotlib.pyplot as pyplot
a = [ pow(10,i) for i in range(10) ]
fig = pyplot.figure()
ax = fig.add_subplot(2,1,1)
line, = ax.semilogy(a, color='blue', lw=2)
show()
I know this is slightly off-topic, since some comments mentioned the ax.set_yscale('log') to be "nicest" solution I thought a rebuttal could be due. I would not recommend using ax.set_yscale('log') for histograms and bar plots. In my version (0.99.1.1) i run into some rendering problems - not sure how general this issue is. However both bar and hist has optional arguments to set the y-scale to log, which work fine.
references:
http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.bar
http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.hist
So if you are simply using the unsophisticated API, like I often am (I use it in ipython a lot), then this is simply
yscale('log')
plot(...)
Hope this helps someone looking for a simple answer! :).

Adding pie chart at given coordinates to cartopy projection

I am a beginner in data visualization, and even more with cartopy, I know for most of people my question would be obvious. I am trying to get familiar with cartopy and I successfully plot text and point. But I couldn't achieve it for pie chart.
I just want to plot pie chart on a particular projection. But I am really confuse, despite the documentation of cartopy. I have first try this:
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
ax = plt.axes(projection=ccrs.Robinson())
ax.coastlines(resolution='110m') # 110, 50, 10
ax.stock_img()
lat, long = 30, 30 # the latitude longitude
ax.pie(long, lat, [0.25, 0.75], transform=ccrs.PlateCarree())
That do not work, So I have checked and I found this Cartopy coastlines hidden by inset_axes use of Axes.pie but I do not understand what happend under the hood and furthermore it seems limited to PlateCarre(). I have try to modified it but I do not managed to get it work properly.
So my very simple question is How can I add several pie chart to a specific projection given latitude and longitude? If you can develop your answer you will be really welcome.
You can use an inset_axes to place a new axes into the plot, which will allow to host the pie chart. The position of the inset_axes is determined by the bbox_to_anchor argument. To have this argument use the coordinates of the projection of the cartopy axes (ax), you need to set the bbox_transform=ax.transData.
If you have your coordinates in a different coordinate system, you need to convert them to the one in use using the projection's .transform_point method first.
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
ax = plt.axes(projection=ccrs.Robinson())
ax.coastlines(resolution='110m')
ax.stock_img()
def plot_pie_inset(data,ilon,ilat,ax,width):
ax_sub= inset_axes(ax, width=width, height=width, loc=10,
bbox_to_anchor=(ilon, ilat),
bbox_transform=ax.transData,
borderpad=0)
wedges,texts= ax_sub.pie(data)
ax_sub.set_aspect("equal")
lon,lat = 90,30
lonr,latr = ccrs.Robinson().transform_point(lon,lat, ccrs.PlateCarree())
plot_pie_inset([0.25, 0.75],lonr,latr,ax,0.5)
plt.show()

Resources