📌  相关文章
📜  如何更改 matplotlib 中的颜色条标签?

📅  最后修改于: 2022-05-13 01:54:58.004000             🧑  作者: Mango

如何更改 matplotlib 中的颜色条标签?

在本文中,我们将了解如何使用Python在 matplotlib 中更改颜色条标签。

colorbar()函数用于绘制颜色条,它属于 matplotlib 的 pyplot 模块,在绘图中添加了一个颜色条,指示色标。

创建一个简单的颜色条进行演示

要创建颜色条,我们将使用 color() 方法,为此,我们将创建数据集,然后使用散点图进行演示。

Python3
# Python Program illustrating
# pyplot.colorbar() method
import numpy as np
import matplotlib.pyplot as plt
 
# Dataset
# List of total number of items purchased
# from each products
purchaseCount = [100, 200, 150, 23, 30, 50,
                156, 32, 67, 89]
 
# List of total likes of 10 products
likes = [50, 70, 100, 10, 10, 34, 56, 18, 35, 45]
 
# List of Like/Dislike ratio of 10 products
ratio = [1, 0.53, 2, 0.76, 0.5, 2.125, 0.56,
        1.28, 1.09, 1.02]
 
# scatterplot
plt.scatter(x=purchaseCount, y=likes, c=ratio, cmap="summer")
 
plt.colorbar()
plt.show()


Python3
# Python Program illustrating
# pyplot.colorbar() method
import numpy as np
import matplotlib.pyplot as plt
 
# Dataset
# List of total number of items purchased
# from each products
purchaseCount = [100, 200, 150, 23, 30, 50,
                156, 32, 67, 89]
 
# List of total likes of 10 products
likes = [50, 70, 100, 10, 10, 34, 56, 18, 35, 45]
 
# List of Like/Dislike ratio of 10 products
ratio = [1, 0.53, 2, 0.76, 0.5, 2.125, 0.56,
        1.28, 1.09, 1.02]
 
# scatterplot
plt.scatter(x=purchaseCount, y=likes, c=ratio,
            cmap="summer")
 
plt.colorbar().ax.tick_params(labelsize=10)
 
plt.show()


Python3
# Python Program illustrating
# pyplot.colorbar() method
import numpy as np
import matplotlib.pyplot as plt
 
# Dataset
# List of total number of items purchased
# from each products
purchaseCount = [100, 200, 150, 23, 30, 50,
                156, 32, 67, 89]
 
# List of total likes of 10 products
likes = [50, 70, 100, 10, 10, 34, 56, 18, 35, 45]
 
# List of Like/Dislike ratio of 10 products
ratio = [1, 0.53, 2, 0.76, 0.5, 2.125, 0.56,
        1.28, 1.09, 1.02]
 
# scatterplot
plt.scatter(x=purchaseCount, y=likes, c=ratio,
            cmap="summer")
 
plt.colorbar().ax.set_ylabel('Label',
                             rotation=120)
 
plt.show()


Python3
# Python Program illustrating
# pyplot.colorbar() method
import numpy as np
import matplotlib.pyplot as plt
 
# Dataset
# List of total number of items purchased
# from each products
purchaseCount = [100, 200, 150, 23, 30, 50,
                156, 32, 67, 89]
 
# List of total likes of 10 products
likes = [50, 70, 100, 10, 10, 34, 56, 18, 35, 45]
 
# List of Like/Dislike ratio of 10 products
ratio = [1, 0.53, 2, 0.76, 0.5, 2.125, 0.56,
        1.28, 1.09, 1.02]
 
# scatterplot
plt.scatter(x=purchaseCount, y=likes, c=ratio, cmap="summer")
plt.colorbar(orientation="horizontal").ax.set_xticklabels(ratio, rotation=45)
 
plt.show()


输出:

方法 1:更改颜色标签中的标签字体大小

要更改标签的字体大小,我们将使用ax.tick_params()方法来增加标签的字体。

Python3

# Python Program illustrating
# pyplot.colorbar() method
import numpy as np
import matplotlib.pyplot as plt
 
# Dataset
# List of total number of items purchased
# from each products
purchaseCount = [100, 200, 150, 23, 30, 50,
                156, 32, 67, 89]
 
# List of total likes of 10 products
likes = [50, 70, 100, 10, 10, 34, 56, 18, 35, 45]
 
# List of Like/Dislike ratio of 10 products
ratio = [1, 0.53, 2, 0.76, 0.5, 2.125, 0.56,
        1.28, 1.09, 1.02]
 
# scatterplot
plt.scatter(x=purchaseCount, y=likes, c=ratio,
            cmap="summer")
 
plt.colorbar().ax.tick_params(labelsize=10)
 
plt.show()

输出:

方法二:改变和旋转标签的位置

要旋转颜色条标签,我们将对水平和垂直使用 set_xticklabels() 和 set_yticklabels() 方法。

示例 1:水平更改位置

Python3

# Python Program illustrating
# pyplot.colorbar() method
import numpy as np
import matplotlib.pyplot as plt
 
# Dataset
# List of total number of items purchased
# from each products
purchaseCount = [100, 200, 150, 23, 30, 50,
                156, 32, 67, 89]
 
# List of total likes of 10 products
likes = [50, 70, 100, 10, 10, 34, 56, 18, 35, 45]
 
# List of Like/Dislike ratio of 10 products
ratio = [1, 0.53, 2, 0.76, 0.5, 2.125, 0.56,
        1.28, 1.09, 1.02]
 
# scatterplot
plt.scatter(x=purchaseCount, y=likes, c=ratio,
            cmap="summer")
 
plt.colorbar().ax.set_ylabel('Label',
                             rotation=120)
 
plt.show()

输出:

示例 2:垂直更改位置

Python3

# Python Program illustrating
# pyplot.colorbar() method
import numpy as np
import matplotlib.pyplot as plt
 
# Dataset
# List of total number of items purchased
# from each products
purchaseCount = [100, 200, 150, 23, 30, 50,
                156, 32, 67, 89]
 
# List of total likes of 10 products
likes = [50, 70, 100, 10, 10, 34, 56, 18, 35, 45]
 
# List of Like/Dislike ratio of 10 products
ratio = [1, 0.53, 2, 0.76, 0.5, 2.125, 0.56,
        1.28, 1.09, 1.02]
 
# scatterplot
plt.scatter(x=purchaseCount, y=likes, c=ratio, cmap="summer")
plt.colorbar(orientation="horizontal").ax.set_xticklabels(ratio, rotation=45)
 
plt.show()

输出: