簡體   English   中英

如果條件為真,則行 plot 帶有不同的標記 python 3

[英]Line plot with different markers if condition is true python 3

我正在嘗試從數組的第 1 列創建一行 plot 。 如果同一數組的第 2 列中的某個條件已滿,則 plot 行的標記應更改(如果條件已滿,則標記 ='o',如果條件為假,則標記 ='x'。但是,結果我的 plot 不正確。

import numpy as np
import matplotlib.pyplot as plt
import random

###These are 100 random numbers
randomlist = random.sample(range(0, 100), 100)

###This is an array with 50 rows and 2 columns
arr = np.array(randomlist)
arr_re = arr.reshape(50,2)

### This is a lineplot of column 1 with different markers dependent on the value of column 2
figure, ax = plt.subplots(figsize=(13, 6))
for i in range(0,50,1):
 #figure, ax = plt.subplots(figsize=(13, 6))
 if arr_re[i,1] > 50:
  ax.plot(arr_re[i,0], color="black", marker='o', label='1880-1999')
 else:
  ax.plot(arr_re[i,0], color="black", marker='x', label='1880-1999')
plt.show()

也許有人可以給我一個提示。 干杯icorrect_result plot 應該看起來像這樣,但是根據 column2 的條件更改標記

上面代碼的主要問題是您忘記向 plot function 添加 x 值。 實現您的目標的一種方法是首先 plot 隨機點線,然后 plot 使用不同標記的點的分散。 請參閱下面我對您的代碼的調整。

import numpy as np
import matplotlib.pyplot as plt
import random

###These are 100 random numbers
randomlist = random.sample(range(0, 100), 100)

###This is an array with 50 rows and 2 columns
arr = np.array(randomlist)
arr_re = arr.reshape(50,2)

### This is a lineplot of column 1 with different markers dependent on the value of column 2
figure, ax = plt.subplots(figsize=(13, 6))

# plot column 1
plt.plot(arr_re[:,0])

# scatter plot the markers based on a condition
for i in range(0,50,1):
    if arr_re[i,1] > 50:
        ax.scatter(i,arr_re[i,0], color="black", marker='o', label='1880-1999')
    else:
        ax.scatter(i,arr_re[i,0], color="black", marker='x', label='1880-1999')
plt.show()

結果是:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM