gpt4 book ai didi

python - 偏移的颜色

转载 作者:行者123 更新时间:2023-12-01 21:22:15 28 4
gpt4 key购买 nike

我正在尝试实现这种理想的绘图图形格式: enter image description here

我成功生成了图表,也以某种方式 (plt.pcolor) 覆盖了绘图的默认颜色(这可能有点矫枉过正,但我​​找不到其他选项),但我正在努力:

  1. 下一个颜色层与主网格有偏移

enter image description here

  1. 我也不知道如何从顶部、左侧、下方移除所有勾号(棍子)但保留标签
  2. 我想将上面的标签向下移动
  3. 我还想为颜色添加 3 个条件。蓝色也可以是基于第一个绘图颜色的渐变蓝色

enter image description here

这是生成绘图的功能代码(在中间的图片上):

import numpy as np
import pandas as pd
from datetime import datetime as dt
from datetime import timedelta
import matplotlib
import matplotlib.pyplot as plt
from calendar import monthrange
import matplotlib.colors as mcolors

def create_graph(all_rows, task_names, farmers):

harvest = np.array(all_rows)
fig, ax = plt.subplots()
im = ax.imshow(harvest)

# We want to show all ticks...
ax.set_xticks(np.arange(len(farmers)))
ax.set_yticks(np.arange(len(task_names)))
# ... and label them with the respective list entries
ax.set_xticklabels(farmers, fontsize=4)
ax.set_yticklabels(task_names, fontsize=8)

# Rotate the tick labels and set their alignment.
plt.setp(ax.get_xticklabels(), rotation=0, ha="center", rotation_mode="anchor")
ax.tick_params(top=True, bottom=False, labeltop=True, labelbottom=False)

#Turn spines off and create white grid.
for edge, spine in ax.spines.items():
spine.set_visible(False)

cmap, norm = mcolors.from_levels_and_colors([0, 1, 4], ['grey', 'green'])

plt.pcolor(harvest, cmap=cmap, norm=norm, snap=True)
ax.set_xticks(np.arange(harvest.shape[1]+1)-.5, minor=True)
ax.set_yticks(np.arange(harvest.shape[0]+1)-.5, minor=True)
ax.grid(which="minor", color="w", linestyle='-', linewidth=1)

# ax.tick_params(which="minor", bottom=False, left=False)
# ax.set_title("Harvest of local farmers (in tons/year)")
fig.tight_layout()
plt.show()
plt.close()


vegetables = ["cucumber", "tomato", "lettuce", "asparagus",
"potato", "wheat", "barley"]
farmers = [1, 2, 3, 4, 5, 6, 7]

harvest = np.array([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0],
[2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0],
[1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0],
[0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0],
[0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0],
[1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1],
[0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]])

create_graph(harvest, vegetables, farmers)

我尝试查看 matplotlib,但我并不比以前更聪明。

最佳答案

一些说明:

  • 删除所有刻度线:ax.tick_params(which='both', length=0)。请注意,您可以调用 ax.tick_params多次使用不同的参数。刻度标签将自动设置得更靠近绘图。默认情况下,它仅对主要刻度进行操作,因此 which='both' 使其也对次要刻度进行操作。
  • imshow 在两半处有边框,这将整数位置很好地放置在每个单元格的中心。
  • pcolor(和 pcolormesh)假设一个位置网格(默认为整数),因此它与 imshow 对齐不佳.
  • 当次要刻度与主要刻度重叠时,它们会被抑制。因此,在这种情况下,只需将它们设置为 0.5 的倍数即可。
  • vminvmax 告诉哪个数字对应于颜色图的最低颜色,哪个对应于最高颜色。
  • 可以为所有高于 vmax 的值设置 over color。该值可以通过 extend='max' 显示在颜色栏中。 (默认情况下,所有高于 vmax 的值只使用最高颜色,同样,低于 vmin 的值使用最低颜色)。请注意,如果您使用标准颜色图并想使用 set_over,则需要制作颜色图的副本(因为 set_over 更改了颜色图,您可能需要其他地 block 不受影响)。
  • default colormap被称为“viridis”。还有许多其他可能。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
from matplotlib.ticker import MultipleLocator

def create_graph(all_rows, task_names, farmers):
harvest = np.array(all_rows)
fig, ax = plt.subplots()

# We want to show all ticks...
ax.set_xticks(np.arange(len(farmers)))
ax.set_yticks(np.arange(len(task_names)))
# ... and label them with the respective list entries
ax.set_xticklabels(farmers, fontsize=4)
ax.set_yticklabels(task_names, fontsize=8)

# Rotate the tick labels and set their alignment.
plt.setp(ax.get_xticklabels(), rotation=0, ha="center", rotation_mode="anchor")
ax.tick_params(top=True, bottom=False, labeltop=True, labelbottom=False)
ax.tick_params(which='both', length=0) # hide tick marks
ax.xaxis.set_minor_locator(MultipleLocator(0.5)) # minor ticks at halves
ax.yaxis.set_minor_locator(MultipleLocator(0.5)) # minor ticks at halves

# Turn spines off and create white grid.
for edge, spine in ax.spines.items():
spine.set_visible(False)

cmap = mcolors.LinearSegmentedColormap.from_list("grey-blue", ['grey', 'steelblue'])
cmap.set_over('green')

im = ax.imshow(harvest, cmap=cmap, vmin=0, vmax=1)
ax.grid(which="minor", color="w", linestyle='-', linewidth=1)

# ax.tick_params(which="minor", bottom=False, left=False)
# ax.set_title("Harvest of local farmers (in tons/year)")
plt.colorbar(im, extend='max' , ax=ax)
fig.tight_layout()
plt.show()
#plt.close()


vegetables = ["cucumber", "tomato", "lettuce", "asparagus",
"potato", "wheat", "barley"]
farmers = [1, 2, 3, 4, 5, 6, 7]

harvest = np.array([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0],
[2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0],
[1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0],
[0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0],
[0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0],
[1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1],
[0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]])

create_graph(harvest, vegetables, farmers)

resulting plot

上面的代码创建了一个颜色图,它从 0 的灰色平滑地过渡到 1 的蓝色。如果您想要值 0 和 1 的特殊颜色,以及中间值的更标准的颜色图,您可以使用 'over' , 一个 'under' 和一个 'bad' 颜色。 ('bad' 表示无穷大或非数字的值。)您可以复制 'harvest' 矩阵并将高值更改为 'np.nan' 以使它们具有特殊颜色。不幸的是,没有一种简单的方法可以在颜色栏中显示不良颜色,但是可以通过 extend='both' 显示“under”和“over”颜色并制作矩形(而不是三角形)与 extendrect=True

from copy import copy

cmap = copy(plt.get_cmap('hot'))
cmap.set_under('grey')
cmap.set_over('steelblue')
cmap.set_bad('green')

im = ax.imshow(np.where(harvest <= 1, harvest, np.nan), cmap=cmap, vmin=0.000001, vmax=0.999999)

plt.colorbar(im, extend='both', extendrect=True, ticks=np.arange(0, 1.01, .1), ax=ax)

3 special colors

关于python - 偏移的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63710008/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com