python - 使用 Matplotlib Python 绘制图形
问题描述
我想以不同的图形打印图形,但所有图形都是重叠的。在这里,我的代码中有一个 for 循环(第三个 for 循环),并且我在size 存储的计数中访问numpyArraysDisplay.size
and的位置。x
现在第一行计数0 to 44
,然后第二行计数,依此类推(大小不固定)。
之后,我将其附加到x
一个名为position
并绘制图形的列表中,但对于第一行,它完美地保存了图形,但对于第二行,它与同一图中的第一行重叠,依此类推。
但是我想要所有线的所有图表都在不同的图中。我已经显示了重叠图的图形。
我写了下面的代码:
import numpy as np
import csv
from scipy.stats import entropy
from matplotlib import pyplot as plt
position = []
store_entropy = [] # Empty list defined to store the list of entropy
outputfile = open("Output.txt", "w") # Output file open in write mode for saving the outputs
# Reading the csv file by reader
with open('data.csv', 'r') as file:
reader = csv.reader(file)
for line in reader:
lines = filter(None, line[2:])
lines1 = ' '.join(lines).split(',')
# Loop for every rows in the file
for row in lines1[::]:
makeOneLine = row.rstrip('\n')
delimiter = " "
makeOneLine = delimiter.join(makeOneLine.split(delimiter, 3))
# Makes Numpy Array of all the lines in the file
numpyArraysDisplay = np.array(list(makeOneLine.split(" ")))
# print(numpyArraysDisplay)
outputfile.write(str(numpyArraysDisplay) + '\n')
# Loop for defining the first segment of the the Numpy Arrays
for x in range(numpyArraysDisplay.size):
part1 = numpyArraysDisplay[:x + 1] # First segment divide
strings, counts = np.unique(part1, return_counts=True)
# Counting the frequency of the words appearing in the file line by line
CountWordsfrequency = np.array(list(dict(zip(strings, counts)).values()))
# print(CountWordsfrequency)
outputfile.write(str(CountWordsfrequency) + '\n')
# Loop for finding the Probability and Entropy of the dialogue types
for y in range(0, CountWordsfrequency.size):
probability = CountWordsfrequency[y] / part1.size
outputfile.write("Probability is : " + str(probability) + '\n')
ent2 = entropy(counts, base=10)
outputfile.write("Entropy is \t" + str(ent2) + '\n')
# Store all the Entropies in a list for further work
store_entropy.append(ent2)
# print(store_entropy)
position.append(x+1)
print(position)
plt.figure(figsize=(15, 7))
plt.plot(position, store_entropy, '-o')
plt.xlabel("Segmentation Break Point")
plt.ylabel("Entropy")
plt.xticks(np.arange(0, len(position) + 1, 1))
plt.savefig('Plots/', figurewidth='25cm')
plt.show()
plt.clf()
size = len(store_entropy)
# One Line for loop and if condition to slipt the list by particular values as
# we have to give list inside list for passing particular index of entropy
idx_list = [idx for idx, val in enumerate(store_entropy) if val == 0.0]
res = [store_entropy[i: j] for i, j in zip([0] + idx_list, idx_list + ([size] if idx_list[-1] !=
size else []))]
# print(res)
# for loop and if condition for setting the lists of the Entropies index starting
# from zero instead of 1 and removing the empty lists created in the lists.
res2 = [x for x in res if x != []]
#print(res2)
# print("The List after Split:" + str(res))
#print(res2[0])
但我想要不同图形上的所有图表。
解决方案
在上述情况下,当我查看我的代码时,我发现如果我想将列表传递给,plt.plot()
那么我们可能只传递列表变量。当我们使用 for 循环绘制图形时,它会自动获取索引和值。
我有以下解决方案并取得了结果。
for a, b in enumerate(res2):
plt.figure(figsize=(15, 7))
plt.plot(b, '-*', color="red")
plt.xlabel("Dialogue Act Types")
plt.ylabel("Entropy")
plt.xticks(np.arange(0, len(b) + 1, 1))
plt.savefig('Plots/', figurewidth='25cm')
plt.show()
plt.close()
推荐阅读
- java - 赋值操作期间未抛出空指针异常
- angular - Angular 8:当用户点击子组件文本框时让父组件检测
- python - 网络抓取 Yelp,我如何检索每个单独评分的值?
- python - 列表在循环中附加一个对象。我该如何纠正这个错误?
- android - 在 Android Q 上,如何获取左侧导航手势区域的垂直坐标?
- php - 如何在codeigniter中调用自定义助手的自定义兄弟方法并使用数据库
- django - 如何在 Django 视图中存储全局变量
- javascript - 了解 express 是什么以及如何使用它
- javascript - CSS 过渡,同时将对象保持在中心视口中
- reactjs - Chrome 审计:第三方代码中的 Datacamp