简体   繁体   English

我有一个数据框 df,我想绘制一个径向条形图/柱形图,其中的值映射到城市

[英]I have a dataframe df and I want to draw a radial bar/column chart with the values mapped against the cities

I have a dataframe df and I want to draw a radial bar/column chart with the values mapped against the cities.我有一个数据框 df,我想绘制一个径向条形图/柱形图,其中的值映射到城市。

The dataframe looks like:数据框看起来像:

City城市 Counts计数
Leeds利兹 5 5个
Liverpool利物浦 7 7
Birmingham伯明翰 8 8个
Nottingham诺丁汉 14 14

I am able to get the horizontal bar chart but not the radial one我能够得到水平条形图但不能得到径向条形图

colors = ["red","green","blue","yellow","magenta","cyan"]
plt.barh("state", "counts", data = m, color = colors)
plt.xlabel("Number of Firearm Incidents per head")
plt.ylabel("LSOA Region")
plt.title("Firearm Incidents vs LSOA per head")
plt.show()

I want something like these two 1 2我想要像这两个这样的东西1 2

This is a possible approach:这是一种可能的方法:

# import pandas for data wrangling
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

# Build a dataset
df = pd.DataFrame({'City': ['Leeds', 'Liverpool', 'Birmingham', 'Nottingham'],
                   'Counts': [5, 7, 8, 14]})

# Reorder the dataframe
df = df.sort_values(by=['Counts'])

# initialize the figure
plt.figure(figsize=(20,10))
ax = plt.subplot(111, polar=True)
plt.axis('off')

# Constants = parameters controling the plot layout:
upperLimit = 100
lowerLimit = 30
labelPadding = 4

# Compute max and min in the dataset
max = df['Counts'].max()

# Let's compute heights: they are a conversion of each item value in those new coordinates
# In our example, 0 in the dataset will be converted to the lowerLimit (10)
# The maximum will be converted to the upperLimit (100)
slope = (max - lowerLimit) / max
heights = slope * df.Counts + lowerLimit

# Compute the width of each bar. In total we have 2*Pi = 360°
width = 2*np.pi / len(df.index)

# Compute the angle each bar is centered on:
indexes = list(range(1, len(df.index)+1))
angles = [element * width for element in indexes]
angles

# Draw bars
bars = ax.bar(
    x=angles, 
    height=heights, 
    width=width, 
    bottom=lowerLimit,
    linewidth=2, 
    edgecolor="white",
    color="#61a4b2",
)

# Add labels
for bar, angle, height, label in zip(bars,angles, heights, df["City"]):

    # Labels are rotated. Rotation must be specified in degrees :(
    rotation = np.rad2deg(angle)

    # Flip some labels upside down
    alignment = ""
    if angle >= np.pi/2 and angle < 3*np.pi/2:
        alignment = "right"
        rotation = rotation + 180
    else: 
        alignment = "left"

    # Finally add the labels
    ax.text(
        x=angle, 
        y=lowerLimit + bar.get_height() + labelPadding, 
        s=label, 
        ha=alignment, 
        va='center', 
        rotation=rotation, 
        rotation_mode="anchor") 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我有两个数据框(DF1)和(DF2)。 我想替换 (DF2) 中与 (DF1) 的两列上的条件匹配的列的值 - I have two dataframes (DF1) and (DF2). I want to substitute values for a column in (DF2) that match criteria on two columns of (DF1) 在遍历嵌套列表后,我需要创建一个 Dataframe。 我有 6 个值希望映射到 Dataframe 中的列 - I need to create a Dataframe after iterating through a nested list. I have 6 values that I would like mapped to a column in a Dataframe 我想对列名进行分组并将它们的值添加到 df 中 - I want to group column names and add their values in a df 我有两个 df,我想要比较 df2 中不存在的值 - I have two df which i want the values which are not present in df2 when compared 我想在熊猫数据框中编码列值 - I want to encode the column values in pandas dataframe 我在 pandas dataframe 列中有字典作为值。 我想将键列和值作为列值 - I have dictionary as value in pandas dataframe columns. I want to make the keys columns and values as column value 我想使用python中的数据框列创建饼图 - I want to create a pie chart using a dataframe column in python I have two pyspark dataframe and want to calculate sum in points colum in second dataframe based on column values in first dataframe - I have two pyspark dataframe and want to calculate sum in points colum in second dataframe based on column values in first dataframe 当我有一个名为“df”的数据框并将 df[&#39;column_name&#39;][-1] 放入 python 时,我会得到什么? - What do i get when i have a dataframe called “df” and i put df['column_name'][-1] in python? 我有一个数据框,想根据前一行填充所有空列值,并使用基于另一列的相同值? - I have a dataframe and want to fill all empty column values based on previous rows with identical values based in another column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM