How can I change a single bar in a bar plot to a different colour? - pandas

I have a dataframe that looks as follows
ACQUISITION_CHANNEL
RIDER_ID
Organic
2735
Referral
1216
Digital
751
Offline
296
Unknown
108
Job Platforms
67
And I am making a bar plot as below using:
channel_rider_count.plot(kind='bar',
legend=None)
plt.title('Count of Riders by Acquisition Channel')
plt.xlabel('Acquisition Channel')
plt.ylabel('Count')
How can I change the colour of the 'Referral' bar but leave the others the same?

colors = ['b','r','b','b','b','b','b']
channel_rider_count.plot(kind='bar',
legend=None,
color=colors)
plt.title('Count of Riders by Acquisition Channel')
plt.xlabel('Acquisition Channel')
plt.ylabel('Count')

Related

creating legend for US cities map using basemap and matplotlib

I have a large dataframe with 5 columns, State, City , Count, latitude, longitude. I am trying to create a geographical map using basemap with 50 US State and cities, in this map the count value is shown by the red circle. The size of circle indicate the counts value.
Here is a sample of dataframe :
city
state
count
latitude
longitude
BROOKLYN
NY
831
40.649188
-73.933724
NEW YORK
NY
646
40.734332
-74.010112
CHICAGO
IL
614
41.850100
-87.650000
HOUSTON
TX
530
29.741797
-95.309376
BRONX
NY
415
40.816461
-73.862173
MIAMI
FL
401
25.752956
-80.271061
PHOENIX
AZ
382
33.859694
-112.115872
DALLAS
TX
311
32.902156
-96.794543
SAN ANTONIO
TX
259
29.518456
-98.60973
ANCHORAGE
AK
20
61.189063
-149.886241
HONOLULU
HI
56
21.271982
-157.821362
PONCE
PR
61
17.987655
-66.623600
I am trying to add legend that shows the count level on lower side of the map with red circle.
This is my code:
plt.figure(figsize=[12,18])
base_map = Basemap(llcrnrlon=-119,llcrnrlat=22,urcrnrlon=-64,urcrnrlat=49,
projection='lcc',lat_1=32,lat_2=45,lon_0=-95)
# load the shapefile, use the name 'states'
#base_map.shadedrelief()
base_map.readshapefile('st99_d00', name='states', drawbounds=True)
# Get the location of each city and plot it
state_set = set(['AK','HI','PR'])
for lat, long, name, size in zip(df['latitude'].tolist(),
df'longitude'].tolist(),
df['state'].tolist(),
df['count'].tolist()):
x, y = base_map(long,lat)#lat, long)
base_map.plot(x,y,marker='o',color='Red',markersize=size/20)#label=name)
if name not in state_set:
state_set.add(name)
plt.text(x, y, name,fontsize=11, color='k')
plt.title('****')
#plt.legend()
plt.show()
I am adding this part to legend section.This code works but the legend color are gray, they are very compact not easy to read. how can I change color and make them smaller or spread out?
# make legend with dummy points
for a in [100, 300, 500,700,850]:
plt.scatter([], [], c='k', alpha=0.5, s=a,
label=str(a) + ' counts')
plt.legend(scatterpoints=1, frameon=False,
labelspacing=1, loc='lower left')
Is this right way to add the legend ?How can I adjust the legend?
I am stuck and pretty confused, I appreciate any help and feedback.
TIA!

geom_bar multiple criteria to sort and color

I have a data.table that Im plotting with geom_bar.
The data.table has multiple pieces of information (aa, codon, pos) by which the counts (value/contig) can be grouped. For now, Im plotting contig (y) at each pos (x) by aa_codon.
rn aa codon pos variable value contig
1: ASP_GAT_-17 ASP GAT -17 PC3 0.33962840 0.33962840
2: ASP_GAC_-17 ASP GAC -17 PC3 0.29253629 0.29253629
3: SER_TCT_-17 SER TCT -17 PC3 0.27009135 0.27009135
I managed to sort x by the position and the stacked bar by using fill according to which codon-pos is higher. However, each aa_codon_pos appears in one color and I would like that the color was by aa (keeping the same order). Every attempt to fill them by aa results in incorrect order of the stacked bar (since it summarizes all the codon_pos for that aa). Is there any way to sort and color the stacked bars independently?
enter image description here

How to calculate distance using voltage from Q4X Analog Laser Sensor

I have a Q4X Stainless Steel Analog Laser Sensor to calculate the distance with analog output voltage.
It does display distance on Laser Sensor display and I am trying to do the same thing in my code using scale factor but its not matches with Laser Sensor display value.
here is my scaling factor.
#define A2D_SCL_LASER ( 11.81f / ( 10.0f - 0.0f ) ) // inches per volt
Devices specs:
Supply Voltage (Vcc)
12 to 30 V dc
Sensing Range—Threaded Barrel Models
500 mm models: 25 mm to 500 mm (0.98 in to 19.68 in)
300 mm models: 25 mm to 300 mm (0.98 in to 11.81 in)
100 mm models: 25 mm to 100 mm (0.98 in to 3.94 in)
Comparison of distance calculation in my code and sensor display
My values(mm) sensor display value(mm)
1V 29.9974 52
2V 59.944 80
3V 89.916 107
4V 119.888 134
5V 150.114 162
6V 179.832 190
7V 209.804 217
8V 240.03 245
9V 270.002 272
10V 300 300
Ref:
http://info.bannerengineering.com/cs/groups/public/documents/literature/185623.pdf
I simply fitted the sensor displayed value with the voltage you give in your question. The R²=1 means that the fit is perfect (or near perfect) and this is a good sign.
The equation you are searching for is
Distance(mm) = 27.533 x Volt + 24.467

stacked bar chart from grouped object

I get the expected count of following group-by query. But when I add .plot.bar() method, I get bar chart for each record.
How do I get stacked bar chart?
df.groupby(['department', 'status'])['c_name'].count()
department status
Agriculture Accepted 3
Pending 2
Rejected 13
Department of Education and Training Accepted 290
Rejected 65
Higher Education Accepted 424
Pending 24
Rejected 92
Medical Education and Research Accepted 34
Pending 3
Rejected 1
This will create a bar chart but not the stacked one.
.plot(kind='bar', stacked=True)
For each department there should be 3 colors (for Accepted, Pending and Rejected)
Update:
I managed using pivot.
gdf=df.groupby(['department', 'status'])['c_name'].count().reset_index()
gdf.pivot(index='department', columns='status').plot(kind='bar', stacked=True)
But is it possible to improve the chart quality?
You are close, need unstack:
df.groupby(['department','status'])['c_name'].count().unstack().plot(kind='bar', stacked=True)

Bar Plot with more than one variables

I have this set of data:
TestSystems
[1] 0013-021 0013-022 0013-031 0013-032 0013-033 0013-034
Levels: 0013-021 0013-022 0013-031 0013-032 0013-033 0013-034
Utilization
[1] 61.42608 64.95802 31.51387 45.11971 43.66110 63.68363
Availability
[1] 28.92506 32.58015 11.86372 16.22164 36.23264 40.54977
str(TestSystems)
Factor w/ 6 levels "0013-021","0013-022",..: 1 2 3 4 5 6
str(Utilization)
num [1:6] 61.4 65 31.5 45.1 43.7 ...
str(Availability)
num [1:6] 28.9 32.6 11.9 16.2 36.2 ...
I would like to have a plot as below:
http://imgur.com/snPOVW5
The plot is not from R, but other software. I would like the same plot to be from R. Appreciate any help.
thanks.
I have tried with below code and it works
df <- data.frame(TestSystems, Availability, Utilization)
df.long<-melt(df)
p <- ggplot(df.long,aes(TestSystems, value,fill=variable))+ geom_bar(stat="identity",position="dodge")
how do I display those 12 values at the top of each bar?