Python 字典散点图字典

Python 字典散点图字典,python,dictionary,matplotlib,plot,Python,Dictionary,Matplotlib,Plot,我需要一本字典的散点图 我的数据如下所示: {'+C': {1: 191, 2: 557}, '+B': None, '-B': None, '+D': None, '+N': {1: 1, 3: 1}, '+L': {1: 2819, 2: 1506}, '>L': None, '<C': {0: 2125}, '<B': None, '<L': {0: 2949, 1: 2062}} 您需要为x设置整数类别,然后可以在x轴上指定标签。下面的代码使用dict中的操作代

我需要一本字典的散点图

我的数据如下所示:

{'+C': {1: 191, 2: 557}, '+B': None, '-B': None, '+D': None, '+N': {1: 1, 3: 1}, '+L': {1: 2819, 2: 1506}, '>L': None, '<C': {0: 2125}, '<B': None, '<L': {0: 2949, 1: 2062}}

您需要为x设置整数类别,然后可以在x轴上指定标签。下面的代码使用dict中的操作代码进行此操作。请注意,python中的字典是无序的,因此需要对键进行排序。您可以改为使用有序dict,然后直接使用dict.keys()

每个点的注释都是字符串,一次放置一个字符串,并在绘图上添加文本注释。我们需要使用plt.axis()在绘图上显式设置x、y轴范围,因为自动计算范围时不包括注释

import matplotlib.pyplot as plt

action_sequence = {
    '+C': {1: 191, 2: 557}, '+B': None, '-B': None, '+D': None, 
    '+N': {1: 1, 3: 1}, '+L': {1: 2819, 2: 1506}, 
    '>L': None, '<C': {0: 2125}, '<B': None, '<L': {0: 2949, 1: 2062}
}

# x data are categorical; define a lookup table mapping category string to int
x_labels = list(sorted(action_sequence.keys()))
x_values = list(range(len(x_labels)))
lookup_table = dict((v,k) for k,v in enumerate(x_labels))

# Build a list of points (x, y, annotation) defining the scatter plot.
points = [(lookup_table[action], key, anno)
      for action, values in action_sequence.items()
      for key, anno in (values if values else {}).items()]
x, y, anno = zip(*points)

# y is also categorical, with integer labels for the categories
y_values = list(range(min(y), max(y)+1))
y_labels = [str(v) for v in y_values]

plt.figure(figsize=(10,8))
plt.title('Scatter Plot', fontsize=20)
plt.xlabel('x', fontsize=15)
plt.ylabel('y', fontsize=15)
plt.xticks(x_values, x_labels)
plt.yticks(y_values, y_labels)
plt.axis([min(x_values)-0.5, max(x_values)+0.5, 
          min(y_values)-0.5, max(y_values)+0.5])
#plt.scatter(x, y, marker = 'o')
for x_k, y_k, anno_k in points:
    plt.text(x_k, y_k, str(anno_k))

plt.show()
导入matplotlib.pyplot作为plt
动作顺序={
“+C”:{1:191,2:557},“+B”:无,“-B”:无,“+D”:无,
“+N”:{1:1,3:1},“+L”:{1:2819,2:1506},

“>L”:无,您需要为x设置整数类别,然后在x轴上分配标签。下面的代码使用dict中的操作代码来实现此目的。请注意,python中的字典是无序的,因此需要对键进行排序。您可以改为使用有序dict,然后直接使用dict.keys()

每个点的注释都是字符串,在绘图上一次放置一个文本注释。我们需要使用plt.axis()在绘图上显式设置x、y轴范围,因为自动计算范围时不包括注释

import matplotlib.pyplot as plt

action_sequence = {
    '+C': {1: 191, 2: 557}, '+B': None, '-B': None, '+D': None, 
    '+N': {1: 1, 3: 1}, '+L': {1: 2819, 2: 1506}, 
    '>L': None, '<C': {0: 2125}, '<B': None, '<L': {0: 2949, 1: 2062}
}

# x data are categorical; define a lookup table mapping category string to int
x_labels = list(sorted(action_sequence.keys()))
x_values = list(range(len(x_labels)))
lookup_table = dict((v,k) for k,v in enumerate(x_labels))

# Build a list of points (x, y, annotation) defining the scatter plot.
points = [(lookup_table[action], key, anno)
      for action, values in action_sequence.items()
      for key, anno in (values if values else {}).items()]
x, y, anno = zip(*points)

# y is also categorical, with integer labels for the categories
y_values = list(range(min(y), max(y)+1))
y_labels = [str(v) for v in y_values]

plt.figure(figsize=(10,8))
plt.title('Scatter Plot', fontsize=20)
plt.xlabel('x', fontsize=15)
plt.ylabel('y', fontsize=15)
plt.xticks(x_values, x_labels)
plt.yticks(y_values, y_labels)
plt.axis([min(x_values)-0.5, max(x_values)+0.5, 
          min(y_values)-0.5, max(y_values)+0.5])
#plt.scatter(x, y, marker = 'o')
for x_k, y_k, anno_k in points:
    plt.text(x_k, y_k, str(anno_k))

plt.show()
导入matplotlib.pyplot作为plt
动作顺序={
“+C”:{1:191,2:557},“+B”:无,“-B”:无,“+D”:无,
“+N”:{1:1,3:1},“+L”:{1:2819,2:1506},
“>L”:无,'