此文轉載自:https://blog.csdn.net/weixin_44108388/article/details/110205949
Draw Gradient Color Map using python
github源碼地址:
歡迎關注我!
pratical_skills_tools
Draw Gradient Color Map
Dependencies
- pandas
- matplotlib
- numpy
- seaborn
You can configure it using pip install XXX
in terminal.
Results
figure1:
figure2:
Using and Explanation
1.改變顏色的種類
在程序path_Results.py
文件中,修改plot_results_path
函數中的:
lc = gd.colorline(x, y, z, cmap=plt.get_cmap('cool'), linewidth=linewidth_car) # 'jet' #'cool'
中的cmap=plt.get_cmap('cool')
參數即可。可以設置的有:
參考 matplotlib tutorials中的colors部分
舉例:
2.改變圖例的標注范圍
可以進行圖例colorbar刻度的調整,包括平移和比例放縮:
在程序path_Results.py
文件中,plot_gd_bar
函數中的:
def plot_gd_bar(fig, ax, lc, max_pro, max_tran=0, cars_num=1, car_num=0, offset=0):
參數:
max_pro
: 調整比例
max_tran
: 調整偏移量
比如main.py
中函數調用的時候:
cb = paths.plot_gd_bar(fig, ax, lc, result0[-1, 2], 10)
#最后兩個參數一個是調整比例,一個是調整偏移量
比例放縮的含義:原本[0,5]可以調整為[0,10],在上面函數調用中的result0[-1, 2]
處寫上2
即可;
平移的含義:原本[0,5]可以調整為[2,7],在上面函數調用中的10
處寫上2
即可。
具體效果可以看figure2和figure3的對比,偏移了10個單位。
Steps:
1.load CSV files
result0 = pd.read_csv("mp_1.csv")
result1 = pd.read_csv("mp_2.csv")
result0 = result0.values
result1 = result1.values
the file:
first row: x data
second row: y data
third row: z data, which is to set the gradient color map.
2.set the size of map and the limits of x axis and y axis
#設置畫布
width_img = 5
height_img = 5
fig = plt.figure(figsize=(int(width_img)+2, int(height_img)+2),
facecolor='none')
ax = plt.gca()
#設置圖像上下界
plt.xlim(0,20)
plt.ylim(0,20)
3.draw color map
lc = paths.plot_results_path(result0,4)
lc2 = paths.plot_results_path(result1, 4)
4.draw colorbar
可以進行圖例colorbar刻度的調整,包括平移和比例放縮:
比例放縮的含義:原本[0,5]可以調整為[0,10],在下面函數調用中的result0[-1, 2]
處寫上2
即可;
平移的含義:原本[0,5]可以調整為[2,7],在下面函數調用中的10
處寫上2
即可;
cb = paths.plot_gd_bar(fig, ax, lc, result0[-1, 2], 10)
#最后兩個參數一個是調整比例,一個是調整偏移量
Notice:
plot_gd_bar(fig, ax, lc, result0[-1, 2], 10)
#最后兩個參數一個是調整比例,一個是調整偏移量
Notice:
以上方便修改和調整的是main.py
和path_Result.py
,gradient.py
最好不要修改。