How do you create line segments between two points? - python

I have this bit of code that plots out the points:
import matplotlib.pyplot as plot
from matplotlib import pyplot
all_data = [[1,10],[2,10],[3,10],[4,10],[5,10],[3,1],[3,2],[3,3],[3,4],[3,5]]
x = []
y = []
for i in xrange(len(all_data)):
x.append(all_data[i][0])
y.append(all_data[i][1])
plot.scatter(x,y)
pyplot.show()
but I want all the possible lines that could be made that looks something like this:
I've tried matplotlib path, but it doesn't work well for me.

This can be optimized but it works:
for point in all_data:
for point2 in all_data:
pyplot.plot([point[0], point2[0]], [point[1], point2[1]])

import matplotlib.pyplot as plt
import itertools
fig=plt.figure()
ax=fig.add_subplot(111)
all_data = [[1,10],[2,10],[3,10],[4,10],[5,10],[3,1],[3,2],[3,3],[3,4],[3,5]]
plt.plot(
*zip(*itertools.chain.from_iterable(itertools.combinations(all_data, 2))),
color = 'brown', marker = 'o')
plt.show()

One other way could be to use matplotlib patches
import matplotlib
import pylab as pl
fig, ax = pl.subplots()
import matplotlib.patches as patches
from matplotlib.path import Path
verts = [(x1,y1), (x2,y2)]
codes = [Path.MOVETO,Path.LINETO]
path = Path(verts, codes)
ax.add_patch(patches.PathPatch(path, color='green', lw=0.5))

using all combinations?
import matplotlib.pyplot as plot
from matplotlib import pyplot
all_data = [[1,10],[2,10],[3,10],[4,10],[5,10],[3,1],[3,2],[3,3],[3,4],[3,5]]
x = []
y = []
for i in combinations(all_data,2):
x.extend(i[0])
y.extend(i[1])
plot.plot(x,y)
pyplot.show()

Related

Changing number size on Color Map in Python

I'm sure this is probably easy and a simple fix, but I was having a lot of trouble with changing the number sizes on the colormap. Below is the code I have
from matplotlib import cm
import matplotlib.pyplot as plt
from matplotlib.colors import Normalize
cmap = cm.get_cmap('plasma')
fig = plt.figure(figsize=(25,25))
norm = Normalize(vmin=min(dz), vmax=max(dz))
colors = cmap(norm(dz))
sc = cm.ScalarMappable(cmap=cmap,norm=norm)
sc.set_array([])
plt.colorbar(sc)
I am aware on how to change them for the graph with
plt.xticks(size=20)
plt.yticks(size=20)
But just not for the colormap. If anyone can help I would appreciate it.Thank you.
You are looking for the labelsize argument of the tick_params method of the Axes associated to the colorbar. Have a look at the following example:
import matplotlib.pyplot as plt
from numpy.random import default_rng
data = default_rng(0).random((25, 100))
fig, ax = plt.subplots()
ctrf = ax.contourf(data)
cbar = fig.colorbar(ctrf)
cbar.ax.tick_params(labelsize=16)
plt.show()

Add text next to vertical line in matplotlib

Here is my code:
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime
import numpy as np
fig, ax = plt.subplots(1,1)
sample_dates = np.array([datetime.datetime(2000,1,1), datetime.datetime(2001,1,1)])
sample_dates = mdates.date2num(sample_dates)
plt.vlines(x=sample_dates, ymin=0, ymax=10, color = 'r')
ax.xaxis.set_major_locator(mdates.YearLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d.%m.%y'))
plt.show()
It plots two red lines at certain dates on x-axis. Now I would like to add text to every line. Text should be parallel to the line. Where do I start?
You can use Matplotlib text function to draw text on the plots. It has a lot of parameters that can be set. See documentation and examples here.
Here is an example with some text parallel to the lines:
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime
import numpy as np
from matplotlib.pyplot import text
fig, ax = plt.subplots(1,1)
sample_dates = np.array([datetime.datetime(2000,1,1), datetime.datetime(2001,1,1)])
sample_dates = mdates.date2num(sample_dates)
plt.vlines(x=sample_dates, ymin=0, ymax=10, color = 'r')
ax.xaxis.set_major_locator(mdates.YearLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d.%m.%y'))
for i, x in enumerate(sample_dates):
text(x, 5, "entry %d" % i, rotation=90, verticalalignment='center')
plt.show()
Should look like this:

Matplotlib PatchCollection to Legend

Currently, I am trying to make my own custom legend handler by creating a proxy artist (?) patch using PatchCollections and then following http://matplotlib.org/users/legend_guide.html to make a custom handler.
However I am running into a roadblock in trying to implement this into the legend. The arguments for legend takes in patches, but not patchcollections.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import matplotlib.patches as mpatches
from matplotlib.path import Path
from matplotlib.collections import PatchCollection
fig = plt.figure()
ax = fig.add_subplot(111)
verts1 = [(0.,0.),(0.,1.),(1.,1.),(0.51,0.51),(0.,0.),(0.,0.),]
codes1 = [Path.MOVETO,Path.LINETO,Path.LINETO,Path.LINETO,Path.MOVETO,Path.CLOSEPOLY,]
path1 = Path(verts1,codes1)
patch1 = mpatches.PathPatch(path1,ls='dashed',ec='red',facecolor="none")
verts2 = [(0.49,0.49),(0.,0.),(1.,0.),(1.,1.),(0.5,0.5),(0.,0.),]
codes2 = [Path.MOVETO,Path.LINETO,Path.LINETO,Path.LINETO,Path.MOVETO,Path.CLOSEPOLY,]
path2 = Path(verts2,codes2)
patch2 = mpatches.PathPatch(path2,ls='solid',edgecolor='red', facecolor="none")
patch = PatchCollection([patch1,patch2],match_original=True)
ax.set_xlim(-2,2)
ax.set_ylim(-2,2)
ax.add_collection(patch)
The above is the code to visualise the handler. Basically a rectangle with the upper triangle as dashed lines and the lower as solid
Using,
plt.legend([patch],["hellocello"],loc='upper right')
Recreates the error. Is there a workaround?
From the example in this section, it looks like you need to define an object and express all coordinates in terms of the handlebox size,
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import matplotlib.patches as mpatches
from matplotlib.path import Path
from matplotlib.collections import PatchCollection
class AnyObject(object):
pass
class AnyObjectHandler(object):
def legend_artist(self, legend, orig_handle, fontsize, handlebox):
x0, y0 = handlebox.xdescent, handlebox.ydescent
width, height = handlebox.width, handlebox.height
hw = 0.5*width; hh = 0.5*height
verts1 = [(x0,y0),(x0,y0+height),(x0+width,y0+height),((x0+hw)*1.01,(y0+hh)*1.01),(x0,y0),(x0,y0),]
codes1 = [Path.MOVETO,Path.LINETO,Path.LINETO,Path.LINETO,Path.MOVETO,Path.CLOSEPOLY,]
path1 = Path(verts1,codes1)
patch1 = mpatches.PathPatch(path1,ls='dashed',ec='red',facecolor="none")
verts2 = [((x0+hw)*0.99,(y0+hh)*0.99),(x0,y0),(x0+width,y0),(x0+width,y0+height),(x0+hw,y0+hh),(x0,y0),]
codes2 = [Path.MOVETO,Path.LINETO,Path.LINETO,Path.LINETO,Path.MOVETO,Path.CLOSEPOLY,]
path2 = Path(verts2,codes2)
patch2 = mpatches.PathPatch(path2,ls='solid',edgecolor='red', facecolor="none")
patch = PatchCollection([patch1,patch2],match_original=True)
handlebox.add_artist(patch)
return patch
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlim(-2,2)
ax.set_ylim(-2,2)
plt.legend([AnyObject()], ['hellocello'],
handler_map={AnyObject: AnyObjectHandler()})
plt.show()
This seems to work okay with PatchCollection, at least for me on matplotlib version 1.4.3. The result looks like,

Drawing lines between two plots in Matplotlib

I am drawing two subplots with Matplotlib, essentially following :
subplot(211); imshow(a); scatter(..., ...)
subplot(212); imshow(b); scatter(..., ...)
Can I draw lines between those two subplots? How would I do that?
The solution from the other answers are suboptimal in many cases (as they would only work if no changes are made to the plot after calculating the points).
A better solution would use the specially designed ConnectionPatch:
import matplotlib.pyplot as plt
from matplotlib.patches import ConnectionPatch
import numpy as np
fig = plt.figure(figsize=(10,5))
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
x,y = np.random.rand(100),np.random.rand(100)
ax1.plot(x,y,'ko')
ax2.plot(x,y,'ko')
i = 10
xy = (x[i],y[i])
con = ConnectionPatch(xyA=xy, xyB=xy, coordsA="data", coordsB="data",
axesA=ax2, axesB=ax1, color="red")
ax2.add_artist(con)
ax1.plot(x[i],y[i],'ro',markersize=10)
ax2.plot(x[i],y[i],'ro',markersize=10)
plt.show()
You could use fig.line. It adds any line to your figure. Figure lines are higher level than axis lines, so you don't need any axis to draw it.
This example marks the same point on the two axes. It's necessary to be careful with the coordinate system, but the transform does all the hard work for you.
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
fig = plt.figure(figsize=(10,5))
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
x,y = np.random.rand(100),np.random.rand(100)
ax1.plot(x,y,'ko')
ax2.plot(x,y,'ko')
i = 10
transFigure = fig.transFigure.inverted()
coord1 = transFigure.transform(ax1.transData.transform([x[i],y[i]]))
coord2 = transFigure.transform(ax2.transData.transform([x[i],y[i]]))
line = matplotlib.lines.Line2D((coord1[0],coord2[0]),(coord1[1],coord2[1]),
transform=fig.transFigure)
fig.lines = line,
ax1.plot(x[i],y[i],'ro',markersize=20)
ax2.plot(x[i],y[i],'ro',markersize=20)
plt.show()
I'm not sure if this is exactly what you are looking for, but a simple trick to plot across subplots.
import matplotlib.pyplot as plt
import numpy as np
ax1=plt.figure(1).add_subplot(211)
ax2=plt.figure(1).add_subplot(212)
x_data=np.linspace(0,10,20)
ax1.plot(x_data, x_data**2,'o')
ax2.plot(x_data, x_data**3, 'o')
ax3 = plt.figure(1).add_subplot(111)
ax3.plot([5,5],[0,1],'--')
ax3.set_xlim([0,10])
ax3.axis("off")
plt.show()

How to set ticks on Fixed Position , matplotlib

Can anyone help me set the ticks on a fixed position using matplotlib? I've tried using FixedPosition as this tutorial describes:
ax = pl.gca()
ax.xaxis.set_major_locator(eval(locator))
http://scipy-lectures.github.io/intro/matplotlib/matplotlib.html#figures-subplots-axes-and-ticks
But when I try to run, it tells me that set_major_locator method does not exist.
A simple example would be very useful.
Thanks.
Just use ax.set_xticks(positions) or ax.set_yticks(positions).
For example:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.set_xticks([0.15, 0.68, 0.97])
ax.set_yticks([0.2, 0.55, 0.76])
plt.show()
import numpy as np
import matplotlib.ticker as ticker
import matplotlib.pyplot as plt
name_list = ('Omar', 'Serguey', 'Max', 'Zhou', 'Abidin')
value_list = np.random.randint(0, 99, size = len(name_list))
pos_list = np.arange(len(name_list))
ax = plt.axes()
ax.xaxis.set_major_locator(ticker.FixedLocator((pos_list)))
ax.xaxis.set_major_formatter(ticker.FixedFormatter((name_list)))
plt.bar(pos_list, value_list, color = '.75', align = 'center')
plt.show()

Categories