fig = plt.figure(figsize=(10,30))
ax1 = fig.add_subplot(1,1,1)
ax2 = fig.add_subplot(2,1,2)
ax3 = fig.add_subplot(3,1,3)
ax1.imshow(np.ones((100,200,3))) # white
ax2.imshow(np.zeros((100,200,3))) # black
ax3.imshow(np.zeros((100,200,3))) # black
The above code yields the below image
ax1 the white image is where I expected to be. the ax2, ax3, the black images are overlapped with each other
Figured it out!
Turns out add_subplot requires the overal dimensions. So the correct way to write it this
fig = plt.figure(figsize=(10,15))
ax1 = fig.add_subplot(3,1,1)
ax2 = fig.add_subplot(3,1,2)
ax3 = fig.add_subplot(3,1,3)
Related
I am trying to show the legend for each subplot but it doesn't seem like working. There is a box on the upper right of the subplot but it's empty. This is my code which I have tried. Also is there anyway I can enlarge the first subplot?
XWD_TO = data.iloc[:,0:1]
VAS_AX = data.iloc[:,1:2]
BTC_AUD = data.iloc[:,2:]
# Daily data chart of XWD.TO and VAS.AX (need to fix legend)
#fig = plt.figure()
fig, (ax1, ax2) = plt.subplots(2, sharex=True, sharey=True)
fig.suptitle('Daily Volatility')
ax1 = plt.subplot(211)
ax1.set_title("XWD.TO and VAS.AX")
ax1.plot(XWD_TO,c='orange')
ax1.plot(VAS_AX,c='green')
ax1.legend(loc = "upper right")
ax2 = plt.subplot(212)
ax2.set_title("BTC-AUD")
ax2.plot(BTC_AUD,c='blue')
plt.show()
You need to label each subplot. I have edited your code a bit. Hope it helps.
XWD_TO = data.iloc[:,0:1]
VAS_AX = data.iloc[:,1:2]
BTC_AUD = data.iloc[:,2:]
# Daily data chart of XWD.TO and VAS.AX (need to fix legend)
#fig = plt.figure()
fig, (ax1, ax2) = plt.subplots(2, sharex=True, sharey=True)
fig.suptitle('Daily Volatility')
ax1.set_title("XWD.TO and VAS.AX")
ax1.plot(XWD_TO,c='orange',label='XWD_TO')
ax1.plot(VAS_AX,c='green',label='VAS_AX'))
ax1.legend(loc = "upper right")
ax2.set_title("BTC-AUD")
ax2.plot(BTC_AUD,c='blue',label='BTC_AUD')
ax2.legend(loc = "upper right")
plt.show()
I'm sure that I've done all things right but in the end the result I got is a sccatter plot that only shows the second datasets data.
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.scatter(train["ENGINESIZE"], train["CO2EMISSIONS"], color = "green")
ax1.scatter(test["ENGINESIZE"], test["CO2EMISSIONS"], color = "red")
plt.xlabel("Engine Size")
plt.ylabel("Emission")
plt.show()
Here You can see what's going on in my output in link below.
It shows only red data(test data) in the output.
Where is the "output link below", please? For now I can only imagine what you are describing.
Also it helps if both plots have the same axis. That is, both have the same x-axis and then they can vary on their y-axis.
If so:
fig, ax = plt.subplots()
df.plot(kind = 'scatter', x= train["ENGINESIZE"], y = train["CO2EMISSIONS"], color = {'g'}, ax = ax)
df.plot(kind = 'scatter', x= test["ENGINESIZE"], y = test["CO2EMISSIONS"], color = {'r'}, ax = ax)
plt.xlabel()
def visualize(goal_x, goal_y, goal_z, epoch_arr):
# %% Create Color Map
colormap = plt.get_cmap("binary")
norm = matplotlib.colors.Normalize(vmin=min(epoch_arr), vmax=max(epoch_arr))
# %% 3D Plot
fig = plt.figure()
ax3D = fig.add_subplot(111, projection='3d')
ax3D.set_facecolor('xkcd:salmon')
ax3D.scatter(goal_x, goal_y, goal_z, s=100, c=colormap(norm(epoch_arr.values)), marker='o')
plt.show()
The above code produces the following picture:
However, as you can see there is a point in the right side that is clearly still not 100% opaque. You can see the grid lines through the point. How do I make the scatter plot points 100% opaque, no transparency?
Some tricks will help. Here I plot all the markers in white first, then plot again on top using the intended color.
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# make-up some data
goal_x = list(range(10))
goal_y = list(range(10))
goal_z = list(range(10))
epoch_arr = np.linspace(0,1,10)
fig = plt.figure(figsize=(8,8))
ax3D = fig.add_subplot(111, projection='3d')
ax3D.set_facecolor('xkcd:salmon')
# First plot: all markers are in white color
ax3D.scatter(goal_x, goal_y, goal_z, s=500, c='w', marker='o', alpha=1.0, zorder=10)
colormap = plt.get_cmap("binary")
norm = matplotlib.colors.Normalize(vmin=min(epoch_arr), vmax=max(epoch_arr))
#ax3D.scatter(goal_x, goal_y, goal_z, s=100, c=colormap(norm(epoch_arr.values)), marker='o')
# Second plot: use intended colormap
ax3D.scatter(goal_x, goal_y, goal_z, s=500, c='b', marker='o', zorder=11)
plt.show()
The resulting plot:
Setting alpha=1 should be enough.
ax3D.scatter(..., alpha=1)
Alternatively set depthshade=False
ax3D.scatter(..., depthshade=False)
The result will be the same in both cases.
I want to prepare some hexbin plots from Pandas. My initial code is:
fig = plt.figure(figsize=(11,8))
ax1 = fig.add_subplot(111)
df2.plot(kind='hexbin', x='var1', y='var2', C='var3', reduce_C_function=np.median, gridsize=25,vmin=0, vmax=40,ax=ax1)
ax1.set_xlim([-5,2])
ax1.set_ylim([0,7])
However when I change this to:
fig = plt.figure(figsize=(11,8))
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
And plot create four subplots similar to the first example it turns off the xlabels and xticklabels.
What code to I need to switch them back on? And is this something I can do as a defaults?
I would like to draw a triangle, but one of the sides needs to be a circle segment. The example is not working: all blue outside the circle needs to be removed. Can this be done directly, without calculating the entire contour myself?
Thank you!
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
polygon = plt.Polygon([(0,0.6),(1,2),(2,0.4)], True)
circle=plt.Circle((0,0),1.0,facecolor='None', edgecolor='black')
ax.add_patch(polygon)
ax.add_patch(circle)
plt.show()
You can use the set_clip_path property if you capture the added patch of the polygon. Given your example:
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
polygon = plt.Polygon([(0,0.6),(1,2),(2,0.4)], True)
circle = plt.Circle((0,0),1.0,facecolor='None', edgecolor='black')
patch_poly = ax.add_patch(polygon)
ax.add_patch(circle)
patch_poly.set_clip_path(circle)