I have an numpy 2d array want to show it inside Ellipse Demo
the first line is the color and the second is the sum of feeling which is the third line
xz = np.array([['E6C637', '1692', 'well'],
['7EC31B', '1386', 'free'],
['595884', '1032', 'alone'],
['40B6B8', '905', 'comfortable'],
['99D013', '687', 'fine']])
inside this code:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Ellipse
NUM = 250
ells = [Ellipse(xy=np.random.rand(2) * 10,
width=np.random.rand(), height=np.random.rand(),
angle=np.random.rand() * 360)
for i in range(NUM)]
fig, ax = plt.subplots(subplot_kw={'aspect': 'equal'})
for e in ells:
ax.add_artist(e)
e.set_clip_box(ax.bbox)
e.set_alpha(np.random.rand())
e.set_facecolor(np.random.rand(3))
ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
plt.show()
The following code shows the given data as a pie chart:
import matplotlib.pyplot as plt
import numpy as np
xz = [['E6C637', '1692', 'well'],
['7EC31B', '1386', 'free'],
['595884', '1032', 'alone'],
['40B6B8', '905', 'comfortable'],
['99D013', '687', 'fine']]
labels = [data[2] for data in xz]
sizes = [float(data[1]) for data in xz]
colors = ['#'+data[0] for data in xz]
fig1, ax1 = plt.subplots()
ax1.pie(sizes, labels=labels, colors=colors,
autopct='%1.1f%%', startangle=90)
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
# plt.legend() # show a legend
plt.show()
Related
When I run the following lines, I get a plot with a large space at the top and the bottom with no bars.
How can I remove this extra space?
import pandas as pd
import numpy as np
import random
import matplotlib.pyplot as plt
from matplotlib.transforms import Affine2D
random.seed(1)
df = pd.DataFrame(np.random.randn(50, 1), columns=["parameter"])
df["standard_error"]= ((df.parameter**2)**0.5)/2
name = "plot"
x = ["A"+str(x) for x in df.index.tolist()]
y1 = df.parameter
yerr1 = df.standard_error
fig, ax = plt.subplots()
fig.set_figheight(len(x))
plt.rc('axes', labelsize=22)
plt.grid(b=True, which='major', color='#666666', linestyle='-', alpha=0.2)
trans1 = Affine2D().translate(-0.1, 0.0) + ax.transData
trans2 = Affine2D().translate(+0.1, 0.0) + ax.transData
er1 = ax.errorbar(y1, x, xerr=yerr1, marker="o", linestyle="none", transform=trans1)
ax.axvline(x=0, color="black")
plt.savefig(name + '.png', bbox_inches='tight')
If you mean the extra space below and above your smallest and largest data points along the y-axis then you can simply use plt.ylim, e.g:
plt.ylim(0, 50)
Which will change the extent of the y-axis to the range 0 - 50. Similarly for the x-axis there's plt.xlim
I want to set discrete colorbar in ImageGrid.
ImageGrid
Here's an example:
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
import numpy as np
import matplotlib
lon,lat = np.meshgrid(np.arange(-180, 180, 10), np.arange(-85, 90, 10))
data = np.sort(np.random.rand(18, 36),axis=1)
fig = plt.figure()
grid = ImageGrid(fig, 111,
nrows_ncols=(2, 1),
axes_pad=(0.35, 0.35),
label_mode="1",
share_all=True,
cbar_location="right",
cbar_mode="each",
cbar_size="5%",
cbar_pad="6%",
)
# Settings
bounds = [0,0.01,0.04,0.07,0.1,0.13,0.16,0.2,0.25,0.35,0.45,0.6,0.9]
colors = ['#390231','#7F1CAB','#0047FD','#0072FE','#019EFF','#00C4FF','#01EDFF',\
'#00FFFB','#00FFC8','#29F905','#FBDD03','#FA0F00']
# Original colorbar
p = grid[0].pcolormesh(lon,lat,data, vmin=0, vmax=0.9, cmap='jet')
cb = grid.cbar_axes[0].colorbar(p)
# Defined colorbar
cmap = matplotlib.colors.ListedColormap(colors)
norm = matplotlib.colors.BoundaryNorm(bounds, cmap.N)
p = grid[1].pcolormesh(lon,lat,data, cmap=cmap, norm=norm)
cb = grid.cbar_axes[1].colorbar(p, ticks=bounds)
grid[0].set_title('jet')
grid[1].set_title('Defined')
plt.show()
This is the result:
As you can see, the location of ticks are wrong.
If ticks are at boundaries of each color block, the second figure will look correct.
Subplots
Then, I tested subplots. It works fine!
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
lon,lat = np.meshgrid(np.arange(-180, 180, 10), np.arange(-85, 90, 10))
data = np.sort(np.random.rand(18, 36),axis=1)
f, (ax1, ax2) = plt.subplots(1, 2,sharey=True)
# Settings
bounds = [0,0.01,0.04,0.07,0.1,0.13,0.16,0.2,0.25,0.35,0.45,0.6,0.9]
colors = ['#390231','#7F1CAB','#0047FD','#0072FE','#019EFF','#00C4FF','#01EDFF',\
'#00FFFB','#00FFC8','#29F905','#FBDD03','#FA0F00']
# Defined colorbar
cmap = matplotlib.colors.ListedColormap(colors)
norm = matplotlib.colors.BoundaryNorm(bounds, cmap.N)
# Jet
p = ax1.pcolormesh(lon,lat,data, vmin=0, vmax=0.9, cmap='jet')
f.colorbar(p,ax=ax1)
ax1.set_title('jet')
# Defined
p = ax2.pcolormesh(lon,lat,data, cmap=cmap, norm=norm)
f.colorbar(p,ax=ax2,ticks=bounds)
ax2.set_title('defined')
plt.show()
This is the result:
Single
I tested my script in single figure. It works fine!
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
lon,lat = np.meshgrid(np.arange(-180, 180, 10), np.arange(-85, 90, 10))
data = np.sort(np.random.rand(18, 36),axis=1)
fig = plt.figure()
# Settings
bounds = [0,0.01,0.04,0.07,0.1,0.13,0.16,0.2,0.25,0.35,0.45,0.6,0.9]
colors = ['#390231','#7F1CAB','#0047FD','#0072FE','#019EFF','#00C4FF','#01EDFF',\
'#00FFFB','#00FFC8','#29F905','#FBDD03','#FA0F00']
# Defined colorbar
cmap = matplotlib.colors.ListedColormap(colors)
norm = matplotlib.colors.BoundaryNorm(bounds, cmap.N)
# Jet
plt.pcolormesh(lon,lat,data, vmin=0, vmax=0.9, cmap='jet')
plt.colorbar()
plt.show()
# Defined
p = plt.pcolormesh(lon,lat,data, cmap=cmap, norm=norm)
plt.colorbar(p, ticks=bounds)
plt.title('Single fig')
plt.show()
This is the result of single figure of jet and defined:
A workaround would be to set the labels manually.
ticks=np.linspace(bounds[0],bounds[-1], len(bounds))
cb = grid.cbar_axes[1].colorbar(p, ticks=ticks)
cb.ax.set_yticklabels(bounds)
I'm trying to plot 23 graphs in a 6x4 grid, with one figure taking up twice the width of the other figures. I'm using gridspec and my current code is:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec
x = np.arange(0, 7, 0.01)
fig = plt.figure(figsize=(6, 4))
gs = gridspec.GridSpec(nrows=6, ncols=4)
for n in range(22):
ax = fig.add_subplot(gs[n])
ax.plot(x, np.sin(0.2*n*x))
corrax = fig.add_subplot(gs[22])
fig.tight_layout()
plt.show()
This produces the following:
I want to increase the width of the rightmost plot in the bottom row so it takes up the remaining space in that row. Is there a way to accomplish this?
You can use slices to select several positions from the gridspec, e.g. gs[22:24].
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec
x = np.arange(0, 7, 0.01)
fig = plt.figure(figsize=(6, 4))
gs = gridspec.GridSpec(nrows=6, ncols=4)
for n in range(22):
ax = fig.add_subplot(gs[n])
ax.plot(x, np.sin(0.2*n*x))
corrax = fig.add_subplot(gs[22:24])
corrax.plot(x,np.sin(0.2*22*x), color="crimson", lw=3)
fig.tight_layout()
plt.show()
You can also slice the gridspec two-dimensionally. E.g. to create a 3x3 grid and make the plot in the lower right corner span two columns and two rows, you could slice like gs[1:,1:].
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec
x = np.arange(0, 7, 0.01)
fig = plt.figure(figsize=(6, 4))
gs = gridspec.GridSpec(nrows=3, ncols=3)
for n in range(3):
ax = fig.add_subplot(gs[0,n])
ax.plot(x, np.sin(0.2*n*x))
if n !=0:
ax = fig.add_subplot(gs[n,0])
ax.plot(x, np.sin(0.2*n*x))
corrax = fig.add_subplot(gs[1:,1:])
corrax.plot(x,np.sin(0.2*22*x), color="crimson", lw=3)
fig.tight_layout()
plt.show()
#corrax = fig.add_subplot(gs[5,2:])
corrax = fig.add_subplot(6,4,(23,24))
both shold work.
see examples
In the example below, I create a rectangular patch using matplotlib.patches.Polygon. Is there a way to scale the patch before adding it to the plot?
I've tried using matplotlib.transforms.Affine2D in a variety of ways with no success. As usual, the matplotlib documentation on transformations is woefully insufficient.
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot([-3,3],[-3,3])
x = [-1,0,1,1,0,-1]
y = [1,1,1,-1,-1,-1]
poly = Polygon( zip(x,y), facecolor='red', edgecolor='red', alpha=0.5)
ax.add_patch(poly)
plt.show()
If by scale you mean multiplication by a factor, you can easily do this via numpy.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot([-3,3],[-3,3])
x = [-1,0,1,1,0,-1]
y = [1,1,1,-1,-1,-1]
scale = 2
poly = Polygon( np.c_[x,y]*scale, facecolor='red', edgecolor='red', alpha=0.5)
ax.add_patch(poly)
plt.show()
The same can be achieved with a matplotlib.transforms.Affine2D() transform.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
import matplotlib.transforms as transforms
fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot([-3,3],[-3,3])
x = [-1,0,1,1,0,-1]
y = [1,1,1,-1,-1,-1]
trans = transforms.Affine2D().scale(2) + ax.transData
poly = Polygon( np.c_[x,y], facecolor='red', edgecolor='red', alpha=0.5,
transform=trans)
ax.add_patch(poly)
plt.show()
Although it seems a bit overkill for a simple scaling like this.
I am playing around with histogram2d and I am trying incorporate a color bar logarithmic values.
Here is my current code:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib.colors import LinearSegmentedColormap
cmap = LinearSegmentedColormap.from_list('mycmap', ['black', 'maroon',
'crimson', 'orange', 'white'])
fig = plt.figure()
ax = fig.add_subplot(111)
H = ax.hist2d(gas_pos[:,0]/0.7, gas_pos[:,1]/0.7, cmap=cmap,
norm=matplotlib.colors.LogNorm(), bins=350, weights=np.log(gas_Temp))
ax.tick_params(axis=u'both', which=u'both',length=0)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
cb = fig.colorbar(H[3], ax=ax, shrink=0.8, pad=0.01,
orientation="horizontal", label=r'$\log T\ [\mathrm{K}]$')
cb.ax.set_xticklabels([1,2,3,4])
cb.update_ticks()
empty = Rectangle((0,0 ), 0, 0, alpha=0.0)
redshift = fig.legend([empty], [r'$z = 127$'],
loc='upper right', frameon=False, handlelength=0, handletextpad=0)
redshift.get_texts()[0].set_color('white')
#fig.add_artist(redshift)
plt.show()
The weights are values not passed through np.log() and are currently being normalized through LogNorm().
What I am trying to get is to have the colorbar tic labels to be the logarithmic values of what is currently there eg. 10**4 --> 4, 10**6 --> 6, etc.
I have tried changing the formatting and also passing through the logarithmic values of np.log(gas_Temp), but nothing is really working.
The idiomatic thing to do is use a LogFormatterExponent to do the formatting of your colorbar. That's exactly what you need: to display 10**x values as x, or in other words, to display y values as log10(x).
Proof using dummy data:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
from matplotlib.ticker import LogFormatterExponent # <-- one new import here
# generate dummy data
histdata = 10**(np.random.rand(200,200)*4 + 1) # 10^1 -> 10^5
# plot
fig = plt.figure()
ax = fig.add_subplot(111)
ax.tick_params(axis=u'both', which=u'both',length=0)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
im = plt.imshow(histdata,cmap='viridis',norm=LogNorm())
cb = fig.colorbar(im, ax=ax, shrink=0.8, pad=0.01,
orientation="horizontal", label=r'$\log T\ [\mathrm{K}]$')
# v-- one new line here
cb.formatter = LogFormatterExponent(base=10) # 10 is the default
cb.update_ticks()
Compare the result of your original (left) with the modified version (right):