Plot rectangle using defined x-axis in Matplotlib - matplotlib

I'd like to plot rectangles using defined xticks and ylim using Matplotlib, as shown in the example below:
import matplotlib.pyplot as plt
x = ['00:00', '01:00', '02:00', '03:00', '04:00' , '05:00', '06:00', '07:00', '08:00' ,'09:00' ,'10:00', '11:00', '12:00',
'13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00']
plt.ylim([1,10])
With those limits, use the x index to print a rectangle following this:
rect = Rectangle((x[4], x[7]), 4, 8, color='yellow')
Finally, the idea is have multiples rectangles. There's a way to do that without use date/time functions?

The parameters for plt.Rectangle are ((x, y), width, height). You can draw a rectangle for example as follows:
import matplotlib.pyplot as plt
from matplotlib.colors import to_rgba
x = ['00:00', '01:00', '02:00', '03:00', '04:00', '05:00', '06:00', '07:00', '08:00', '09:00', '10:00', '11:00',
'12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00']
plt.figure(figsize=(15, 5))
plt.xticks(range(len(x)), x)
plt.ylim([1, 10])
x_start, x_end = 4, 7
y_start, y_end = 4, 8
ax = plt.gca()
ax.add_patch(plt.Rectangle((x_start, y_start), x_end - x_start, y_end - y_start,
facecolor=to_rgba('crimson', 0.5), edgecolor='black', lw=2))
plt.show()

Related

matplotlib grouped bar color change

I am trying to compare group A and group B using grouped bar, but group A must have the same colors and group B must have different colors and legends. I somehow created graph, but not sure how to change the color of each group B bar graphs..
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
labels = ['M1', 'A1', 'M2', 'A2', 'M3', 'A3', 'M4', 'A4', 'M5', 'A5']
A_group = [20, 34, 30, 35, 27, 17, 64, 23, 47, 52]
B_group = [25, 32, 34, 20, 25, 76, 33, 54, 16, 21]
x = np.arange(len(labels)) # the label locations
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, A_group, width, label='A group')
rects2 = ax.bar(x + width/2, B_group, width, label='B group')
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Accuracy')
ax.set_title('Test')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()
ax.bar_label(rects1, padding=3)
ax.bar_label(rects2, padding=3)
plt.xticks(rotation=30, ha='right')
plt.ylim(0, 100)
fig.tight_layout()
plt.show()
Now my graph looks like this:
I want to make my graph like this. Below is an example using powerpoint.
Any helps will be appreciated. Thank you in advance.
Try drawing the bars in B_group one by one:
fig, ax = plt.subplots()
ax.bar(x-width/2, A_group, width=width,label='A Group')
cmap = plt.get_cmap('tab20')
ax.set_prop_cycle(color=[cmap(k) for k in x+1])
for i in x:
ax.bar(i+width/2, B_group[i], width=width)
Output:

Matplotlib scatter plot color-coded by text, how to add legend?

I'm trying to color-code a scatter plot based on the string in a column. I can't figure out how to set up the legend.
Repeatable Example
%matplotlib inline
import matplotlib.pyplot as plt
import pandas as pd
## Dummy Data
x = [0, 0.03, 0.075, 0.108, 0.16, 0.26, 0.37, 0.49, 0.76, 1.05, 1.64,
0.015, 0.04, 0.085, 0.11, 0.165, 0.29, 0.37, 0.6, 0.78, 1.1]
y = [16.13, 0.62, 2.15, 41.083, 59.97, 13.30, 7.36, 6.80, 4.97, 3.53, 11.77,
30.21, 64.47, 57.64, 56.83, 46.69, 4.22, 30.35, 35.12, 5.22, 25.32]
label = ['a', 'a', 'c', 'a', 'c', 'b', 'c', 'c', 'c', 'b', 'c',
'c', 'c', 'a', 'b', 'a', 'a', 'a', 'b', 'c', 'c', 'c']
df = pd.DataFrame(
list(zip(x, y, label)),
columns =['x', 'y', 'label']
)
## Set up colors dictionary
mydict = {'a': 'darkviolet',
'b': 'darkgoldenrod',
'c': 'olive'}
## Plotting
plt.scatter(df.x, df.y, c=df['label'].map(mydict))
plt.legend(loc="upper right", frameon=True)
Current Output
Desired Output
Same plot as above, I just want to define the legend handle.
Thanks for any help
You can use matplotlib.patches.mpatches
Just add these lines of code to your script
import matplotlib.patches as mpatches
fake_handles = [mpatches.Patch(color=item) for item in mydict.values()]
label = mydict.keys()
plt.legend(fake_handles, label, loc='upper right', prop={'size': 10})
and you will get
You will make a list of legend handles as shown below. legendhandle will take the first element of the list of lines.
import matplotlib.pyplot as plt
import pandas as pd
## Dummy Data
x = [0, 0.03, 0.075, 0.108, 0.16, 0.26, 0.37, 0.49, 0.76, 1.05, 1.64,
0.015, 0.04, 0.085, 0.11, 0.165, 0.29, 0.37, 0.6, 0.78, 1.1]
y = [16.13, 0.62, 2.15, 41.083, 59.97, 13.30, 7.36, 6.80, 4.97, 3.53, 11.77,
30.21, 64.47, 57.64, 56.83, 46.69, 4.22, 30.35, 35.12, 5.22, 25.32]
label = ['a', 'a', 'c', 'a', 'c', 'b', 'c', 'c', 'c', 'b', 'c',
'c', 'c', 'a', 'b', 'a', 'a', 'a', 'b', 'c', 'c', 'c']
df = pd.DataFrame(
list(zip(x, y, label)),
columns =['x', 'y', 'label']
)
## Set up colors dictionary
mydict = {'a': 'darkviolet',
'b': 'darkgoldenrod',
'c': 'olive'}
legendhandle = [plt.plot([], marker="o", ls="", color=color)[0] for color in list(mydict.values())]
plt.scatter(df.x, df.y, c=df['label'].map(mydict))
plt.legend(legendhandle,list(mydict.keys()),loc="upper right", frameon=True)
plt.show()
Are you open to seaborn:
import seaborn as sns
sns.scatterplot(data=df, x='x',y='y',hue='label', palette=mydict)
Output:
With pandas/matplotlib only, you can do a loop:
fig, ax = plt.subplots()
for l,d in df.groupby('label'):
d.plot.scatter(x='x',y='y', label=l, c=mydict[l], ax=ax)
plt.legend()
Output:

matplotlib scatterplot - only a few labels are displayed on x axis

While plotting using scatterplot in matplotlib, I find some of the values from x-axis are missing in the labels. I want to have all the x-axis legends to be displayed in the graph.
This might be related to tick spacing but I am not sure how to set it to display all the x-axis values.
In the sample code, I want to have all the dates displayed on x-axis
x = [datetime.date(2019, 6, 16), datetime.date(2019, 6, 17), datetime.date(2019, 6, 18), datetime.date(2019, 6, 19),
datetime.date(2019, 6, 20), datetime.date(2019, 6, 21), datetime.date(2019, 6, 22), datetime.date(2019, 6, 23),
datetime.date(2019, 6, 24), datetime.date(2019, 6, 25), datetime.date(2019, 6, 26), datetime.date(2019, 6, 27),
datetime.date(2019, 6, 28), datetime.date(2019, 6, 29), datetime.date(2019, 6, 30), datetime.date(2019, 7, 1),
datetime.date(2019, 7, 2), datetime.date(2019, 7, 3), datetime.date(2019, 7, 4), datetime.date(2019, 7, 5),
datetime.date(2019, 7, 6), datetime.date(2019, 7, 7), datetime.date(2019, 7, 8), datetime.date(2019, 7, 9),
datetime.date(2019, 7, 10), datetime.date(2019, 7, 11), datetime.date(2019, 7, 12), datetime.date(2019, 7, 13),
datetime.date(2019, 7, 15)]
y = [0.15338331291011087, 0.15340904024033467, 0.1534195786228156, 0.15343290378685995, 0.15331644003478487,
0.1533570064827251, 0.1531156771286262, 0.15307150988142237, 0.15306137109205153, 0.15302301551230038,
0.15295889536607005, 0.15298157619113423, 0.15286883583977182, 0.15283539558962958, 0.15284508041253356,
0.15281542656182034, 0.1527844647725921, 0.15277054534676898, 0.1527339281127108, 0.15270419704783855,
0.15261812595095475, 0.15255120245035042, 0.15251650362641, 0.15257536163149088, 0.15253967278547242,
0.15249871561808356, 0.15248591103997422, 0.15242121840852002, 0.15248773465596907]
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.scatter(x, y, s=10, c='b', marker="s", label='y')
plt.legend(loc='upper left')
plt.xticks(rotation=90)
plt.show()
Plot that I get with the sample code
Just pass the value of x in the plt.xticks() and set x-axis using 'plt.gcf' it will work.
I have create a random list for the x and plot the graph check it.
from matplotlib import pyplot as plt
from datetime import datetime
def std(a):
return datetime.strptime(a, '%Y, %m, %d').date()
x = [std('2019, 6, 16'), std('2019, 6, 17'), std('2019, 6, 18'), std('2019, 6, 19'),
std('2019, 6, 20'), std('2019, 6, 21'), std('2019, 6, 22'), std('2019, 6, 23'),
std('2019, 6, 24'), std('2019, 6, 25'), std('2019, 6, 26'), std('2019, 6, 27'),
std('2019, 6, 28'), std('2019, 6, 29'), std('2019, 6, 30'), std('2019, 7, 1'),
std('2019, 7, 2'), std('2019, 7, 3'), std('2019, 7, 4'), std('2019, 7, 5'),
std('2019, 7, 6'), std('2019, 7, 7'), std('2019, 7, 8'), std('2019, 7, 9'),
std('2019, 7, 10'), std('2019, 7, 11'), std('2019, 7, 12'), std('2019, 7, 13'),
std('2019, 7, 15')]
y = [0.15338331291011087, 0.15340904024033467, 0.1534195786228156, 0.15343290378685995, 0.15331644003478487,
0.1533570064827251, 0.1531156771286262, 0.15307150988142237, 0.15306137109205153, 0.15302301551230038,
0.15295889536607005, 0.15298157619113423, 0.15286883583977182, 0.15283539558962958, 0.15284508041253356,
0.15281542656182034, 0.1527844647725921, 0.15277054534676898, 0.1527339281127108, 0.15270419704783855,
0.15261812595095475, 0.15255120245035042, 0.15251650362641, 0.15257536163149088, 0.15253967278547242,
0.15249871561808356, 0.15248591103997422, 0.15242121840852002, 0.15248773465596907]
fig = plt.figure(figsize=(8,5))
ax1 = fig.add_subplot(111)
ax1.scatter(x, y, s=10, c='b', marker="s", label='y')
plt.legend(loc='upper left')
#plt.xticks(x,rotation=90)
#plt.xticks(range(len(x)))
plt.gca().margins(x=0)
plt.gcf().canvas.draw()
t_l = plt.gca().get_xticklabels()
maxsize = max([t.get_window_extent().width for t in t_l])
m = .2 # inch margin
s = maxsize/plt.gcf().dpi*len(x)+3*m
margin = m/plt.gcf().get_size_inches()[1]
plt.gcf().subplots_adjust(left=margin, right=0.8-margin)
plt.gcf().set_size_inches(s, plt.gcf().get_size_inches()[1])
plt.xticks(x,rotation=90)
plt.show()

color coding using scalar mappable in matplotlib

is a subplot I created using matplotlib. Is it possible to code the colors on the basis of a pre-defined range? I want to pass an additional parameter, voltage to the function drawLoadDuration and define a scale (using if-else construct?) that sets the color. Higher the voltage, darker the shade. Also, for some reason, the y-tick labels for the colorbar are not showing.
Any lead is most welcome...Thanks!
import matplotlib.cm
from pylab import *
import numpy as np
f, (ax1, ax2, ax3) = plt.subplots(3, sharex=True, sharey=False)
#other subplots
ax3.set_title('Load Profile')
ax3.patch.set_facecolor('silver')
ax3.grid(True)
cmap= plt.cm.bone_r
barHeight = 3
ticklist = []
def drawLoadDuration(period, starty, opacity):
ax3.broken_barh((period), (starty, barHeight), alpha=opacity, facecolors=cmap(opacity), lw=0.5, zorder=2)
ticklist.append(starty+barHeight/2.0)
return 0
drawLoadDuration([(0, 5), (13, 4), (19, 3), (23, 1)], 3, 0.5) #Fan
drawLoadDuration([(19, 1)], 9, 0.65) #Tube Light
drawLoadDuration([(19, 5)], 15, 0.35) #Bulb
drawLoadDuration([(7, 2), (16, 1)], 21, 0.28) #Charger
drawLoadDuration([(15, 0.5), (20, 1)], 27, 0.7) #Television
drawLoadDuration([(9, 1), (17, 1)], 33, 1) #Pump
drawLoadDuration([(2,4)], 39, 0.8) #Scooter
ax3.set_ylim(0, 45)
ax3.set_xlim(0, 24)
ax3.set_xlabel('Time (Hours)')
ax3.set_yticks(ticklist)
xticklist = np.linspace(0.5, 24, 48)
ax3.set_xticks(xticklist)
ax3.set_xticklabels(["{}{}m".format(int(h%12+12*(h%12==0)),
{0:"p",1:"a"}[(h%24)<12]) if ((h*10)%10)==0 \
else "" for h in xticklist], fontsize='9', rotation=90)
ax3.tick_params('x', colors=cmap(1.0), tick1On=True)
ax3.set_yticklabels(['Fan', 'Tube light', 'Bulb', 'Cellphone Charger', 'Television', 'Pump', 'Scooter'])
######################### Code Block for Colorbar
sm = matplotlib.cm.ScalarMappable(cmap=cmap) # create a scalarmappable from the colormap
sm.set_array([])
cbar = f.colorbar(sm, ticks=[-3, -2, -1, 0, 1, 2, 3], aspect=10, orientation='vertical', ax=ax3) # using scalarmappable to create colorbar
cbar.ax.text(3, 0.65, 'Power', rotation=90)
cbar.ax.set_yticklabels(['>1000', '>800', '>500', '>200', '>100', '<10']) #not working!!!
plt.show()
You may create a normalization instance, matplotlib.colors.Normalize(vmin=0, vmax=1000) as to map the voltage values to the range between 0 and 1, which will then be understood by the colormap. Inside the plotting function you would use this normalization as facecolors=cmap(norm(voltage)).
import matplotlib.cm
import matplotlib.pyplot as plt
import numpy as np
f, ax3 = plt.subplots()
ax3.set_title('Load Profile')
ax3.patch.set_facecolor('silver')
ax3.grid(True)
cmap= plt.cm.bone_r
# create normalization instance
norm = matplotlib.colors.Normalize(vmin=0, vmax=1000)
# create a scalarmappable from the colormap
sm = matplotlib.cm.ScalarMappable(cmap=cmap, norm=norm)
sm.set_array([])
barHeight = 3
ticklist = []
def drawLoadDuration(period, starty, voltage):
ax3.broken_barh((period), (starty, barHeight), alpha=1,
facecolors=cmap(norm(voltage)), lw=0.5, zorder=2)
ticklist.append(starty+barHeight/2.0)
return 0
drawLoadDuration([(0, 5), (13, 4), (19, 3), (23, 1)], 3, 500) #Fan
drawLoadDuration([(19, 1)], 9, 650) #Tube Light
drawLoadDuration([(19, 5)], 15, 350) #Bulb
drawLoadDuration([(7, 2), (16, 1)], 21, 280) #Charger
drawLoadDuration([(15, 0.5), (20, 1)], 27, 700) #Television
drawLoadDuration([(9, 1), (17, 1)], 33, 1000) #Pump
drawLoadDuration([(2,4)], 39, 800) #Scooter
ax3.set_ylim(0, 45)
ax3.set_xlim(0, 24)
ax3.set_xlabel('Time (Hours)')
ax3.set_yticks(ticklist)
xticklist = np.linspace(0.5, 24, 48)
ax3.set_xticks(xticklist)
ax3.set_xticklabels(["{}{}m".format(int(h%12+12*(h%12==0)),
{0:"p",1:"a"}[(h%24)<12]) if ((h*10)%10)==0 \
else "" for h in xticklist], fontsize='9', rotation=90)
ax3.tick_params('x', colors=cmap(1.0), tick1On=True)
ax3.set_yticklabels(['Fan', 'Tube light', 'Bulb', 'Cellphone Charger', 'Television', 'Pump', 'Scooter'])
######################### Code Block for Colorbar
# using scalarmappable to create colorbar
cbar = f.colorbar(sm, ticks=[10,100,200,500,800,1000], aspect=10, orientation='vertical', ax=ax3, label='Power')
plt.show()

Looking to add multiple xtick labels for two parallel bar charts

I have a dataset that looks like this:
I want to do the following:
Make sure the bars do not overlap.
Treat each bar like a separate dataset, i.e. the labels on the x axis should be separate, one for the yellow series, one for the red series. These labels should be the words (I want to have two series of xtick labels in this chart) One for words_2, and one for words_1..
Current code:
import matplotlib.pyplot as plt
import numpy as np
import copy
import random
from random import randint
random.seed(11)
word_freq_1 = [('test', 510), ('Hey', 362), ("please", 753), ('take', 446), ('herbert', 325), ('live', 222), ('hate', 210), ('white', 191), ('simple', 175), ('harry', 172), ('woman', 170), ('basil', 153), ('things', 129), ('think', 126), ('bye', 124), ('thing', 120), ('love', 107), ('quite', 107), ('face', 107), ('eyes', 107), ('time', 106), ('himself', 105), ('want', 105), ('good', 105), ('really', 103), ('away',100), ('did', 100), ('people', 99), ('came', 97), ('say', 97), ('cried', 95), ('looked', 94), ('tell', 92), ('look', 91), ('world', 89), ('work', 89), ('project', 88), ('room', 88), ('going', 87), ('answered', 87), ('mr', 87), ('little', 87), ('yes', 84), ('silly', 82), ('thought', 82), ('shall', 81), ('circle', 80), ('hallward', 80), ('told', 77), ('feel', 76), ('great', 74), ('art', 74), ('dear',73), ('picture', 73), ('men', 72), ('long', 71), ('young', 70), ('lady', 69), ('let', 66), ('minute', 66), ('women', 66), ('soul', 65), ('door', 64), ('hand',63), ('went', 63), ('make', 63), ('night', 62), ('asked', 61), ('old', 61), ('passed', 60), ('afraid', 60), ('night', 59), ('looking', 58), ('wonderful', 58), ('gutenberg-tm', 56), ('beauty', 55), ('sir', 55), ('table', 55), ('turned', 54), ('lips', 54), ("one's", 54), ('better', 54), ('got', 54), ('vane', 54), ('right',53), ('left', 53), ('course', 52), ('hands', 52), ('portrait', 52), ('head', 51), ("can't", 49), ('true', 49), ('house', 49), ('believe', 49), ('black', 49), ('horrible', 48), ('oh', 48), ('knew', 47), ('curious', 47), ('myself', 47)]
word_freq_2 = [((tuple[0], randint(1,500))) for i,tuple in enumerate(word_freq_1)]
N = 25
ind = np.arange(N) # the x locations for the groups
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
words_1 = [x[0] for x in word_freq_1][:25]
values_1 = [int(x[1]) for x in word_freq_1][:25]
words_2 = [x[0] for x in word_freq_2][:25]
values_2 = [int(x[1]) for x in word_freq_2][:25]
print words_2
rects1 = ax.bar(ind, values_1, color='r')
rects2 = ax.bar(ind + width, values_2, width, color='y')
# add some text for labels, title and axes ticks
ax.set_ylabel('Words')
ax.set_title('Word Frequencies by Test and Training Set')
ax.set_xticks(ind + width)
ax.set_xticklabels(words_2,rotation=90)
ax.tick_params(axis='both', which='major', labelsize=6)
ax.tick_params(axis='both', which='minor', labelsize=6)
fig.tight_layout()
ax.legend((rects1[0], rects2[0]), ('Test', 'Train'))
plt.savefig('test.png')
I found a solution to this. The key is to set two types of xticks as minor and major. In addition, the overlapping bars was due to me not specifying the bar width for rects1.
rects1 = ax.bar(ind, values_1, width,color='r')
rects2 = ax.bar(ind + width, values_2, width, color='y')
ax.set_ylabel('Words')
ax.set_title('Word Frequencies by Test and Training Set')
ax.set_xticks(ind,minor=False)
ax.set_xticks(ind + width,minor=True)
ax.set_xticklabels(words_1,rotation=90,minor=False,ha='left')
ax.set_xticklabels(words_2,rotation=90,minor=True,ha='left')
ax.tick_params(axis='both', which='major', labelsize=6)
ax.tick_params(axis='both', which='minor', labelsize=6)
Which results in: