Is it possible to create something like the picture? cause I just can't get it to work. Mine isn't showing shapes on the grid for some reason. Any help would be appreciated
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Rectangle
import matplotlib.patches as mpatches
# Data for plotting
fig, ax = plt.subplots(1,figsize=(8,7))
#use add_patch instead, it's more clear what you are doing
ax.plot([1,5,2],[2,3,4],color="cyan")
rect = Rectangle((5,5),6,6, fill= False)
plt.gca().add_patch(rect)
ax.add_patch(rect)
plt.ylim(1400,2300)
plt.xlim(34,48)
plt.xlabel("inches")
plt.ylabel("weight (lbs)")
plt.title('Váha a vyváženie lietadla Cessna 172N')
plt.legend(["Utility","Normal category"])
plt.grid()
plt.plot
plt.show()
Related
This code is from Basemap docs pages, with some changing to focus on my issue at hand.
I want to color(in this case, magenta) all lands(Antarctica can be overlooked), and all ocean should be white like Antarctica is right now. The shapefile is from here
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
from matplotlib.patches import PathPatch
import numpy as np
fig = plt.figure(figsize=(20,20))
ax = fig.add_subplot(111)
map = Basemap(llcrnrlon=-180,llcrnrlat=-85,urcrnrlon=180.,urcrnrlat=85, projection='merc')
map.readshapefile('countries_lakes/ne_10m_admin_0_countries_lakes', 'units')
patches = []
for info, shape in zip(map.units_info, map.units):
patches.append(Polygon(np.array(shape), True))
ax.add_collection(PatchCollection(patches, facecolor= 'm', edgecolor='k', linewidths=1.))
plt.savefig('dem.png', bbox_inches='tight')
This is probably ridiculously simple. All I want is to fill a set of Polygons with a hatch pattern, but the pattern never ever appears; I've played with parameters like fill and facecolor without success. The following is a minimal working example
from matplotlib.collections import PatchCollection, LineCollection
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
from matplotlib import rc
region1 = [(0,0),(1,0),(1,1),(0,1)]
region2 = [(1,1),(2,1),(2,2),(1,2)]
polygon1=plt.Polygon(region1,closed=True,fill=False,hatch='/')
polygon2=plt.Polygon(region2,closed=True,fill=False,hatch='\\')
patches = PatchCollection([polygon1,polygon2],match_original=True)
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111)
ax.add_collection(patches)
ax.relim()
ax.autoscale_view()
plt.show()
Any help is appreciated
I'd like to toggle the visibility of a subset of PolygonPatches in an Axes instance, after adding them as a PatchCollection, but I'm not sure if there's an efficient way to do that.
Is there a way of getting a subset of the patches from the Axes instance, then toggling their visibility?
That is sure possible. You can directly use PatchCollection.set_visible() to show and hide the PatchCollection.
Then, use a Button to toggle visibility.
import numpy as np
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
from matplotlib.widgets import Button
import matplotlib.pyplot as plt
patches = []
for i in range(3):
polygon = Polygon(np.random.rand(3, 2), True)
patches.append(polygon)
colors = 100*np.random.rand(len(patches))
p = PatchCollection(patches, alpha=0.4)
p.set_array(np.array(colors))
fig, ax = plt.subplots()
ax.add_collection(p)
bax = fig.add_axes([0.45,0.91,0.1,0.05])
button = Button(bax, "toggle")
def update(event):
p.set_visible(not p.get_visible())
fig.canvas.draw_idle()
button.on_clicked(update)
plt.show()
Is there any way to put contours labels in 3D plots? Clabel is apparently not implemented in 3D
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
x,y = np.meshgrid(np.linspace(-1,1,10),np.linspace(-1,1,10))
z=-(x**2+y**2)
fig,ax = plt.subplots()
C=ax.contour(x,y,z)
ax.clabel(C)
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
C=ax.contour(x,y,z,zdir='z',offset=-2)
ax.clabel(C)
As clabel is not implemented and the return value is None, there is indeed no point in calling clabel() for the time being. You can use the C.levels attribute to manually add labels to the graph.
It won't have the nice inline feature that hides the contour under the labels though.
I wanted to rotate a Rectangle in matplotlib but when I apply the transformation, the rectangle doesn't show anymore:
rect = mpl.patches.Rectangle((0.0120,0),0.1,1000)
t = mpl.transforms.Affine2D().rotate_deg(45)
rect.set_transform(t)
is this a known bug or do I make a mistake?
The patch in the provided code makes it hard to tell what's going on, so I've made a clear demonstration that I worked out from a matplotlib example:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib as mpl
fig = plt.figure()
ax = fig.add_subplot(111)
r1 = patches.Rectangle((0,0), 20, 40, color="blue", alpha=0.50)
r2 = patches.Rectangle((0,0), 20, 40, color="red", alpha=0.50)
t2 = mpl.transforms.Affine2D().rotate_deg(-45) + ax.transData
r2.set_transform(t2)
ax.add_patch(r1)
ax.add_patch(r2)
plt.xlim(-20, 60)
plt.ylim(-20, 60)
plt.grid(True)
plt.show()
Apparently the transforms on patches are composites of several transforms for dealing with scaling and the bounding box. Adding the transform to the existing plot transform seems to give something more like what you'd expect. Though it looks like there's still an offset to work out.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib as mpl
fig = plt.figure()
ax = fig.add_subplot(111)
rect = patches.Rectangle((0.0120,0),0.1,1000)
t_start = ax.transData
t = mpl.transforms.Affine2D().rotate_deg(-45)
t_end = t_start + t
rect.set_transform(t_end)
print repr(t_start)
print repr(t_end)
ax.add_patch(rect)
plt.show()