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
Related
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()
Is there a way to use SymLogNorm with imshow, but make the colorbar basically stretch the colors so that the colorbar actually appears linear?
Below is a short code
from pylab import *
import numpy as np
from matplotlib.colors import SymLogNorm
data = np.random.uniform(low=-10, high=10, size=(10,10))
norm = SymLogNorm(2,vmin=-10,vmax=10)
fig, axes = plt.subplots()
im = axes.imshow(data,extent=[-10,10,-10,10],cmap=plt.cm.jet,norm=norm)
cb = fig.colorbar(im)
that produces this
I basically want this image, but want to stretch the colorbar so the ticks appear linear, not log.
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')
My code is:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
After plotting some points, when I use the plt.show() method then it displays a 3D axes system but there is only one octant. I need all 8 of them for my project. Is there any way to get them?
Thanks in advance.
It should put your data (presumably negative) in view when you plot it. However, it's worth knowing how to manually set the limits as well:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlim(-1,1)
ax.set_ylim(-1,1)
ax.set_zlim(-1,1)
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()