I have just started experimenting with matplotlib, since I often come across instances where I need to plot some data, for which matplotlib seems an excellent tool. I attempted to adapt the ellipse example in the main site, so as to draw two circles instead, how ever after running the code, I find that none of the patches are displayed, I am not able to figure out what exactly is wrong... here is the code. Thanks in Advance.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import matplotlib.patches as mpatches
plt.axis([-3,3,-3,3])
ax = plt.axes([-3,3,-3,3])
# add a circle
art = mpatches.Circle([0,0], radius = 1, color = 'r', axes = ax)
ax.add_artist(art)
#add another circle
art = mpatches.Circle([0,0], radius = 0.1, color = 'b', axes = ax)
ax.add_artist(art)
print ax.patches
plt.show()
Which version of matplotlib are you using? I'm not able to replicate your results, I can see the two ellipsis quite well. I'm going by a long shot, but i guess you mean to do something like this:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import matplotlib.patches as mpatches
# create the figure and the axis in one shot
fig, ax = plt.subplots(1,figsize=(6,6))
art = mpatches.Circle([0,0], radius = 1, color = 'r')
#use add_patch instead, it's more clear what you are doing
ax.add_patch(art)
art = mpatches.Circle([0,0], radius = 0.1, color = 'b')
ax.add_patch(art)
print ax.patches
#set the limit of the axes to -3,3 both on x and y
ax.set_xlim(-3,3)
ax.set_ylim(-3,3)
plt.show()
Related
I'm trying to plot 3D data in 2D using orthographic projection. Here is partially what I'm looking for:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
fig = plt.figure(figsize=(10,10),facecolor='white')
axs = [fig.add_subplot(223)]
axs.append(fig.add_subplot(224))#,sharey=axs[0]))
axs.append(fig.add_subplot(221))#,sharex=axs[0]))
rng = np.random.default_rng(12345)
values = rng.random((100,3))-.5
values[:,1] = 1.6*values[:,1]
values[:,2] = .5*values[:,2]
for ax,axis in zip(axs,['y','x','z']):
axis1,axis2={'x':(1,2),'y':(0,2),'z':(0,1)}[axis]
ax.add_patch(plt.Circle([0,0], radius=.2, color='pink',zorder=-20))
ax.scatter(values[:,axis1],values[:,axis2])
axs[0].set_xlabel('x')
axs[2].set_ylabel('y')
axs[1].set_xlabel('y')
axs[0].set_ylabel('z')
fig.subplots_adjust(.08,.06,.99,.99,0,0)
plt.show()
There are some issues with this plot and the fixes I tried: I would need 'equal' aspect so that the circles are actually circle. I would also need the circles to be of the same size in each subplot. Finally, I would like the space to be optimized (i.e. with as little white space inside and between the subplots as possible).
I have tried sharing the axis between the subplots, then doing .axis('scaled') or .set_aspect('equal','box',share=True) for each axes, but the axis end up not being properly shared, and the circle in each subplot end up of different sizes. And while it crops the subplots to the data, it leaves a lot of space between the subplots. .axis('equal') or .set_aspect('equal','datalim',share=True) without axis shared leaves white space inside the subplots, and with shared axis, it leaves out some data.
Any way to make it work? And it would be perfect if it can work on matplotlib 3.4.3.
You can use a common xlim, ylim for your subplots and set your equal ratio with ax.set_aspect(aspect='equal', adjustable='datalim'):
See full code below:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
fig = plt.figure(figsize=(10,10),facecolor='white')
axs = [fig.add_subplot(223)]
axs.append(fig.add_subplot(224))#,sharey=axs[0]))
axs.append(fig.add_subplot(221))#,sharex=axs[0]))
rng = np.random.default_rng(12345)
values = rng.random((100,3))-.5
values[:,1] = 1.6*values[:,1]
values[:,2] = .5*values[:,2]
for ax,axis in zip(axs,['y','x','z']):
axis1,axis2={'x':(1,2),'y':(0,2),'z':(0,1)}[axis]
ax.add_patch(plt.Circle([0,0], radius=.2, color='pink',zorder=-20))
ax.scatter(values[:,axis1],values[:,axis2])
ax.set_xlim([np.amin(values),np.amax(values)])
ax.set_ylim([np.amin(values),np.amax(values)])
ax.set_aspect('equal', adjustable='datalim')
axs[0].set_xlabel('x')
axs[2].set_ylabel('y')
axs[1].set_xlabel('y')
axs[0].set_ylabel('z')
fig.subplots_adjust(.08,.06,.99,.99,0,0)
plt.show()
The output gives:
I made it work using gridspec (I changed scatter for plot to visually make sure no data gets left out). It requires some tweaking of the figsize to really minimize the white space within the axes. Thank you to #jylls for the intermediate solution.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
%matplotlib inline
rng = np.random.default_rng(12345)
values = rng.random((100,3))-.5
values[:,1] = 1.6*values[:,1]
values[:,2] = .5*values[:,2]
fig = plt.figure(figsize=(10,8),facecolor='white')
ranges = np.ptp(values,axis=0)
gs = GridSpec(2, 2, None,.08,.06,.99,.99,0,0, width_ratios=[ranges[0], ranges[1]], height_ratios=[ranges[1], ranges[2]])
axs = [fig.add_subplot(gs[2])]
axs.append(fig.add_subplot(gs[3]))#,sharey=axs[0]))
axs.append(fig.add_subplot(gs[0]))#,sharex=axs[0]))
for ax,axis in zip(axs,['y','x','z']):
axis1,axis2={'x':(1,2),'y':(0,2),'z':(0,1)}[axis]
ax.add_patch(plt.Circle([0,0], radius=.2, color='pink',zorder=-20))
ax.plot(values[:,axis1],values[:,axis2])
ax.set_aspect('equal', adjustable='datalim')
axs[0].set_xlabel('x')
axs[2].set_ylabel('y')
axs[1].set_xlabel('y')
axs[0].set_ylabel('z')
plt.show()
I would like to plot a matrix as image and a vector as a line in this image.
something like that:
I manage to do the code for the matrix image, but I'm not able to make the black line (here I did just
an example in powerpoint).
this is my code so far:
from mpl_toolkits.axes_grid1 import make_axes_locatable
from matplotlib.ticker import LogLocator
from matplotlib import rcParams
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
import numpy as np
rcParams['font.size']=35
x = np.arange(1,16,1)
y = np.arange(-50,0,1)
z = 100 * np.random.random_sample((15, 50))
line = np.linspace(0,100,50)
fig, ax = plt.subplots(figsize=(25,25))
divider = make_axes_locatable(ax)
cax = divider.append_axes('right', size='5%', pad=0.1)
im = ax.pcolor(x,y,z.T,norm=LogNorm(0.1, 100),cmap= 'jet')
cbar = fig.colorbar(im,cax=cax, orientation='vertical')
cbar.ax.yaxis.set_major_locator(LogLocator()) # <- Why? See above.
cbar.ax.set_ylabel('Resistividade \u03C1 [ohm.m]', rotation=270)
#ax2=ax.twinx()
#ax2.plot(line,y,'k--',linewidth=10)
ax.set_xlabel('Aquisição')
ax.set_ylabel('Profundidade [m]')
plt.savefig('mrec_1'+'.png',bbox_inches = "tight", format='png', dpi=300)
plt.show()
I have tried to use the ax.twinx() but since the order of magnetude is different the values on x-axis doesn' match.
Would someone help me please?
I tried this but it depends on the kind of data you have for the black line "track". I think you can just make an array of different (x,y) coordinates for the vertices (where the line changes direction + point of origin and point of end). Basing on what I saw in the image, you can add these two lines (is an approximation)
coordinates=np.array([[7,0],[7,-5],[9,-5],[9,-13], [7,-13],[7,-23],[13,-23],[13,-60]])
ax.plot(coordinates[:,0], coordinates[:,1], c='k', linewidth=10)
where every element of coordinates is the [x,y] couple. But if you have x and y from a well log, for example, you can just use those as arrays instead of coordinates[:,0] and coordinates[:,1]
As #BlueScr33n mentioned I used the twiny and works fine.
I'm sorry if this is expected behavior, but I wanted to understand what was going on here better. So if I try to draw a circle with plt.Circle and projection='3d'
import matplotlib.pyplot as plt
f2 = plt.figure()
ax1 = f2.add_subplot(1,1,1,projection='3d')
x = plt.Circle([1,1],radius=10)
ax1.add_artist(x)
ax1.set_xlim(0,100)
ax1.set_ylim(0,100)
ax1.set_zlim(0,100)
plt.show()
then I get the following bizarre outcome:
although it all works as expected if I just remove the projection='3d'. I would appreciate any context as to why this weird result happens, I guess I don't totally understand why projection='3d' would mangle things so much.
You need somewhere a projection from 2d to 3d to accommodate your circular artist. One way is to create a circle patch and map it to 3d as shown in here, which I have adapted to answer your question. You can choose zdir='x' to be zdir='y' or zdir='z' as per your need.
import matplotlib.pyplot as plt
from matplotlib.patches import Circle, PathPatch
from mpl_toolkits.mplot3d import Axes3D
import mpl_toolkits.mplot3d.art3d as art3d
f2 = plt.figure()
ax1 = f2.add_subplot(1,1,1,projection='3d')
circle = Circle((1, 1), 10, edgecolor='red', facecolor=None, fill=False)
ax1.add_patch(circle)
art3d.pathpatch_2d_to_3d(circle, z=0, zdir='x')
ax1.set_xlim3d(-20, 20)
ax1.set_ylim3d(-20, 20)
ax1.set_zlim3d(-20, 20)
I'm considerably new to python and making a map of a room. I've plotted the room, obstacles etc. And some points (which are sensors). Now I want make a 2-D cone which shows the area in which sensors see. I will have an angle and radius for the cone.
I've tried searching but mostly 3-D cones have been discussed here in previous questions. How the cone should look
Any guidance is appreciated
You would use matplotlib.patches.Wedge such as this example. Another example that I've reduced to the more relevant bits is:
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from matplotlib.collections import PatchCollection
import numpy as np
fig, ax = plt.subplots()
patches = []
wedge = mpatches.Wedge((.5, .5), 0.5, 30, 270, ec="none")
patches.append(wedge)
colors = np.linspace(0, 1, len(patches))
collection = PatchCollection(patches, cmap=plt.cm.hsv, alpha=0.3)
collection.set_array(np.array(colors))
ax.add_collection(collection)
plt.show()
Which produces something like:
Obviously you will need to tweak the theta1 and theta2 from their 30 and 270 to fit whatever angle you are trying to represent, and move the origin to wherever the sensors are located. Additionally you may want to color them all the same, instead of a rainbow but I'll let you figure out the details XD
Ended up using the following:
import matplotlib.pyplot as plt
from matplotlib.patches import Wedge
fig, ax = plt.subplots()
patches=[]
ax.axis('equal')
we = Wedge((2756.6747,5339751.8148),10,30,180,edgecolor='b',facecolor='none')
patches.append(we)
ax.add_artist(we)
ax.set(xlim=[2740, 2800], ylim=[5339740, 5339780])
plt.show()
thanks to the direction given by #reedinationer
I was trying to help someone add a colorbar for the vertical blue bar in the image below. We tried many variations of plt.colorbar(row_colors) (like above and below sns.clustermap()) and looked around online for 2 hours, but no luck. We just want to add a colorbar for the blues, please help!
import pickle
import numpy as np
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
feat_mat, freq, label = pickle.load(open('file.pkl', 'rb'))
feat_mat_df = pd.DataFrame(feat_mat[4])
freq_df = pd.DataFrame(freq)
freq_df_transposed = freq_df.transpose()
my_palette = dict(zip(set(freq_df_transposed[int('4')]), sns.color_palette("PuBu", len(set(freq_df_transposed[int('4')]))))))
row_colors = freq_df_transposed[int('4')].map(my_palette)
sns.clustermap(feat_mat_df, metric="euclidean", standard_scale=1, method="complete", cmap="coolwarm", row_colors = row_colors)
plt.show()
This is where he based his code from: #405 Dendrogram with heatmap and coloured leaves
I think something like this should work for your purposes- I didn't have a clustermap example available but the logic is the same to do what you want to do. Basically-you're going to take that list of colors you made and imshow it, then hide the imshow plot, and plot the colorbar in its place.
In my example, I use make_axes_locatable to place axes next to the plot with your data to put the colorbar inside - https://matplotlib.org/2.0.2/mpl_toolkits/axes_grid/users/overview.html. I find placing a new axes for other objects (legends color maps or otherwise) easier than trying to draw them on the same axes.
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np; np.random.seed(0)
import seaborn as sns
from mpl_toolkits.axes_grid1 import make_axes_locatable
import random
uniform_data = np.random.rand(10, 12)
fig, ax = plt.subplots(1,1, figsize = (5,5))
divider = make_axes_locatable(ax)
axDivY = divider.append_axes( 'right', size=0.2, pad= 0.1)
axDivY2 = divider.append_axes( 'right', size=0.2, pad= 0.2)
# we will use this for the colorscale bar
axDivY3 = divider.append_axes( 'right', size=0.2, pad= 0.2)
ax1 = sns.heatmap(uniform_data, ax=ax, cbar_ax=axDivY)
# the palette you were using to make the label column on the clustermap
# some simulated labels for your data with values
color_label_list =[random.randint(0,20) for i in range(20)]
pal = sns.color_palette("PuBu", len(set(color_label_list)))
n = len(pal)
size = 1
# plot the colors with imshow to make a colormap later
ax2 = axDivY2.imshow(np.array([color_label_list]),
cmap=mpl.colors.ListedColormap(list(pal)),
interpolation="nearest", aspect="auto")
# turn off the axes so they aren't visible- note that you need ax.axis('off) if you have older matplotlib
axDivY2.set_axis_off()
axDivY2.set_visible(False)
# plot the colorbar on the other axes (which is on top of the one that we turned off)
plt.colorbar(ax2, cax = axDivY3) ;