简体   繁体   中英

Pandas Plot range as bar

I have the following Dataframe(this table is just an example, the Types and sizes are more):

df = pd.DataFrame({
'type':['A','A','B','B','C','C','D','D'],
'size':['a','b','c','d','e','f','g','h'],
'Nx':[4.3,2.4,2.5,4.4,3.5,1.8,4.5,2.8],
'min':[0.5,2.5,0.7,3.2,0.51,2,0.3,3],
'max':[1.5,3.4,1.7,4.3,1.51,3,1.2,4]})

print(df)
ax=df.plot.bar(x='type',y='max',stacked=True,bottom=df['min'])
ax.plt(x='type',y='Nx')

This is the result:

  type size   Nx   min   max
0    A    a  4.3  0.50  1.50
1    A    b  2.4  2.50  3.40
2    B    c  2.5  0.70  1.70
3    B    d  4.4  3.20  4.30
4    C    e  3.5  0.51  1.51
5    C    f  1.8  2.00  3.00
6    D    g  4.5  0.30  1.20
7    D    h  2.8  3.00  4.00

在此处输入图像描述

how can i plot this data by having just one column for Type A, B,C.. And then plot scatter for Type,Nx to be like this:在此处输入图像描述

You can add a new column called height equal to max - min since the plt.bar method takes a height parameter, then reindex the DataFrame by ['type','size'] . Then loop through the levels of this multiindex DataFrame and plot a bar with a different color for each unique type and size combination.

This also requires you to define your own color palette. I chose a discrete color palette from plt.cm and mapped integer values to each color. As you are looping through each unique type and size, you can have a counter for the inner most loop to ensure that each bar within the same type has a different color.

NOTE: this does make the assumption that there aren't multiple rows with the same type and size.

To show this is generalizable, I added another bar of type 'D' and size 'i' and it appears as a distinct bar in the plot.

import pandas as pd
import matplotlib.pyplot as plt

## added a third size to type D
df = pd.DataFrame({
'type':['A','A','B','B','C','C','D','D','D'],
'size':['a','b','c','d','e','f','g','h','i'],
'Nx':[4.3,2.4,2.5,4.4,3.5,1.8,4.5,2.8,5.6],
'min':[0.5,2.5,0.7,3.2,0.51,2,0.3,3,4.8],
'max':[1.5,3.4,1.7,4.3,1.51,3,1.2,4,5.3]})

## create a height column for convenience
df['height'] = df['max'] - df['min']
df_grouped = df.set_index(['type','size'])

## create a list of as many colors as there are categories
cmap = plt.cm.get_cmap('Accent', 10)

## loop through the levels of the grouped DataFrame
for each_type, df_type in df_grouped.groupby(level=0):
    color_idx=0
    for each_size, df_type_size in df_type.groupby(level=1):
        color_idx += 1
        plt.bar(x=[each_type]*len(df_type_size), height=df_type_size['height'], bottom=df_type_size['min'], width=0.4, 
            edgecolor='grey', color=cmap(color_idx))
        plt.scatter(x=[each_type]*len(df_type_size), y=df_type_size['Nx'], color=cmap(color_idx))

plt.ylim([0, 7])
plt.show()

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM