ax = df_seeds_train[df_seeds_train['cluster']==0].plot(kind='scatter', x='asymmetry', y='perimeter', s=50, c='green', sharex=False)
df_seeds_train[df_seeds_train['cluster']==1].plot(kind='scatter',x='asymmetry',y='perimeter',s=50, c='orange', sharex=False, ax = ax)
df_seeds_train[df_seeds_train['cluster']==2].plot(kind='scatter',x='asymmetry',y='perimeter',s=50, c='purple', sharex=False, ax = ax)
centers.plot(kind = 'scatter', x='asymmetry', y='perimeter', c='red', s=50, marker='x', sharex=False, ax=ax)
I need to get red markers in the center of my clusters but I keep getting KeyErrors for 'asymmetry' and 'perimeteter'. Does anybody know how to fix this. I added an image of the outcome I get now.Outcome
Thanks in advance!
df_seeds_train
Related
hello im ussing fb prophet and changing old white layout for a better experience, i manage to change background and line colors in 'plot.py' , but cant change black scatters as seen on pic.
allready make a search but dont find a way
how can i change it?
if ax is None:
fig = plt.figure(facecolor='w', figsize=figsize)
ax = fig.add_subplot(111)
else:
fig = ax.get_figure()
fcst_t = fcst['ds'].dt.to_pydatetime()
ax.plot(m.history['ds'].dt.to_pydatetime(), m.history['y'], 'k.')
ax.plot(fcst_t, fcst['yhat'], ls='-', c='#0072B2')
if 'cap' in fcst and plot_cap:
ax.plot(fcst_t, fcst['cap'], ls='--', c='k')
if m.logistic_floor and 'floor' in fcst and plot_cap:
ax.plot(fcst_t, fcst['floor'], ls='--', c='k')
if uncertainty and m.uncertainty_samples:
ax.fill_between(fcst_t, fcst['yhat_lower'], fcst['yhat_upper'],
color='#ffffff', alpha=0.2)
# Specify formatting to workaround matplotlib issue #12925
locator = AutoDateLocator(interval_multiples=False)
formatter = AutoDateFormatter(locator)
ax.xaxis.set_major_locator(locator)
ax.xaxis.set_major_formatter(formatter)
ax.grid(True, which='major', c='white', ls='-', lw=1, alpha=0.2)
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
fig.tight_layout()
return fig
my plot
Desired Outcome:
So I was trying to add legends to the subplots,
I tried to add ax[0].legend() to the code, but it raise "No handles with labels found to put in legend."
fig, ax = plt.subplots(1, 2, figsize=(15, 6))
ax = ax.flatten()
sns.histplot(train[train['Survived'] == 0]['Age'], ax=ax[0])
sns.histplot(train[train['Survived'] == 1]['Age'], color='red', alpha=0.4, ax=ax[0])
sns.histplot(train[train['Survived'] == 0]['Fare'], ax=ax[1])
sns.histplot(train[train['Survived'] == 1]['Fare'], color='red', alpha=0.4, ax=ax[1])
plt.show()
I know what to do now:
ax[0].legend(['Survived', 'Not Survived'])
ax[1].legend(['Survived', 'Not Survived'])
Consider this code:
fig, ax = plt.subplots(figsize=(8,5))
sns.heatmap(np.ones((4,4)), ax=ax, cbar=None, cmap="Blues", linewidths=1, linecolor='black')
fig, ax = plt.subplots(figsize=(8,5))
sns.heatmap(np.ones((4,4)), ax=ax, cmap="Blues", linewidths=1, linecolor='black')
It outputs:
Why does adding the argument cbar=None change the result of the outcome?
It is the default behavior by cbar, some kind of bug.
Turn off cbar:
fig, ax = plt.subplots(figsize=(8,5))
sns.heatmap(np.ones((4, 4)), ax=ax, cmap='Blues', linewidths=1, linecolor='black', cbar=False, vmin=0, vmax=2)
Turn on cbar:
fig, ax = plt.subplots(figsize=(8,5))
sns.heatmap(np.ones((4,4)), ax=ax, cmap='Blues', linewidths=1, linecolor='black', cbar=True, vmin=0, vmax=2)
As showed by #Alpha, cbar value should be boolean as it can be seen in the documentation
cbar : boolean, optional
Whether to draw a colorbar.
For both figures to have the same size, you can use:
fig, ax = plt.subplots(figsize=(8,5))
sns.heatmap(np.ones((4,4)), ax=ax, linewidths=1, linecolor='black', cmap="Blues",)
cbar = ax.collections[0].colorbar
cbar.remove()
plt.show()
and ...
fig, ax = plt.subplots(figsize=(8,5))
sns.heatmap(np.ones((4,4)), ax=ax, linewidths=1, linecolor='black', cmap="Blues",)
cbar = ax.collections[0].colorbar
#cbar.remove()
plt.show()
I have a DataFrame with three numerical variables Porosity, Perm and AI. I would like to make a subplot and in each plot, I would like the histogram of the three variables, by a categorical variable 'Facies'. Facies can take only two values: Sand and Shale.
In summary, each subplot needs a histogram and each histogram must be drawn based in the categorical variable Facies, to make a comparison between facies.
So far, I can make it work, but I cannot add the axis title to each subplot.
plt.subplot(311)
plt.hist(df_sd['Porosity'].values, label='Sand', bins=30, alpha=0.6)
plt.hist(df_sh['Porosity'].values, label='Shale', bins=30, alpha=0.6)
ax.set(xlabel='Porosity (fraction)', ylabel='Density', title='Porosity
Histogram')
plt.legend()
plt.subplot(312)
plt.hist(df_sd['log10Perm'].values, label='Sand', bins=30, alpha=0.6,)
plt.hist(df_sh['log10Perm'].values, label='Shale', bins=30, alpha=0.6)
ax.set(xlabel='Permeability (mD)', ylabel='Density', title='Permeability
Histogram')
plt.legend()
plt.subplot(313)
plt.hist(df_sd['AI'].values, label='Sand', bins=30, alpha=0.6)
plt.hist(df_sh['AI'].values, label='Shale', bins=30, alpha=0.6)
ax.set(xlabel='AI (units)', ylabel='Density', title='Acoustic Impedance
Histogram')
plt.legend()
plt.subplots_adjust(left=0.0, bottom=0.0, right=1.5, top=3.5, wspace=0.1,
hspace=0.2);
#I have tried with:
fig, axs = plt.subplots(2, 1)
but when I code
axs[0].hist(df_sd['Porosity'].values, label='Sand', bins=30, alpha=0.6)
axs[0].hist(df_sd['Porosity'].values, label='Shale', bins=30, alpha=0.6)
#But the histogram for shale overrides the histogram for Sand.
I would like to have this result but with both x and y axis with label names. Furthermore, it would be helpful to have a title for each subplot.
I just did a subplot with contours, but I think the framework will be very similar:
fig, axs = plt.subplots(2, 2, constrained_layout=True)
for ax, extend in zip(axs.ravel(), extends):
cs = ax.contourf(X, Y, Z, levels, cmap=cmap, extend=extend, origin=origin)
fig.colorbar(cs, ax=ax, shrink=0.9)
ax.set_title("extend = %s" % extend)
ax.locator_params(nbins=4)
plt.show()
I think the main point to note (and this I learned from the link below) is their use of zip(axs.ravel()) in the for loop to establish each ax and then plot what you wish on that ax. I'm fairly certain you can adapt this for your uses.
The full example is available at: https://matplotlib.org/gallery/images_contours_and_fields/contourf_demo.html#sphx-glr-gallery-images-contours-and-fields-contourf-demo-py
I have found an answer:
fig = plt.figure()
ax = fig.add_subplot(111)
ax1 = fig.add_subplot(311)
ax2 = fig.add_subplot(312)
ax2 = fig.add_subplot(313)
plt.subplot(311)
ax1.hist(df_sd['Porosity'].values, label='Sand', bins=30, alpha=0.6)
ax1.hist(df_sh['Porosity'].values, label='Shale', bins=30, alpha=0.6)
ax1.set(xlabel='Porosity (fraction)', ylabel='Density', title='Porosity Histogram')
ax1.legend()
I am working on a regression problem and I want to plot 3 DataFrames. I don't know how to set the labels for the Dataframes. I want blue->ACTUAL, green->SVR, red->MLR.
What is wrong with the code?
ax1 = y_test[1800:1900].plot(color='blue', linewidth=3)
predicted_y[1800:1900].plot(color='green', linewidth=3, ax =ax1)
predicted_y1[1800:1900].plot(color='red', linewidth=3, ax=ax1)
plt.legend(loc='upper center', bbox_to_anchor=(0.5, 1.05), prop={'size':35})
plt.show()
I plot this and it shows me all colors with 0 values.
I think it should work if you add labels to your plots:
ax1 = y_test[1800:1900].plot(color='blue', linewidth=3, label = 'ACTUAL')
predicted_y[1800:1900].plot(color='green', linewidth=3, ax =ax1, label = 'SVR')
predicted_y1[1800:1900].plot(color='red', linewidth=3, ax=ax1, label = 'MVR')
plt.legend(loc='upper center', bbox_to_anchor=(0.5, 1.05), prop={'size':35})
plt.show()