I use this code that drow
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
data = pd.DataFrame({"Type of defect":["A","B","C","D","E","F","G","Other"], "Count":[17,202,387,25,825,12,3,45]})
data=data.set_index("Type of defect")
data = pd.concat([data[data.index!='Other'].sort_values(by='Count',ascending = False), data[data.index=='Прочее']])
data['Accumulated frequency'] = 100 *data['Count'].cumsum() / data['Count'].sum()
data['Limit']=80
data['Vital few']=np.where((data['Limit'] <= data['Accumulated frequency']) & (data['Limit'].shift(1) <= data['Accumulated frequency'].shift(1)), 0, 100)
fig, axes = plt.subplots()
ax1 = data.plot(use_index=True, y='Count', kind='bar', ax=axes)
ax2 = data.plot(use_index=True, y='Accumulated frequency', marker='D', color="C1", kind='line', ax=axes, secondary_y=True)
ax2.set_ylim([0,110])
ax3 = data.plot(use_index=True, y='Limit', color="gray", kind='line', linestyle='dashed', ax=axes, secondary_y=True)
ax4 = data.plot(use_index=True, y='Vital few', color="yellow", kind='area', ax=axes, secondary_y=True, alpha=0.1)
I get the following picture
However, I need to get this chart
The main problem is how to display "vital few" (Yellow area). There are also problems with the location of the legend and row/column labels. Please help me with this.
Area graphs cannot draw rectangles, so you need to use matplotlib's axvspan(). axvspan() is not reflected in the legend, so you need to add it, and use Patch to set the rectangle and label.
from matplotlib.patches import Patch
fig, axes = plt.subplots()
ax1 = data.plot(use_index=True, y='Count', kind='bar', ax=axes)
ax2 = data.plot(use_index=True, y='Accumulated frequency', marker='D', color="C1", kind='line', ax=axes, secondary_y=True)
ax2.set_ylim([0,110])
ax3 = data.plot(use_index=True, y='Limit', color="gray", kind='line', linestyle='dashed', ax=axes, secondary_y=True)
#ax4 = data.plot(use_index=True, y='Vital few', color="yellow", kind='area', ax=axes, secondary_y=True, alpha=0.1)
axes.axvspan(-0.5,1.25, ymax=0.95,facecolor='yellow', alpha=0.1)
handler1, label1 = ax1.get_legend_handles_labels()
#handler2, label2 = ax2.get_legend_handles_labels()
handler3, label3 = ax3.get_legend_handles_labels()
#print(label1, label2, label3)
add_legend = [Patch(facecolor='yellow', edgecolor='yellow', alpha=0.1, label='Vital few(right)')]
axes.legend(handles=handler1+handler3+add_legend)
plt.show()
EDIT:
If it is strictly linked to the y-axis value, it can be handled by a bar chart as an alternative method. By increasing the default width, the bars will be connected.
ax4 = data.plot(use_index=True, y='Vital few',color='yellow', kind='bar', width=1.0,ax=axes, secondary_y=True, alpha=0.1)
Related
fig, ax = plt.subplots(figsize = (10,7))
sns.lineplot(data = dearborn_1111_groupby,
x = 'Date',
y = 'Rent',
hue = 'generic_type',
palette = 'husl',
ax = ax).set_title('1111 Dearborn Median In Place Rents (2018 - 2022)')
sns.lineplot(data = dearborn_1111_groupby,
x = 'Date',
y = 'Rent_apartlist',
color = 'black',
ax = ax)
ax.legend(bbox_to_anchor = (1.15, 0.95), title = 'Unit Type')
plt.show()
line plot
I'm trying to add a legend containing the black line. However the black line is a separate lineplot. How Do I include the black line into the existing legend or a separate legend?
You can to add a label to the line, via sns.lineplot(..., label=...).
Note that when using bbox_to_anchor for the legend, you also need to set loc=.... By default, loc='best', which change the anchor point depending on small changes in the plot or its parameters. plt.tight_layout() fits the legend and the labels nicely into the plot figure.
Here is some example code using Seaborn's flights dataset.
import matplotlib.pyplot as plt
import seaborn as sns
flights = sns.load_dataset('flights')
fig, ax = plt.subplots(figsize=(12, 5))
sns.lineplot(data=flights,
x='year',
y='passengers',
hue='month',
palette='husl',
ax=ax)
sns.lineplot(data=flights,
x='year',
y='passengers',
color='black',
label='Black Line',
ax=ax)
ax.legend(bbox_to_anchor=(1.02, 0.95), loc="upper left", title='Unit Type')
ax.margins(x=0)
plt.tight_layout()
plt.show()
I have two working barplots about the sentiments of tweets (neutral, positive, negative). How can I merge them into one, side by side?
First bar:
plt.figure(figsize=(6,5))
plt.title('Classification of All tweets into sentiment categories',fontsize=15)
plt.ylabel('Percentage [%]',fontsize=18)
ax = (df_navalny.sentiment.value_counts()/len(df_navalny)*100).plot(kind="bar", rot=0,color=['#04407F','#0656AC','#0A73E1'])
ax.set_yticks(np.arange(0, 110, 10))
plt.grid(color='#95a5a6', linestyle='-.', linewidth=1, axis='y', alpha=0.7)
ax2 = ax.twinx()
ax2.set_yticks(np.arange(0, 110, 10)*len(df_navalny)/100)
for p in ax.patches:
ax.annotate('{:.2f}%'.format(p.get_height()), (p.get_x()+0.15, p.get_height()+1))
Second bar:
plt.figure(figsize=(6,5))
plt.title('Classification of All tweets into sentiment categories',fontsize=15)
plt.ylabel('Percentage [%]',fontsize=18)
ax = (df_putin.sentiment.value_counts()/len(df_putin)*100).plot(kind="bar", rot=0,color=['#04407F','#0656AC','#0A73E1'])
ax.set_yticks(np.arange(0, 110, 10))
plt.grid(color='#95a5a6', linestyle='-.', linewidth=1, axis='y', alpha=0.7)
ax2 = ax.twinx()
ax2.set_yticks(np.arange(0, 110, 10)*len(df_putin)/100)
for p in ax.patches:
ax.annotate('{:.2f}%'.format(p.get_height()), (p.get_x()+0.15, p.get_height()+1))
It's a bit complicated but Matplotlib site offers a demo and when you copy and past you have the following
Here it is the code
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import host_subplot
from mpl_toolkits import axisartist
# fake data
a = [2341, 5670, 4822]
b = [4290, 5205, 3966]
pca, pcb = [[round(100*x/sum(l),2) for x in l] for l in (a, b)]
# prepare all the vertical axes
ax = host_subplot(111, axes_class=axisartist.Axes)
plt.subplots_adjust(right=0.67)
axa = ax.twinx() ; axb = ax.twinx()
axb.axis['right'] = axb.new_fixed_axis(loc="right", offset=(60, 0))
axa.axis['right'].toggle(all=True)
axb.axis['right'].toggle(all=True)
# plot the bars PLUS invisible lines to represent the samples numerosities
x, w = np.array((1,2,3)), 0.30
ax.bar(x-w, pca, width=w, align='edge', label='a', zorder=1)
ax.bar(x-0, pcb, width=w, align='edge', label='b', zorder=1)
axa.plot((1,1),(0,sum(a)),lw=0)
axb.plot((1,1),(0,sum(b)),lw=0)
# fix xtics, xlabels, 'regular' yticks
plt.xticks((1,2,3)) ; ax.set_xticklabels('NO == YES'.split())
ax.set_yticks(range(0, 101, 10))
# all the ylabels
ax.set_ylabel('Percentages')
axa.set_ylabel('Numerosity of a')
axb.set_ylabel('Numerosity of b')
axa.set_ylim(bottom=0.0)
axb.set_ylim(bottom=0.0)
plt.legend()
plt.grid(zorder=0)
plt.show()
My colorbar is very far away from the bottom of my heatmap. Is there a way to move it closer?
My code is:
import seaborn as sns
Granger2 = Granger
Granger2.columns = Granger_colnames
Granger2.index = Granger_rownames
fig, ax = plt.subplots(figsize=(6,25))
sns.heatmap(Granger2, cmap=rvb, cbar=True, ax=ax,linewidths=.5,cbar_kws={"orientation": "horizontal"})
ax.xaxis.tick_top() # x axis on top
ax.xaxis.set_label_position('top')
#Remove ticks
ax.tick_params(axis='both', which='both', length=0)
# Drawing the frame
ax.axhline(y = 0, color='k',linewidth = 1)
ax.axhline(y = Granger2.shape[0], color = 'k',linewidth = 1)
ax.axvline(x = 0, color = 'k', linewidth = 1)
ax.axvline(x = Granger2.shape[1], color = 'k', linewidth = 1)
plt.show()
You can use e.g. cbar_kws={"orientation": "horizontal", "pad":0.02}. The padding is a fraction of the subplot height, so 0.02 is 2%. See the colorbar docs for more information about pad and other parameters.
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
sns.set_style('whitegrid')
flights = sns.load_dataset('flights')
flights = flights.pivot('year', 'month').droplevel(0, axis=1)
fig, ax = plt.subplots(figsize=(6, 20))
sns.heatmap(flights, cmap='Greens', cbar=True, ax=ax, linewidths=.5,
cbar_kws={"orientation": "horizontal", "pad": 0.02})
ax.xaxis.tick_top() # x axis on top
ax.xaxis.set_label_position('top')
# Remove ticks
ax.tick_params(axis='both', which='both', length=0)
# Drawing the frame
ax.patch.set_edgecolor('0.15')
ax.patch.set_linewidth(2)
plt.tight_layout()
plt.show()
I'm trying to control the zorder of different plots across twinx axes. How can I get the blue noisy plots to appear in the background and the orange smoothed plots to appear in the foreground in this plot?
from matplotlib import pyplot as plt
import numpy as np
from scipy.signal import savgol_filter
random = np.random.RandomState(0)
x1 = np.linspace(-10,10,500)**3 + random.normal(0, 100, size=500)
x2 = np.linspace(-10,10,500)**2 + random.normal(0, 100, size=500)
fig,ax1 = plt.subplots()
ax1.plot(x1, zorder=0)
ax1.plot(savgol_filter(x1,99,2), zorder=1)
ax2 = ax1.twinx()
ax2.plot(x2, zorder=0)
ax2.plot(savgol_filter(x2,99,2), zorder=1)
plt.show()
Similar to this thread, though not ideal, this is an approach using twiny along with twinx.
# set up plots
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax3 = ax1.twiny()
ax4 = ax2.twiny()
# background
ax1.plot(x1)
ax2.plot(x2)
# smoothed
ax3.plot(savgol_filter(x1,99,2), c='orange')
ax4.plot(savgol_filter(x2,99,2), c='orange')
# turn off extra ticks and labels
ax3.tick_params(axis='x', which='both', bottom=False, top=False)
ax4.tick_params(axis='x', which='both', bottom=False, top=False)
ax3.set_xticklabels([])
ax4.set_xticklabels([])
# fix zorder
ax1.set_zorder(1)
ax2.set_zorder(2)
ax3.set_zorder(3)
ax4.set_zorder(4)
plt.show()
Output:
I have a 2x2 graph with date in x-axis in both graphs. I have used datetime.strptime to bring a string into type = datetime.datetime object format.
However I am planning to have some 12 subplots and doing this the following way seems messy.
Is there a better 'pythonic' way?
This is what I have:
xx.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%y %H:%M'))
plt.grid(True)
plt.ylabel('paramA',fontsize=8, color = "blue")
plt.tick_params(axis='both', which='major', labelsize=8)
plt.plot(date_list, myarray[:,0], '-b', label='paramA')
plt.setp(plt.xticks()[1], rotation=30, ha='right') # ha is the same as horizontalalignment
xx = plt.subplot(2,1,2)
xx.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%y %H:%M'))
plt.grid(True)
plt.ylabel('paramB', 'amount of virtual mem',fontsize=8, color = "blue")
plt.tick_params(axis='both', which='major', labelsize=8)
plt.plot(date_list, myarray[:,1], '-y', label='paramB')plt.setp(plt.xticks()[1], rotation=30, ha='right') # ha is the same as horizontalalignment ```
PS: Initially I tried defining the plot as follows. This however did not work:
fig, axs = plt.subplots(2,1,figsize=(15,15))
plt.title('My graph')
for ax in enumerate(axs):
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%y %H:%M:%S'))
You failed to provide any data or a Minimal, Complete, and Verifiable example. Nevertheless, something like this should work. You can extend it to your real case by using desired number of rows and columns in the first command.
fig, axes = plt.subplots(nrows=2, ncols=3)
labels = ['paramA', 'paramB', 'paramC', 'paramD', 'paramE', 'paramF']
for i, ax in enumerate(axes.flatten()):
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%y %H:%M'))
ax.grid(True)
ax.set_ylabel(labels[i], fontsize=8, color="blue")
ax.tick_params(axis='both', which='major', labelsize=8)
ax.plot(date_list, myarray[:,i], '-b', label=labels[i])
plt.setp(plt.xticks()[1], rotation=30, ha='right') # ha is the same as horizontalalignment
EDIT:
Change your code to
fig, axs = plt.subplots(2,1,figsize=(15,15))
plt.title('My graph')
for ax in axs:
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%y %H:%M:%S'))